logback.xml Example

Here are a few logback.xml examples that are used in my projects, just for sharing.

1. Send logs to Console

All logging will be redirected to console.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>
				%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
			</Pattern>
		</layout>
	</appender>

	<logger name="com.mkyong.web" level="debug"
		additivity="false">
		<appender-ref ref="STDOUT" />
	</logger>

	<root level="error">
		<appender-ref ref="STDOUT" />
	</root>

</configuration>

2. Send logs to File

All logging will be redirected to a file c:/logs/debug.log. Furthermore, this log file will be archived daily or the file size is larger than 10MB.

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.web" level="debug"
		additivity="false">
		<appender-ref ref="FILE-AUDIT" />
	</logger>

	<root level="error">
		<appender-ref ref="FILE-AUDIT" />
	</root>

</configuration>

3. Send logs to Console and File

A final example, combine both and add one more “file error” appender to log error messages only.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<configuration>

	<property name="DEV_HOME" value="c:/logs" />

	<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
		<layout class="ch.qos.logback.classic.PatternLayout">
			<Pattern>
				%d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
			</Pattern>
		</layout>
	</appender>
	
	<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>

	<appender name="FILE-ERROR"
		class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>${DEV_HOME}/error.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/error.%d{yyyy-MM-dd}.%i.log
                        </fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy
				class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>10MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
		</rollingPolicy>

	</appender>
	
	<!-- Send logs to both console and file audit -->
	<logger name="com.mkyong.web" level="debug"
		additivity="false">
		<appender-ref ref="FILE-AUDIT" />
		<appender-ref ref="STDOUT" />
	</logger>

	<root level="error">
		<appender-ref ref="FILE-ERROR" />
	</root>

</configuration>
Note
To know more, please refer to this official Logback Appenders.

4. SiftingAppender example

Send logs to a separate log file, the log file name is defined at runtime, via MDC.

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<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.

5. Set log file name programmatically

Set the log file name ${log.name} programmatically, via System.setProperty

logback.xml

<?xml version="1.0" encoding="UTF-8"?>
<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

References

  1. Logback appenders
  2. log4j.properties examples

About the Author

author image
mkyong
Founder of Mkyong.com, love Java and open source stuff. Follow him on Twitter, or befriend him on Facebook or Google Plus. If you like my tutorials, consider make a donation to these charities.

Comments

Leave a Reply

avatar
newest oldest most voted
Nitesh Jain
Guest
Nitesh Jain
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
Russell Bateman
Guest
Russell Bateman

As always, my friend, your posts are super valuable, to the point, easy to understand, accurate and usually helpful. Thanks!

Aman Kachhal
Guest
Aman Kachhal

Can we use logback for windows event logging also ? if so, then how?

Azhar
Guest
Azhar

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.

PETO
Guest
PETO

why [IntelliJ IDEA]’s [inspect code] shows “namespace is not found” for “configuration”

Dishant
Guest
Dishant

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.

firstpostcommenter
Guest
firstpostcommenter

What do the below statements mean:

mukthar ahmed
Guest
mukthar ahmed

@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…

Binh Thanh Nguyen
Guest
Binh Thanh Nguyen

Thanks, nice post

Umarani Pandian
Guest
Umarani Pandian
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
Deepak
Guest
Deepak

If you have got the solution for this problem, please let me know.

Srd
Guest
Srd

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

Carlos Daniel Cañón Carrero
Guest
Carlos Daniel Cañón Carrero

Hi, can you solve this?
I need the same thing… xD
please answer to me, thanks.

CodeWarriro
Guest
CodeWarriro

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?

CodeWarrior
Guest
CodeWarrior

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

Rahul Kesharwani
Guest
Rahul Kesharwani

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

mkyong
Guest
mkyong

Normally, I will attach the FILE-ERROR at root level, so that it will logs any errors.

David L
Guest
David L

Hi Mkyong, is there a way to send the logs over to another (remote) syslog server?
David

Umarani Pandian
Guest
Umarani Pandian

Yes, You can use SocketAppender or SyslogAppender

Neha
Guest
Neha

Dates are not printing through logback.xml only time is printing? How to fixed it ?

mkyong
Guest
mkyong
trackback
Spring MVC + Logback SLF4j example

[…] other appenders (log output), like logs to a file, please visit this log.xml examples, or this logback appender […]

Elango
Guest
Elango

Hi Mkyong Is there any way available to delete the archieved files?

Nimrod007
Guest
Nimrod007

yes look for maxHistory

mkyong
Guest
mkyong

http://logback.qos.ch/manual/appenders.html

Find maxHistory.

P.S Thanks Nimrod007

anuo
Guest
anuo

very good