遠(yuǎn)程辦公”">國務(wù)院聯(lián)防聯(lián)控機(jī)制新聞發(fā)布會(huì),多次肯定“云辦公”、“遠(yuǎn)程辦公”
1056
2025-03-31
問題描述
我一個(gè)項(xiàng)目在自己項(xiàng)目里面打印日志是生效的,但是一旦把這個(gè)項(xiàng)目 install 成 jar 包時(shí),提供給其他項(xiàng)目引入 jar 使用后,再次觸發(fā)打印這個(gè) jar 里的日志時(shí),原來的配置失效,這里特別指出的是時(shí)間格式化配置失效,因?yàn)楹竺嫱ㄟ^日志 Level 能確定是生效的,但是時(shí)間格式化配置始終不生效。
Jar 代碼
package com.jite.flow.engine; import java.io.IOException; import java.util.logging.LogManager; import java.util.logging.Logger; /** * @author Lux Sun * @date 2021/10/19 */ public class LoggerBuilder { public static final Logger LOG = Logger.getLogger(LoggerBuilder.class.getName()); static { if (System.getProperty("java.util.logging.config.file") == null && System.getProperty("java.util.logging.config.class") == null) { try { LogManager.getLogManager().readConfiguration(LoggerBuilder.class.getResourceAsStream("/log.properties")); } catch (IOException e) { e.printStackTrace(); } } } public static void build() { LOG.info("Load the Logger successfully"); } }
handlers=java.util.logging.ConsoleHandler java.util.logging.ConsoleHandler.level=INFO java.util.logging.ConsoleHandler.formatter=java.util.logging.SimpleFormatter java.util.logging.SimpleFormatter.format=%1$tF %tT.%1$tL %4$s [%2$s] - %5$s %6$s%n
分析原因
默認(rèn)情況下,JDK的LogManager會(huì)在JRE目錄下的"lib/logging.properties"這個(gè)文件中讀取配置。
除此之外,LogManager還可以根據(jù)兩個(gè)系統(tǒng)屬性來允許用戶控制日志的配置
"java.util.logging.config.class"
"java.util.logging.config.file"
其中,class這個(gè)屬性優(yōu)先有效,如果設(shè)置,會(huì)忽略file這個(gè)屬性。
而且關(guān)于 JAVA 代碼中的日志級(jí)別的設(shè)置,在JDK中實(shí)際上是有默認(rèn)的日志級(jí)別的。
在配置文件:%JAVA_HOME%\jre\lib\logging.properties中;如果JAVA設(shè)置的級(jí)別比配置文件中配置的還要低,則以配置文件中配置的為準(zhǔn),如果比配置文件中的高,則以程序中配置的為準(zhǔn)。
Java中設(shè)置屬性也有兩種方法
Preferences API
啟動(dòng)的時(shí)候,命令行參數(shù)
使用Preferences API設(shè)置如下
System.setProperty("java.util.logging.config.class", "Your class");
或
System.setProperty("java.util.logging.config.file", "Your properties file");
LogManager.getLogManager().readConfiguration();
java -Djava.util.logging.config.file=abc.properties YourMainClass
指定使用 abc.properties 這個(gè)文件作為配置文件。如果需要,可以加上文件的路徑名。
Tomcat 指定日志配置文件就是使用的命令行參數(shù),用記事本打開 ${tomcat_home}/bin/catalina.bat 文件,可以看到如下內(nèi)容
if not "%LOGGING_CONFIG%" == "" goto noJuliConfig set LOGGING_CONFIG=-Dnop if not exist "%CATALINA_BASE%\conf\logging.properties" goto noJuliConfig set LOGGING_CONFIG=-Djava.util.logging.config.file="%CATALINA_BASE%\conf\logging.properties" :noJuliConfig set JAVA_OPTS=%JAVA_OPTS% %LOGGING_CONFIG%
############################################################ # Default Logging Configuration File # # You can use a different file by specifying a filename # with the java.util.logging.config.file system property. # For example java -Djava.util.logging.config.file=myfile ############################################################ ############################################################ # Global properties ############################################################ # "handlers" specifies a comma separated list of log Handler # classes. These handlers will be installed during VM startup. # Note that these classes must be on the system classpath. # By default we only configure a ConsoleHandler, which will only # show messages at the INFO and above levels. handlers= java.util.logging.ConsoleHandler,java.util.logging.FileHandler # To also add the FileHandler, use the following line instead. #handlers= java.util.logging.FileHandler, java.util.logging.ConsoleHandler # Default global logging level. # This specifies which kinds of events are logged across # all loggers. For any given facility this global level # can be overriden by a facility specific level # Note that the ConsoleHandler also has a separate level # setting to limit messages printed to the console. .level= INFO ############################################################ # Handler specific properties. # Describes specific configuration info for Handlers. ############################################################ # default file output is in user's home directory. #java.util.logging.FileHandler.pattern = %h/java%u.log #java.util.logging.FileHandler.pattern = ..\\logs\\logger.log java.util.logging.FileHandler.pattern = C:\\zzj\\logger.log java.util.logging.FileHandler.limit = 50000 java.util.logging.FileHandler.count = 1 #java.util.logging.FileHandler.formatter = java.util.logging.XMLFormatter java.util.logging.FileHandler.formatter = java.util.logging.SimpleFormatter # Limit the message that are printed on the console to INFO and above. java.util.logging.ConsoleHandler.level = INFO #java.util.logging.ConsoleHandler.formatter = java.util.logging.SimpleFormatter java.util.logging.ConsoleHandler.formatter = java.util.logging.XMLFormatter ############################################################ # Facility specific properties. # Provides extra control for each logger. ############################################################ # For example, set the com.xyz.foo logger to only log SEVERE # messages: com.xyz.foo.level = SEVERE
注意:日志文件路徑不能直接在盤符下,例如不能指定日志文件路徑為C:\\logger.log。
附
當(dāng)然,這里還引發(fā)另一個(gè)問題
我試圖在我的應(yīng)用程序啟動(dòng)時(shí)加載自定義log.properties文件。
我的屬性文件和我的主類在同一個(gè)包中,所以我認(rèn)為如下命令行參數(shù)應(yīng)該加載屬性文件。
-Djava.util.logging.config.file=log.properties
但是,只有當(dāng)我指定屬性文件的完整絕對(duì)路徑時(shí)才加載屬性。任何建議如何使用相對(duì)路徑?
Java日志記錄不會(huì)搜索整個(gè)硬盤中的文件;有很簡單的規(guī)則如何查找文件。你希望Java能夠看到這兩個(gè)文件是屬于彼此的,但是你沒有在任何地方說過。由于Java認(rèn)為屬性文件和類之間沒有連接,除非它們位于磁盤上的相同文件夾中,所以它找不到該文件。
-Djava.util.logging.config.file=log.properties
僅適用于文件log.properties位于Java進(jìn)程的當(dāng)前目錄中(可能非常隨機(jī))。所以你應(yīng)該在這里使用絕對(duì)路徑。
另一種解決方案是將文件logging.properties移動(dòng)到$JAVA_HOME/lib/(或編輯應(yīng)該在那里的文件)。在這種情況下,您不需要設(shè)置系統(tǒng)屬性。
util logging不從類路徑加載,它需要一個(gè)絕對(duì)路徑,這就是為什么其他日志包如log4j更容易配置,并且更適合web應(yīng)用程序,這對(duì)于獲取abs路徑是一件痛苦的事情。
這在java.util.logging.LogManager doco中根本沒有解釋。
Java JDK
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。