Log overview of springboot

Log overview of springboot

Level control:

  • TRACE<DEBUG<INFO<WARN<ERROR<FATAL
  • The default springboot is set to info, and only info and information larger than info (error and warn) will be output in the console
  • If you start it in the debug mode, the debug information will also be output to the console, but the level of the log remains unchanged
  • Set the log level:
    • The format is' logging level.* = LEVEL’
    • LEVEL: options TRACE, DEBUG, INFO, WARN, ERROR, FATAL, OFF
    • The specific can be set to logging level. com.qp = debug stands for com All classes under the QP package are output at the debug level -- that is, all classes except trace are output to the console

Control of output position:

  • Under normal circumstances, it will only output in the console and will not be written to log files. If you want to write log files other than console output, you need to set logging File or logging Path attribute

  • logging.file, setting file, which can be an absolute path or a relative path. For example: logging file=my. log

  • logging.path, set the directory, and spring. Net will be created in this directory Log file and write the log contents, such as logging path=/var/log

  • If you only configure logging File, and a XXX will be generated under the current path of the project Log log file.

    If you only configure logging Path, generate a log file in the / var/log folder as spring log

  • The two cannot be used at the same time. If they are used at the same time, only logging File effective

Custom log configuration

----I can't use it for the time being------

Since the logging service is generally initialized before the ApplicationContext is created, it does not have to be controlled through the Spring configuration file. Therefore, log control and management can still be well supported through system properties and traditional Spring Boot external configuration files.

Spring boot officials recommend preferentially using the file name with - spring as your log configuration (for example, use logback-spring.xml instead of logback.xml), and name it logback-spring.xml XML log configuration file. Spring boot can add some spring boot specific configuration items to it (mentioned below).

The above is the default naming rule and can be placed under src/main/resources.

If you want to fully control the log configuration, but you don't want to use Logback XML is used as the name of Logback configuration, which can be accessed through logging. XML The config attribute specifies the custom name: logging config=classpath:logging-config. xml

Log frame

  • Differences between log4j and slf4j:

SLF4J - Simple Logging Facade For Java, which is a unified Facade abstraction for various Java logging frameworks. There are many Java logging frameworks -- Java. Net is commonly used util. Logging, log4j, logback, commons logging. The Jakarta Commons Logging API (JCL) is used by the spring framework. SLF4J defines a unified log abstraction interface, and the real log implementation is determined at runtime - it provides binding of various log frameworks.

So it's:

Log4j logback commons logging is the framework of logging

There is another layer under the logging framework, which is the interface to call the framework. All framework logging interfaces are slf4j

  • Introduction to the framework:

The default log framework is logback, and it is emphasized here that the logback framework is a strong framework, which is much easier to use than the Log4j framework. Don't think about changing to Log4j.

  • In a normal project, if you want to use log, you can directly add an annotation @Slf4j (based on lombok), and then you can directly use log in the project Info (xxxxx).
  • The principle of this is: the default log framework logback exists in normal projects. We use @slf4j to call the log framework Info method

Specific operations using the logback framework

  • Custom log profiles: finding profiles
    • Try to find the file logback test under classpath xml;
    • If the file does not exist, look for the file logback xml;
    • If both files do not exist, logback automatically configures itself with the basic configurator, which will cause the record to be output to the console.
  • Create a logback under the resource of main xml

  • I thought about the specific grammar in it. CV can solve it.
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
	<!--Output on console-->
	<appender name="stdout" class="ch.qos.logback.core.ConsoleAppender">
		<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
			<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
				<expression>return logger.contains("nacos");</expression>
			</evaluator>
			<OnMatch>DENY</OnMatch>
			<OnMismatch>ACCEPT</OnMismatch>
		</filter>
		<encoder>
			<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
			</pattern>
		</encoder>
	</appender>

	<!--Save all log information-->
	<appender name="rollingFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>./logs/server.log</file>
		<filter class="ch.qos.logback.core.filter.EvaluatorFilter">
			<evaluator class="ch.qos.logback.classic.boolex.JaninoEventEvaluator">
				<expression>return logger.contains("nacos");</expression>
			</evaluator>
			<OnMatch>DENY</OnMatch>
			<OnMismatch>ACCEPT</OnMismatch>
		</filter>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<fileNamePattern>./logs/server.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>30MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
			<maxHistory>10</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
			</pattern>
		</encoder>
	</appender>

	<!--preservation WARN Log of-->
	<appender name="warnFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>./logs/server_warn.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- Archive once a day -->
			<fileNamePattern>./logs/server_warn.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>20MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
			<maxHistory>10</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
			</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>WARN</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>

	<!--preservation error Log of-->
	<appender name="errorFile" class="ch.qos.logback.core.rolling.RollingFileAppender">
		<file>./logs/server_error.log</file>
		<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
			<!-- Archive once a day -->
			<fileNamePattern>./logs/server_error.%d{yyyy-MM-dd}.%i.log</fileNamePattern>
			<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
				<maxFileSize>20MB</maxFileSize>
			</timeBasedFileNamingAndTriggeringPolicy>
			<maxHistory>10</maxHistory>
		</rollingPolicy>
		<encoder>
			<pattern>%date{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
			</pattern>
		</encoder>
		<filter class="ch.qos.logback.classic.filter.LevelFilter">
			<level>ERROR</level>
			<onMatch>ACCEPT</onMatch>
			<onMismatch>DENY</onMismatch>
		</filter>
	</appender>


	<appender name="async-stdout" class="ch.qos.logback.classic.AsyncAppender">
		<appender-ref ref="stdout"/>
		<includeCallerData>true</includeCallerData>
	</appender>
	<appender name="async-rollingFile" class="ch.qos.logback.classic.AsyncAppender">
		<appender-ref ref="rollingFile"/>
		<includeCallerData>true</includeCallerData>
	</appender>
	<appender name="async-warnFile" class="ch.qos.logback.classic.AsyncAppender">
		<appender-ref ref="warnFile"/>
		<includeCallerData>true</includeCallerData>
	</appender>
	<appender name="async-errorFile" class="ch.qos.logback.classic.AsyncAppender">
		<appender-ref ref="errorFile"/>
		<includeCallerData>true</includeCallerData>
	</appender>

	<logger name="org.quartz.impl.jdbcjobstore.JobStoreTX" level="INFO"/>
	<logger name="org.quartz.impl.jdbcjobstore.StdRowLockSemaphore" level="INFO"/>
	<logger name="org.springframework.amqp.rabbit.core.RabbitTemplate" level="INFO"/>
	<logger name="org.springframework.amqp.rabbit.listener.BlockingQueueConsumer" level="INFO"/>

	<root level="INFO">
		<appender-ref ref="async-stdout"/>
		<appender-ref ref="async-rollingFile"/>
		<appender-ref ref="async-warnFile"/>
		<appender-ref ref="async-errorFile"/>
	</root>
</configuration>
  • Finally, these logs will be generated

Keywords: log4j Spring Boot Logback

Added by goldages05 on Mon, 24 Jan 2022 02:25:53 +0200