如下圖所示,運(yùn)行的jar 包和 log4j 的配置文件放在同一個(gè)目錄,jar包內(nèi)沒有任何配置文件:
在spring boot 框架的 main 方法中,通過下面這種方式,加載jar 包外的log4j配置文件
log4j配置文件的輸出位置如下:
運(yùn)行java -jar gf-mfile.jar 之后,控制臺(tái)有info 日志信息,如下圖:
系統(tǒng)也能夠創(chuàng)建 micofile.log 日志文件,但是,文件為null,0字節(jié),里面沒有任何數(shù)據(jù),info 日志信息沒有寫入文件中,如下圖:
問題:
如果,我將log4j配置文件 打到j(luò)ar 包中,如下圖:
執(zhí)行jar,日志文件是能夠生成日志信息的。我把log4j配置文件,放到和jar 包同級(jí)目錄就只創(chuàng)建日志文件不寫入日志信息了。
為什么配置文件放到j(luò)ar包外,日志文件中沒有內(nèi)容? 請(qǐng)大神賜教,感激不盡……
(本人推測(cè),是否是main方法中的讀取,log4j 配置文件的代碼有問題,如果是這樣應(yīng)該怎么修改?)
閉關(guān)修行中......
You also need to check your running parameters, especially the classpath parameter, whether the configuration file outside the package is included in the classpath.
1. Check the output level of LOG in the configuration file
2. Check the Logger code in the code to see if it matches the output level
According to the suggestion of the hero below (fabricated belief), the problem was finally solved successfully. Below, record the detailed steps to solve the problem:
First, find the MANIFEST.MF file in the jar package and find the Main-Class of the file Method:
Execute the following command:
java.exe -cp .;gf-mfile.jar org.springframework.boot.loader.JarLauncher
There is information in the log file.
=========Appendix, java -cp command analysis===============
java -cp .;c:dir1lib.jar Test
-cp is the same as -classpath. It is the path of other classes that the specified class depends on to run, usually a class library, jar package, etc. It needs the full path to the jar package, and the semicolon ";" on the window
Separated by semicolon ":" on Linux. Wildcards are not supported. All jar packages need to be listed, using a dot "." to represent the current path.
Although there are now IDEs such as eclipse, sometimes some programs are compiled and run manually. Many people, including people with many years of development experience, do not know how to parameterize running classes on the command line. A bit unreliable...
Usage example:
java -cp ..libhsqldb.jar org.hsqldb.Server -database mydb
or
java -cp ../lib/hsqldb.jar org.hsqldb.Server -database.0 mydb -dbname.0 mydb