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