SpringBoot 19: log management

Keywords: log4j Spring Apache SpringBoot

The default logging framework used by springboot is Logback.
Simple log properties can be configured in application.properties, logback.xml format can be configured separately, and log4j can be used for management.
The following describes the self-contained log configuration and how to use log4j as log management.

Own log management configuration

modify application.properties
#Configuration log
logging.level.root=INFO
logging.level.org.springframework.web=DEBUG
logging.level.org.hibernate=ERROR
#logging.path=E:\\log
logging.file=E:\\log\\springboot.log

This kind of simple, rough and convenient

Manage with log4j

pom.xml file import

Filter out the spring boot starter logging and add the spring boot starter log4j dependency package.

<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter</artifactId>
	<exclusions>
		<exclusion>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-logging</artifactId>
		</exclusion>
	</exclusions>
</dependency>
<dependency>
	<groupId>org.springframework.boot</groupId>
	<artifactId>spring-boot-starter-log4j</artifactId>
</dependency>

Create a new log4j.properties configuration file in the resources directory

Configure four output formats of info, error, console and debug.

log4j.rootLogger=info,error,CONSOLE,DEBUG
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender     
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout     
log4j.appender.CONSOLE.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n     
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender
log4j.appender.info.layout=org.apache.log4j.PatternLayout     
log4j.appender.info.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.info.datePattern='.'yyyy-MM-dd
log4j.appender.info.Threshold = info   
log4j.appender.info.append=true   
log4j.appender.info.File=E://logs/SpringBoot-Datasource/info/api_services_info.log
log4j.logger.error=error  
log4j.appender.error=org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.layout=org.apache.log4j.PatternLayout     
log4j.appender.error.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.error.datePattern='.'yyyy-MM-dd
log4j.appender.error.Threshold = error   
log4j.appender.error.append=true   
log4j.appender.error.File=E://logs/SpringBoot-Datasource/error/api_services_error.log
log4j.logger.DEBUG=DEBUG
log4j.appender.DEBUG=org.apache.log4j.DailyRollingFileAppender
log4j.appender.DEBUG.layout=org.apache.log4j.PatternLayout     
log4j.appender.DEBUG.layout.ConversionPattern=%d{yyyy-MM-dd-HH-mm} [%t] [%c] [%p] - %m%n  
log4j.appender.DEBUG.datePattern='.'yyyy-MM-dd
log4j.appender.DEBUG.Threshold = DEBUG   
log4j.appender.DEBUG.append=true   
log4j.appender.DEBUG.File=E://logs/SpringBoot-Datasource/debug/api_services_debug.log

Declare the log in the used class and output the log information

private static Logger log = Logger.getLogger(TestController.class);

@ResponseBody
@RequestMapping("/insertTest1")
public String insertTest1(String name, Integer age) {
	log.info("##################->name:" + name);
	userMapperTest1.insert(name, age);
	return "success";
}

Output results:

  2019-06-26-17-04 [http-nio-8080-exec-1] [cn.com.javakf.controller.TestController] [INFO] - ##################->name:zhangsan

Manage with log4j2

Original text: http://www.360doc.com/content/18/0628/10/339365_.shtml

pom.xml adds log4j2 and removes the default logging of spring boot

<dependencies>  
    <dependency>  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-web</artifactId>  
        <exclusions><!-- Remove default configuration -->  
            <exclusion>  
                <groupId>org.springframework.boot</groupId>  
                <artifactId>spring-boot-starter-logging</artifactId>  
            </exclusion>  
        </exclusions>  
    </dependency>  
    <dependency> <!-- Introduce log4j2 rely on -->  
        <groupId>org.springframework.boot</groupId>  
        <artifactId>spring-boot-starter-log4j2</artifactId>  
    </dependency>  
</dependencies> 

Add log4j2-spring.xml configuration file in src/main/resource, and the name must be log4j2-spring, so as to save configuration in application.yml.

<?xml version="1.0" encoding="UTF-8"?>
<!--Set up log4j2 Self log Level is warn-->
<!--Log level and priority sorting: OFF > FATAL > ERROR > WARN > INFO > DEBUG > TRACE > ALL -->
<!--Configuration Hinder status,This is used to set log4j2 The internal information output can not be set.
    When set up trace When you see log4j2 Various internal detailed output-->
<!--monitorInterval: Log4j It can automatically detect and modify the configuration file and reconfigure itself, and set the interval in seconds.-->
<configuration status="warn" monitorInterval="30">
    <!--Define all first appender-->
    <appenders>
        <!--Configuration of this output console-->
        <console name="Console" target="SYSTEM_OUT">
            <!--Format of output log-->
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
        </console>
        <!--The file will print out all the information, this log Every time the program is run, it will be automatically cleared by append Attribute determination, which is also very useful, suitable for temporary testing-->
        <File name="log" fileName="log/test.log" append="false">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} %-5level %class{36} %L %M - %msg%xEx%n"/>
        </File>
        <!-- This will print all info And below, each time the size exceeds size,
        Then this size Size logs are automatically saved by year-The folder created in the month is compressed as an archive.-->
        <RollingFile name="RollingFileInfo" fileName="${sys:user.home}/logs/hpaasvc/info.log"
                     filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)-->
                <ThresholdFilter level="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
                <ThresholdFilter level="WARN" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
        </RollingFile>
 
        <RollingFile name="RollingFileWarn" fileName="${sys:user.home}/logs/hpaasvc/warn.log"
                     filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">
            <Filters>
                <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
                <ThresholdFilter level="ERROR" onMatch="DENY" onMismatch="NEUTRAL"/>
            </Filters>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
            <!-- DefaultRolloverStrategy If the property is not set, the default value is at most 7 files in the same folder. Here, 20 files are set. -->
            <DefaultRolloverStrategy max="20"/>
        </RollingFile>
 
        <RollingFile name="RollingFileError" fileName="${sys:user.home}/logs/hpaasvc/error.log"
                     filePattern="${sys:user.home}/logs/hpaasvc/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">
            <ThresholdFilter level="ERROR"/>
            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>
            <Policies>
                <TimeBasedTriggeringPolicy/>
                <SizeBasedTriggeringPolicy size="100 MB"/>
            </Policies>
        </RollingFile>
 
    </appenders>
    <!--Then define logger,Only defined logger And introduced appender,appender It will take effect.-->
    <loggers>
        <!--Filter out spring and hibernate Some useless debug information-->
        <logger name="org.springframework" level="INFO">
        </logger>
        <logger name="org.mybatis" level="INFO">
        </logger>
        <root level="all">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
        </root>
    </loggers>
 
</configuration>

Posted by AdRock on Mon, 28 Oct 2019 07:14:07 -0700