Logback Set log file name programmatically
By:Roy.LiuLast updated:2019-08-17
In Logback, it is easy to set a log file name programmatically :
- In logback.xml, declares a variable like ${log.name}
- In Java, set the variable via System.setProperty("log.name", "abc")
1. Full example
1.1 A logback file, we will set the ${log.name} variable later.
src/main/resources/logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="PRO_HOME" value="/home/mkyong/ant/logs" /> <property name="USER_HOME" value="${PRO_HOME}" /> <timestamp key="bySecond" datePattern="yyyyMMdd.HHmmss" /> <appender name="FILE-ENGINE-ERROR" class="ch.qos.logback.core.FileAppender"> <file>${USER_HOME}/${log.name}.error</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n</pattern> </encoder> </appender> <appender name="FILE-ENGINE" class="ch.qos.logback.core.FileAppender"> <file>${USER_HOME}/${log.name}-${bySecond}.log</file> <encoder> <pattern>%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{35} - %msg%n</pattern> </encoder> </appender> <logger name="com.mkyong.core" level="debug" additivity="false"> <appender-ref ref="FILE-ENGINE" /> </logger> <logger name="org.springframework" level="error" additivity="false"> <appender-ref ref="FILE-ENGINE-ERROR" /> </logger> <root level="error"> <appender-ref ref="FILE-ENGINE-ERROR" /> </root> </configuration>
1.2 In Java, just set the file name via System.setProperty
AntRunApp.java
package com.mkyong.core; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AntRunApp { private final Logger logger = LoggerFactory.getLogger(AntRunApp.class); public static void main(String[] args) { //Set this before the logger start. System.setProperty("log.name", "mkyong"); AntRunApp obj = new AntRunApp(); obj.start(); private void start() { logger.debug("------ Starting Ant------"); //...
Output
Debug log file path /home/mkyong/ant/logs/mkyong-20150323.221959.log Error log file path /home/mkyong/ant/logs/mkyong.error
2. log.name.rir_IS_UNDEFINED.log
2.1 A common error, normally it is caused by the static logger. For example.
AntRunApp.java
package com.mkyong.core; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AntRunApp { // static, this logger will be initialized before the program is run. // All logs will be redirected to log.name.rir_IS_UNDEFINED.log private static final Logger logger = LoggerFactory.getLogger(AntRunApp.class); private void start() { System.setProperty("log.name", "mkyong"); logger.debug("------ Starting Ant------"); //...
To fix it, just delete the static type.
2.2 If you logs before the System.setProperty, this will also cause the common Logback variable UNDEFINED error.
AntRunApp.java
package com.mkyong.core; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class AntRunApp { private final Logger logger = LoggerFactory.getLogger(AntRunApp.class); private void start() { //Please set the log name first! logger.debug("------ Starting Ant------"); System.setProperty("log.name", "mkyong");
References
From:一号门
Previous:Gradle and JUnit example
COMMENTS