[kotcloud] kotlin + springboot (3) lo4j2 + slf4j configuration log, multi-environment log

Overview

  • log4j2 supports synchronous log and asynchronous log, the performance is better than log4j and logback.
  • log4j2 supports configuration methods: xml , json , yaml, properties

Automatically discover configuration files (Automatic Configuration), the directory is under the classpath: maven directory (src -> main -> resources), file discovery order:

log4j2-test.properties 
 log4j2-test.yaml
 log4j2-test.yml
 log4j2-test.json
 log4j2-test.jsn
 log4j2-test.xml
 log4j2.properties
 log4j2.yaml
 log4j2.yml
 log4j2.json
 log4j2.jsn
 log4j2.xml

If there is no configuration file, log4j2 default configuration:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
  <Appenders>
    <Console name="Console" target="SYSTEM_OUT">
      <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
    </Console>
  </Appenders>
  <Loggers>
    <Root level="error">
      <AppenderRef ref="Console"/>
    </Root>
  </Loggers>
</Configuration>
  • Appenders configuration reference:
  1. Log4j2 official document translation, study notes 2 - Appender classification and examples of common types
  2. Introduction to common Appenders of log4j2

The best documentation is still on the official website

使用 log4j2.yml

Configuration:
  status: WARN
  Appenders:
  #配置控制台输出的格式
    Console:
      name: Console
      target: SYSTEM_OUT
      PatternLayout:
        #%d日期 %t线程 %-5level五个字符的日志输出级别 %logger日志发生的位置 %m日志内容 %n换行
        pattern: %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n

  Loggers:
    Root:
      #总输出级别 error  , 输出到控制台
      level: ERROR
      AppenderRef:
        ref: Console
    Logger:
      #org.kotcloud路径下的日志输出级别为DEBUG, additivity(默认true)=false不重复输出
      name: org.kotcloud
      level: DEBUG
      additivity: false

Do not configure DTD files

configure pom.xml

       <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <!-- 排队默认日志 -->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-starter-logging</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
        <!-- 引入log4j2,会引入slf4j -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>
        <!-- web项目 , 引入log4j-web -->
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-web</artifactId>
            <version>${log4j2.version}</version>
        </dependency>
        <!-- 使用log4j2.yml格式的配置文件,必须加这个依赖,用于识别.yml -->
        <dependency>
            <groupId>com.fasterxml.jackson.dataformat</groupId>
            <artifactId>jackson-dataformat-yaml</artifactId>
        </dependency>

Using Log4j 2 in a Web Application You have to be very careful when using Log4j or any other logging framework in a Java EE web application. When the container shuts down or the web app is undeployed, it's important to do proper cleanup of logs > resources (database connections closed, files closed, etc.). Due to the nature of class loaders in web applications, Log4j resources cannot be cleaned up in the normal way. Log4j must be "started" when the web application is deployed, and "closed" when the web application is not in use. How it works depends on whether your application is a Servlet 3.0 or newer or a Servlet 2.5 web application.

In either case, you will need to add the log4j-web module to your deployment as detailed in the Maven, Ivy and Gradle Artifacts man pages.

To avoid problems, the Log4j shutdown hook will be automatically disabled when the log4j-web jar is included.

Use log4j2, slf4j

@RestController
open class IndexController{

    val log : Logger = LoggerFactory.getLogger(IndexController::class.java)

    @RequestMapping("/index")
    fun index(id:String="") : String{
        log.info("id:{}",id)
        return id
    }
}

After completion, start the service and request the interface:http://localhost:8080/index?id=123456

Output :

2018-04-09 12:33:34.640 [http-nio-80-exec-3] INFO  org.kotcloud.controller.IndexController - id:123456

Log configuration successful!

Multi-environment configuration log:

springBoot can use multi-environment property configuration files, then specify the log file in the multi-environment property configuration file to distinguish the environment.

Add the following files:

  • log4j2-dev.yml
  • log4j2-test.yml
  • log4j2-pro.yml

Configure the log application-dev.yml for different environments in the property configuration file (application-{profile}.yml)

logging:
  config: classpath:log4j2-dev.yml

log4j2-dev.yml , if the development environment wants to output more logs, the log level can be adjusted to the lowest possible level and only output to the console

Configuration:
  status: WARN
  Appenders:
  #配置控制台输出的格式
    Console:
      name: Console
      target: SYSTEM_OUT
      PatternLayout:
        #%d日期 %t线程 %5p五个字符的日志输出级别 %l日志发生的位置 %m日志内容 %n换行
        pattern: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%5p %t %l - %m%n"

  Loggers:
    Root:
      #总输出级别 DEBUG, 输出到控制台
      level: DEBUG
      AppenderRef:
        ref: Console
    Logger:
      #org.kotcloud路径下的日志输出级别为trace, additivity(默认true)=false不重复输出
      name: org.kotcloud
      level: TRACE
      additivity: false
      AppenderRef:
        ref: Console

log4j2-test.yml , test environment logs, output logs to files, do not split logs.

Configuration:
  status: WARN
  Properties:
    Property:
      name: pattern
      #%d日期 %t线程 %5p五个字符的日志输出级别 %l日志发生的位置 %m日志内容 %n换行
      value: "%d{yyyy-MM-dd HH:mm:ss,SSS}:%5p %t %l - %m%n"
  Appenders:
    #配置控制台输出的格式
    File:
        #输出到项目根路径下的 logs/out.log文件
      name: fileLog 
      fileName: logs/out.log
      PatternLayout:
        pattern: ${pattern}

  Loggers:
    Root:
      #总输出级别 error  , 输出到控制台
      level: ERROR
      AppenderRef:
        ref: fileLog
    Logger:
      #org.kotcloud路径下的日志输出级别为trace, additivity(默认true)=false不重复输出
      name: org.kotcloud
      level: DEBUG
      additivity: false
      AppenderRef:
        ref: fileLog


log4j2-pro.yml The production log is output to a file, and the log volume reaches a certain amount of split logs


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325077475&siteId=291194637