如下图所示,运行的jar 包和 log4j 的配置文件放在同一个目录,jar包内没有任何配置文件:
在spring boot 框架的 main 方法中,通过下面这种方式,加载jar 包外的log4j配置文件
log4j配置文件的输出位置如下:
运行java -jar gf-mfile.jar 之后,控制台有info 日志信息,如下图:
系统也能够创建 micofile.log 日志文件,但是,文件为null,0字节,里面没有任何数据,info 日志信息没有写入文件中,如下图:
问题:
如果,我将log4j配置文件 打到jar 包中,如下图:
执行jar,日志文件是能够生成日志信息的。我把log4j配置文件,放到和jar 包同级目录就只创建日志文件不写入日志信息了。
为什么配置文件放到jar包外,日志文件中没有内容? 请大神赐教,感激不尽……
(本人推测,是否是main方法中的读取,log4j 配置文件的代码有问题,如果是这样应该怎么修改?)
黄舟2017-04-18 10:57:51
You also need to check your running parameters, especially the classpath parameter, whether the configuration file outside the package is included in the classpath.
ringa_lee2017-04-18 10:57:51
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
ringa_lee2017-04-18 10:57:51
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