我正在尝试在Maven项目中动态设置日志文件名。
我在博客文章之后尝试了很多次,但是未生成日志文件。
经过几次尝试和调试,我发现在数据库调用之前使用System.setProperty
设置日志文件名时,便会创建日志文件。
System.setProperty("logFileName", "E:\\FICO\\logs\\calculate");
但是我想使用从数据库获得的值来设置文件名。
例如:
String ccmValue = "E:\\FICO\\logs\\calculate_" + getCurrentCCMValue(); // DB Call to get the value
System.setProperty("logfilename", ccmValue ); // E:\\FICO\\logs\\calculate_ccmValue.log
如果在数据库调用之后设置了System.setProperty
,则不会创建文件。
下面是我的log4j.xml文件:
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration debug="true"
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="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<appender name="fileAppender"
class="org.apache.log4j.RollingFileAppender">
<param name="File" value="${logFileName}.log" />
<param name="maxFileSize" value="1MB" />
<param name="maxBackupIndex" value="5" />
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern"
value="%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n" />
</layout>
</appender>
<root>
<priority value="info"></priority>
<appender-ref ref="console"></appender-ref>
<appender-ref ref="fileAppender"></appender-ref>
</root>
</log4j:configuration>
这是控制台日志:
log4j: reset attribute= "false".
log4j: Threshold ="null".
log4j: Level value for root is [info].
log4j: root level set to INFO
log4j: Class name: [org.apache.log4j.ConsoleAppender]
log4j: Setting property [target] to [System.out].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: Adding appender named [console] to category [root].
log4j: Class name: [org.apache.log4j.RollingFileAppender]
log4j: Setting property [file] to [.log].
log4j: Setting property [maxFileSize] to [1MB].
log4j: Setting property [maxBackupIndex] to [5].
log4j: Parsing layout of class: "org.apache.log4j.PatternLayout"
log4j: Setting property [conversionPattern] to [%d{yyyy-MM-dd HH:mm:ss} %-5p %c{1}:%L - %m%n].
log4j: setFile called: .log, true
log4j: setFile ended
log4j: Adding appender named [fileAppender] to category [root].
2019-07-10 22:58:16 INFO Version:46 - HHH000412: Hibernate Core {5.4.3.Final}
2019-07-10 22:58:17 INFO Version:49 - HCANN000001: Hibernate Commons Annotations {5.1.0.Final}
2019-07-10 22:58:18 INFO ConnectionProviderInitiator:137 - HHH000130: Instantiating explicit connection provider: org.hibernate.connection.C3P0ConnectionProvider
2019-07-10 22:58:18 INFO C3P0ConnectionProvider:116 - HHH010002: C3P0 using driver: com.mysql.cj.jdbc.Driver at URL: jdbc:mysql://localhost:3306/gentexdba
2019-07-10 22:58:18 INFO C3P0ConnectionProvider:117 - HHH10001001: Connection properties: {user=root, password=****}
2019-07-10 22:58:18 INFO C3P0ConnectionProvider:120 - HHH10001003: Autocommit mode: false
2019-07-10 22:58:18 INFO MLog:126 - MLog clients using log4j logging.
2019-07-10 22:58:18 INFO MLog:126-使用log4j日志记录的MLog客户端。
2019-07-10 22:58:18 INFO C3P0Registry:248 - Initializing c3p0-0.9.5.3 [built 27-January-2019 00:11:37 -0800; debug? true; trace: 10]
2019-07-10 22:58:18 INFO C3P0ConnectionProvider:200 - HHH10001007: JDBC isolation level: <unknown>