<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                ??一站式輕松地調用各大LLM模型接口,支持GPT4、智譜、豆包、星火、月之暗面及文生圖、文生視頻 廣告
                # Log4j – 在運行時重新加載日志記錄級別 > 原文: [https://howtodoinjava.com/log4j/how-to-reload-log4j-levels-on-runtime/](https://howtodoinjava.com/log4j/how-to-reload-log4j-levels-on-runtime/) 過多的日志記錄是導致應用性能下降的常見原因。 它是[**最佳實踐**](//howtodoinjava.com/category/best-practices/ "java best practices")之一,可確保 Java EE 應用實現中的正確日志記錄。 但是,請注意在生產環境中啟用的日志記錄級別。 過多的日志記錄將觸發服務器上的高 IO,并增加 CPU 使用率。 對于使用較舊硬件的較舊環境或處理大量并發卷的環境而言,這尤其可能成為問題。 ```java A balanced approach is to implement a "reloadable logging level" facility to turn extra logging ON / OFF when required in your day to day production support. ``` 讓我們看看如何使用 Java 7 提供的[**`WatchService`**](https://docs.oracle.com/javase/7/docs/api/java/nio/file/WatchService.html "WatchService")完成此操作。 ## 步驟 1)實現一個`WatchService`,它將監聽給定 log4j 文件中的更改 下面的給定代碼初始化了一個線程,該線程連續監視給定 log4j 文件(`StandardWatchEventKinds.ENTRY_MODIFY`)中的修改。 一旦觀察到文件更改,就會調用`configurationChanged()`方法,并使用 `DOMConfigurator.configure()`方法再次重新加載 log4j 配置。 **`Log4jChangeWatcherService.java`** ```java package com.howtodoinjava.demo; import java.io.IOException; import java.nio.file.FileSystems; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardWatchEventKinds; import java.nio.file.WatchEvent; import java.nio.file.WatchKey; import java.nio.file.WatchService; import org.apache.log4j.xml.DOMConfigurator; public class Log4jChangeWatcherService implements Runnable { private String configFileName = null; private String fullFilePath = null; public Log4jChangeWatcherService(final String filePath) { this.fullFilePath = filePath; } //This method will be called each time the log4j configuration is changed public void configurationChanged(final String file) { System.out.println("Log4j configuration file changed. Reloading logging levels !!"); DOMConfigurator.configure(file); } public void run() { try { register(this.fullFilePath); } catch (IOException e) { e.printStackTrace(); } } private void register(final String file) throws IOException { final int lastIndex = file.lastIndexOf("/"); String dirPath = file.substring(0, lastIndex + 1); String fileName = file.substring(lastIndex + 1, file.length()); this.configFileName = fileName; configurationChanged(file); startWatcher(dirPath, fileName); } private void startWatcher(String dirPath, String file) throws IOException { final WatchService watchService = FileSystems.getDefault().newWatchService(); //Define the file and type of events which the watch service should handle Path path = Paths.get(dirPath); path.register(watchService, StandardWatchEventKinds.ENTRY_MODIFY); Runtime.getRuntime().addShutdownHook(new Thread() { public void run() { try { watchService.close(); } catch (IOException e) { e.printStackTrace(); } } }); WatchKey key = null; while (true) { try { key = watchService.take(); for (WatchEvent< ?> event : key.pollEvents()) { if (event.context().toString().equals(configFileName)) { //From here the configuration change callback is triggered configurationChanged(dirPath + file); } } boolean reset = key.reset(); if (!reset) { System.out.println("Could not reset the watch key."); break; } } catch (Exception e) { System.out.println("InterruptedException: " + e.getMessage()); } } } } ``` ## 2)添加`Log4jConfigurator`,這是您的應用與 log4j 協作的接口 此類是一個工具類,它將 log4j 初始化代碼和重載策略與應用代碼分開。 **`Log4jConfigurator.java`** ```java package com.howtodoinjava.demo; public class Log4jConfigurator { //This ensures singleton instance of configurator private final static Log4jConfigurator INSTANCE = new Log4jConfigurator(); public static Log4jConfigurator getInstance() { return INSTANCE; } //This method will start the watcher service of log4j.xml file and also configure the loggers public void initilize(final String file) { try { //Create the watch service thread and start it. //I will suggest to use some logic here which will check if this thread is still alive; //If thread is killed then restart the thread Log4jChangeWatcherService listner = new Log4jChangeWatcherService(file); //Start the thread new Thread(listner).start(); } catch (Exception e) { e.printStackTrace(); } } } ``` ## 3)測試 log4j 重載事件 為了測試代碼,我記錄了兩個兩個語句:一個調試級別和一個信息級別。 兩個語句在循環 2 秒后都被記錄。 我將在一些日志語句之后更改日志記錄級別,并且應該重新加載 log4j。 **`log4j-config.xml`** ```java <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE log4j:configuration SYSTEM "log4j.dtd"> <log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/"> <appender name="console" class="org.apache.log4j.ConsoleAppender"> <param name="Target" value="System.out"/> <layout class="org.apache.log4j.PatternLayout"> <param name="ConversionPattern" value="[%t] %-5p %c %x - %m%n"/> </layout> </appender> <root> <priority value ="info" /> <appender-ref ref="console" /> </root> </log4j:configuration> ``` **`Log4jConfigReloadExample.java`** ```java package com.howtodoinjava.demo; import org.apache.log4j.Logger; public class Log4jConfigReloadExample { private static final String LOG_FILE_PATH = "C:/Lokesh/Setup/workspace/Log4jReloadExample/log4j-config.xml"; public static void main(String[] args) throws InterruptedException { //Configure logger service Log4jConfigurator.getInstance().initilize(LOG_FILE_PATH); //Get logger instance Logger LOGGER = Logger.getLogger(Log4jConfigReloadExample.class); //Print the log messages and wait for log4j changes while(true) { //Debug level log message LOGGER.debug("A debug message !!"); //Info level log message LOGGER.info("A info message !!"); //Wait between log messages Thread.sleep(2000); } } } Output: [main] INFO com.howtodoinjava.demo.Log4jConfigReloadExample - A info message !! [main] INFO com.howtodoinjava.demo.Log4jConfigReloadExample - A info message !! [main] INFO com.howtodoinjava.demo.Log4jConfigReloadExample - A info message !! [main] INFO com.howtodoinjava.demo.Log4jConfigReloadExample - A info message !! [main] INFO com.howtodoinjava.demo.Log4jConfigReloadExample - A info message !! Log4j configuration file changed. Reloading logging levels !! [main] DEBUG com.howtodoinjava.demo.Log4jConfigReloadExample - A debug message !! [main] INFO com.howtodoinjava.demo.Log4jConfigReloadExample - A info message !! [main] DEBUG com.howtodoinjava.demo.Log4jConfigReloadExample - A debug message !! [main] INFO com.howtodoinjava.demo.Log4jConfigReloadExample - A info message !! ``` **祝您學習愉快!**
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看