logback.xml Example
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
Hi Mkyong,
I want to log a json response in my log and I want to mask certain values as they contain sensitive data. I tried doing the below but it didnt not work. It masks my json response which contains the text last_name but I would like to mask its corresponding value.
E.g. last_name: “Hello” is masked as **********: “Hello”
But i need something like this last_name: “Hello” to last_name: **********
timestamp
version
{
“message”: “%replace(%msg){‘last_name’,’**********’}”
}
20
1000
30
true
excluded
%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} – %replace(%msg){‘(password)=.*?(%amp;|$)’, ‘$1=XXX$2’}%n
As always, my friend, your posts are super valuable, to the point, easy to understand, accurate and usually helpful. Thanks!
Can we use logback for windows event logging also ? if so, then how?
Dates are not printing through logback.xml only time is printing? How to fixed it ?
<pattern>%d %p %c{1.} [%t] %m%n</pattern>
can anyone tell what is the meaning of this c{1.} when I upgraded the spring version its giving error for unable to parse
HI my issue is fileappender is not able to create a file in linux enviroment
i have used all kind of stuffs like
Relative path as well as actual path dont know where are i am going wrong
I want to print request attributes in log. Like any HTTP Server request object.
유용하게 쓰게 되었습니다. 감사드립니다.
Hello there! How should i configure a logback file if i want to send my log files to different host?
When I try “6. SiftingAppender” , Logs are logged to the the filename and head0 log. Is there a way to avoid duplicate logging?
Thank You, very good explanation
Where should the file be located and how do you link logback to the config file?
you should put logback.xml file in src/main/resources folder if your project is in maven. code will automatically pick the file from that location
very good
Hi Mkyong,
I read your blogs they are illustrative simple and straight to point.
I would like to achieve Thread based logs.
public static void doSomething(int productid)
{
Thread.currentThread().setName(“Log-“+productid);
Log.info(” I am log for “+productid);
doJob1(productid);
}
public static doJob1(int productid)
{
//do some thing here and write logs into the corresponding log only
}
if i have 2 products 1001,1002 it should create log-1001.log, log-1002.log. (products are dynamic we can add them in file)
It should be thread safe also. should not mix with other logs.
if you have any idea let me know.
why [IntelliJ IDEA]’s [inspect code] shows “namespace is not found” for “configuration”
Could you somehow provide an example to create logs as JSON by using a JsonEncoder? We are working on a cloud application and have this as a requirement.
In short. we are creating console logs for Developement profile. Where as JSON logs for cloud profile.
What do the below statements mean:
@mkyong:disqus – can you please help me with the below explained scenario.
project # 1) I have a java project enabled with both console and file logging and works super file, logging all events to log file.
project # 2) Another similar project enabled with logging
3) (This is where I need help) – When project # 1 is including in proj#2’s pom as dependencies and some of the classess-methods from proj#1 are being called, I am unable to collect their messages. As in, I am unable to see the called methods log (debug/error) messages…
Thanks, nice post
Hi Mkyong, First of all thank you for this great tutorial.
I’m trying to use SocketAppender in my Android app and everything is working good except that, I get _jsonparsefailure in Kibana. I’m adding json of my log that I see. I’m seeing unicode characters. Any idea how to fix that?
Thanks in advance!
{
“_index”: “logstash-2016.10.11”,
“_type”: “logs”,
“_id”: “AVe1qKsfSNbnwrFakdJ9”,
“_score”: 1,
“_source”: {
“message”: “loadFactorxp?@u0000u0000wbu0000u0000u0000u0004u0000u0000u0000u0001tu0000bHOSTNAMEtu0000tlocalhostxtu00001com.sample.mobile.activity.LaunchActivitypsqu0000~u0000u000b?@u0000u0000wbu0000u0000u0000u0002u0000u0000u0000u0000xtu0000c{source=peo-mobile, message=umtay, node=mobile, position=4132, mac=02:00:00:00:00:00}”,
“tags”: [
“_jsonparsefailure”
],
“@version”: “1”,
“@timestamp”: “2016-10-11T21:31:51.788Z”,
“host”: “172.14.113.119”
},
“fields”: {
“@timestamp”: [
1476221511788
]
}
}
My logback.xml:
<!–true–>
192.168.1.217
5228
10000
If you have got the solution for this problem, please let me know.
Hi Mkyong,
Im trying to create log file for every 5 minutes and max history of 10 files, how can i achieve this?
Since %d{yyyyMMdd-HH-mm} pattern rolls log file every minute, i have overloaded rollover method by extending TimeBasedRollingPolicy class. I could roll log for every 5 minutes, but maxHistory is not working in this case.
Thanks in advance
Hi, can you solve this?
I need the same thing… xD
please answer to me, thanks.
I am not seeing any output to the error file, and aside from logger name, I am using your configuration. Any idea why the error logger is creating the error file but not adding the error content to it?
Normally, I will attach the FILE-ERROR at root level, so that it will logs any errors.
Okay, I fixed the ‘Send logs to Console and Files’. First, add the following filter to the FILE-ERROR appender.
ERROR
ACCEPT
DENY
Then just add the FILE-ERROR appender to the logger
For me it was sending all level of logs into the error.log file.. then it tweak the filter class to “ch.qos.logback.classic.filter.ThresholdFilter” and then it worked fine
Hi Mkyong, is there a way to send the logs over to another (remote) syslog server?
David
Yes, You can use SocketAppender or SyslogAppender
Hi Mkyong Is there any way available to delete the archieved files?
http://logback.qos.ch/manual/appenders.html
Find maxHistory.
P.S Thanks Nimrod007
yes look for maxHistory