Detailed description of common logback.xml configuration files

Keywords: xml log4j encoding

Reasons for choosing logback

A simple comparison between logback and log4j:
1. First, logback is faster to use for the same code path.
2.logback implements the api of log4j natively, and a transformation layer is needed in the middle of log4j.
3. Richer documentation to support the configuration of xml and group.
4. Configuration files can be hot-loaded during project startup.
5. Logs can be automatically archived, logs can be archived, and logs can be automatically compressed into archive files.
6. Support more filters and parametric output.

operation

1. Introducing dependency xml

		<!-- logback -->
				    <dependency>
				      <groupId>ch.qos.logback</groupId>
				      <artifactId>logback-core</artifactId>
				      <version>1.1.8</version>
				    </dependency>
				    <dependency>
				      <groupId>ch.qos.logback</groupId>
				      <artifactId>logback-classic</artifactId>
				      <version>1.1.8</version>
				    </dependency>
				    <dependency>
				      <groupId>org.slf4j</groupId>
				      <artifactId>slf4j-api</artifactId>
				      <version>1.7.22</version>
				    </dependency>

2. Create logback.xml

	<?xml version="1.0" encoding="UTF-8"?>
	<configuration scan="true" scanPeriod="60 seconds">
	
	    <!--Log input to console-->
	    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
	        <encoder>
	            <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
	        </encoder>
	    </appender>
	
	    <!--<appender name="permission" class="ch.qos.logback.core.rolling.RollingFileAppender">-->
	    <!--<file>${catalina.home}/logs/permission.log</file>-->
	    <!--<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">-->
	    <!--<FileNamePattern>${catalina.home}/logs/permission.%d{yyyy-MM-dd}.log.gz</FileNamePattern>-->
	    <!--</rollingPolicy>-->
	    <!--<layout class="ch.qos.logback.classic.PatternLayout">-->
	    <!--<pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>-->
	    <!--</layout>-->
	    <!--</appender>-->
	    <!---->
	    <!--<logger name="xxx" level="INFO">-->
	    <!--<appender-ref ref="permission"/>-->
	    <!--</logger>-->
	
	    <!-- TRACE < DEBUG < INFO < WARN < ERROR -->
	    <root level="INFO">
	        <appender-ref ref="STDOUT" />
	    </root>
	
	</configuration>

Note:
Parameters are defined in configuration: scan, scanPeriod, etc.
1.scan="true" configuration file will be reloaded when it changes.
2.scanPeriod sets the time interval, which will take effect when scan="true".
For example: scanPeriod="60 seconds"

3.appender: Log output form, you can define more than one.
STDOURT: Standardized output class="ch.qos.logback.core.ConsoleAppender": console output
Standard log format output:

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

Interpretation:
% d {yyyyy-MM-dd HH: mm: ss. SSS}: time
[% thread]: Process
%- 5level: Level from do to display 5 widths
% logger: log name
% msg%: specific MSG

Custom Name and Path Log:

		 <appender name="permission" class="ch.qos.logback.core.rolling.RollingFileAppender">
	    <file>${catalina.home}/logs/permission.log</file>
	    <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
	    <FileNamePattern>${catalina.home}/logs/permission.%d{yyyy-MM-dd}.log.gz</FileNamePattern>
	    </rollingPolicy>
	    <layout class="ch.qos.logback.classic.PatternLayout">
	    <pattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger - %msg%n</pattern>
	    </layout>
	    </appender>

Interpretation:

  1. <file>${catalina.home}/logs/permission.log</file> represents the path and prints the log permission.log under the logs folder under the Catalina.home folder.
    2. The others are similar to those above.

4.logger: Logs can be class names, or other names, and finally return appender output.
5.root: There can only be one, log level: sort from small to large: TRACE < DEBUG < INFO < WARN < ERROR

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

Connect with appender

This is what I met in the project, so as a note, if it is useful, you can see that it is useless.

Posted by shortj75 on Tue, 10 Sep 2019 05:20:06 -0700