logback.xml Example
By:Roy.LiuLast updated:2019-08-17
Here are a few logback.xml examples that are used in my projects, just for sharing.
P.S Tested with Logback 1.2.3
1. Send logs to Console
All logging will be redirected to console.
logback.xml
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <logger name="com.mkyong" level="debug" additivity="false"> <appender-ref ref="CONSOLE"/> </logger> <root level="error"> <appender-ref ref="CONSOLE"/> </root> </configuration>
2. Send logs to File + Rotate File
2.1 All logging will be redirected to a file logs/app.log. Furthermore, this log file will be archived daily or when the file size is larger than 10MB.
logback.xml
<configuration> <property name="HOME_LOG" value="logs/app.log"/> <appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${HOME_LOG}</file> <rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy"> <fileNamePattern>logs/archived/app.%d{yyyy-MM-dd}.%i.log.gz</fileNamePattern> <!-- each archived file, size max 10MB --> <maxFileSize>10MB</maxFileSize> <!-- total size of all archive files, if total size > 20GB, it will delete old archived file --> <totalSizeCap>20GB</totalSizeCap> <!-- 60 days to keep --> <maxHistory>60</maxHistory> </rollingPolicy> <encoder> <pattern>%d %p %c{1.} [%t] %m%n</pattern> </encoder> </appender> <logger name="com.mkyong" level="debug" additivity="false"> <appender-ref ref="FILE-ROLLING"/> </logger> <root level="error"> <appender-ref ref="FILE-ROLLING"/> </root> </configuration>
2.2 Below SizeAndTimeBasedFNATP example is deprecated, it may still work, but it is better to use the above new SizeAndTimeBasedRollingPolicy example.
logback.xml
<?xml version="1.0" encoding="UTF-8"?> <configuration> <property name="DEV_HOME" value="c:/logs" /> <appender name="FILE-AUDIT" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${DEV_HOME}/debug.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} - %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy"> <!-- rollover daily --> <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log</fileNamePattern> <timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP"> <maxFileSize>10MB</maxFileSize> </timeBasedFileNamingAndTriggeringPolicy> </rollingPolicy> </appender> <logger name="com.mkyong" level="debug" additivity="false"> <appender-ref ref="FILE-AUDIT" /> </logger> <root level="error"> <appender-ref ref="FILE-AUDIT" /> </root> </configuration>
3. Send error logs to email
logback.xml
<configuration> <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender"> <layout class="ch.qos.logback.classic.PatternLayout"> <Pattern> %d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n </Pattern> </layout> </appender> <appender name="EMAIL" class="ch.qos.logback.classic.net.SMTPAppender"> <smtpHost>smtp.mailgun.org</smtpHost> <smtpPort>25</smtpPort> <username>123</username> <password>123</password> <to>TO_EMAIL</to> <to>RO_ANOTHER_EMAIL</to> <from>FROM_EMAIL</from> <subject>TESTING: %logger{20} - %m</subject> <layout class="ch.qos.logback.classic.html.HTMLLayout"/> </appender> <logger name="com.mkyong" level="error" additivity="false"> <appender-ref ref="EMAIL"/> </logger> <root level="error"> <appender-ref ref="CONSOLE"/> </root> </configuration>
To send email, we need this javax.mail
pom.xml
<dependency> <groupId>com.sun.mail</groupId> <artifactId>javax.mail</artifactId> <version>1.6.2</version> </dependency>
5. Logs Asynchronously
This make logging faster.
logback.xml
<configuration> <appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender"> <!-- //... --> </appender> <appender name="ASYNC" class="ch.qos.logback.classic.AsyncAppender"> <appender-ref ref="FILE-ROLLING" /> </appender> <root level="debug"> <appender-ref ref="ASYNC"/> </root> </configuration>
6. SiftingAppender
Send logs to a separate log file, the log file name is defined at runtime, via MDC.
logback.xml
<configuration> <property name="DEV_HOME" value="c:/logs" /> <appender name="FILE-THREAD" class="ch.qos.logback.classic.sift.SiftingAppender"> <discriminator> <key>logFileName</key> <defaultValue>head0</defaultValue> </discriminator> <sift> <appender name="FILE-${logFileName}" class="ch.qos.logback.core.rolling.RollingFileAppender"> <file>${DEV_HOME}/${logFileName}.log</file> <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder"> <Pattern> %d{yyyy-MM-dd HH:mm:ss} [%thread] %level %logger{35} - %msg%n </Pattern> </encoder> <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy"> <FileNamePattern>${DEV_HOME}/${logFileName}.%i.log.zip</FileNamePattern> <MinIndex>1</MinIndex> <MaxIndex>10</MaxIndex> </rollingPolicy> <triggeringPolicy class="ch.qos.logback.core.rolling.SizeBasedTriggeringPolicy"> <MaxFileSize>10MB</MaxFileSize> </triggeringPolicy> </appender> </sift> </appender> <logger name="com.mkyong.web" level="debug" additivity="false"> <appender-ref ref="FILE-THREAD" /> </logger> <root level="error"> <appender-ref ref="FILE-THREAD" /> </root> </configuration>
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.slf4j.MDC; //... public void run() { MDC.put('logFileName', "head1"); logger.debug("hello"); MDC.remove('logFileName');
Note
Please refer to this complete Logback SiftingAppender example
Please refer to this complete Logback SiftingAppender example
7. Set log file name programmatically
Set the log file name ${log.name} programmatically, via System.setProperty
logback.xml
<configuration> <property name="USER_HOME" value="/home/mkyong/ant/logs" /> <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}.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> <root level="error"> <appender-ref ref="FILE-ENGINE-ERROR" /> </root> </configuration>
TestRunApp.java
package com.mkyong.core; import org.slf4j.Logger; import org.slf4j.LoggerFactory; public class TestRunApp { //No static, else log.name.IS_UNDEFINED.log private final Logger logger = LoggerFactory.getLogger(TestRunApp.class); public static void main(String[] args) { //Set this before the logger start. System.setProperty("log.name", "abcdefg"); TestRunApp obj = new TestRunApp(); obj.start(); private void start() { logger.debug("------ Starting Ant------"); //...
Output
Debug log file path /home/mkyong/ant/logs/abcdefg.log Error log file path /home/mkyong/ant/logs/abcdefg.error
Note
Please refer to this complete example Logback – Set log file name programmatically
Please refer to this complete example Logback – Set log file name programmatically
From:一号门
COMMENTS