spring boot2 using log4j2

spring boot uses logback by default. I've seen many places saying that logback consumes more performance than log4j. I haven't tried it, but I still use log4j more personally.

 

Look at pom dependency first

<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>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-log4j2</artifactId>
        </dependency>

 

spring boot log provides three kinds of log implementation Java Util LoggingLog4J2 Sum Logback

To select one of these, you need to set the system property org.springframework.boot.logging.LoggingSystem. At first, I set it directly in the startup class

 System.setProperty("org.springframework.boot.logging.LoggingSystem","Log4J2");

However, the discovery was not. Later, I looked at the code and found these lines

 

Try it. The real setting is like this

 System.setProperty("org.springframework.boot.logging.LoggingSystem","org.springframework.boot.logging.log4j2.Log4J2LoggingSystem");

Using different log implementations, the system will load the following files by default

Logback

logback-spring.xml, logback-spring.groovy, logback.xml, or logback.groovy

Log4j2

log4j2-spring.xml or log4j2.xml

JDK (Java Util Logging)

logging.properties

 

So if you need to customize the log configuration at this time, you can create log4j2.xml under resource

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN">
    <Properties>
        <Property name="PID">????</Property>
        <Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
        <Property name="LOG_LEVEL_PATTERN">%5p</Property>
        <Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
        <Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
        <Property name="FILE_LOG_PATTERN">%d{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} ${sys:PID} --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
    </Properties>
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.apache.catalina.startup.DigesterFactory" level="error" />
        <Logger name="org.apache.catalina.util.LifecycleBase" level="error" />
        <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
        <logger name="org.apache.sshd.common.util.SecurityUtils" level="warn"/>
        <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
        <Logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="error" />
        <Logger name="org.hibernate.validator.internal.util.Version" level="warn" />
        <logger name="org.springframework.boot.actuate.endpoint.jmx" level="warn"/>
        <Root level="info">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

This is the official default configuration. Here is my own modified configuration

 

<?xml version="1.0" encoding="UTF-8"?>

<!--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 of itself can not be set when it is set to 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">

    <Properties>
        <Property name="PID">????</Property>
        <Property name="LOG_EXCEPTION_CONVERSION_WORD">%xwEx</Property>
        <Property name="LOG_LEVEL_PATTERN">%5p</Property>
        <Property name="LOG_DATEFORMAT_PATTERN">yyyy-MM-dd HH:mm:ss.SSS</Property>
        <Property name="CONSOLE_LOG_PATTERN">%clr{%d{${LOG_DATEFORMAT_PATTERN}}}{faint} %clr{${LOG_LEVEL_PATTERN}} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%15.15t]}{faint} %clr{%-40.40c{1.}}{cyan} %clr{:}{faint} %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
        <Property name="FILE_LOG_PATTERN">%d{${LOG_DATEFORMAT_PATTERN}} ${LOG_LEVEL_PATTERN} ${sys:PID} --- [%t] %-40.40c{1.} : %m%n${sys:LOG_EXCEPTION_CONVERSION_WORD}</Property>
    </Properties>
    <!--Define all first appender-->

    <appenders>

        <!--Configuration of this output console-->
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout pattern="${sys:CONSOLE_LOG_PATTERN}" />
        </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="./logs/info.log"

                     filePattern="./logs/$${date:yyyy-MM}/info-%d{yyyy-MM-dd}-%i.log">

            <!--Console output only level And above( onMatch),Other direct rejection( onMismatch)-->

            <ThresholdFilter level="info" onMatch="ACCEPT" onMismatch="DENY"/>

            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>

            <Policies>

                <TimeBasedTriggeringPolicy/>

                <SizeBasedTriggeringPolicy size="100 MB"/>

            </Policies>

        </RollingFile>

        <RollingFile name="RollingFileWarn" fileName="./logs/warn.log"

                     filePattern="./logs/$${date:yyyy-MM}/warn-%d{yyyy-MM-dd}-%i.log">

            <ThresholdFilter level="warn" onMatch="ACCEPT" onMismatch="DENY"/>

            <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="./logs/error.log"

                     filePattern="./logs/$${date:yyyy-MM}/error-%d{yyyy-MM-dd}-%i.log">

            <ThresholdFilter level="error" onMatch="ACCEPT" onMismatch="DENY"/>

            <PatternLayout pattern="[%d{HH:mm:ss:SSS}] [%p] - %l - %m%n"/>

            <Policies>

                <TimeBasedTriggeringPolicy/>

                <SizeBasedTriggeringPolicy size="100 MB"/>

            </Policies>

        </RollingFile>
        <!-- sql output file -->
        <RollingFile name="SQL" fileName="./logs/sql.log"
                     filePattern="./logs/$${date:yyyy-MM}/sql-%d{yyyy-MM-dd}-%i.log">
            <ThresholdFilter level="debug" onMatch="ACCEPT" onMismatch="DENY"/>

            <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>


        <Logger name="org.apache.catalina.startup.DigesterFactory" level="error" />
        <Logger name="org.apache.catalina.util.LifecycleBase" level="error" />
        <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
        <logger name="org.apache.sshd.common.util.SecurityUtils" level="warn"/>
        <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
        <Logger name="org.eclipse.jetty.util.component.AbstractLifeCycle" level="error" />
        <Logger name="org.hibernate.validator.internal.util.Version" level="warn" />
        <logger name="org.springframework.boot.actuate.endpoint.jmx" level="warn"/>

        <!--output sql-->
        <logger name="com.qthl.wf.dao" level="debug" additivity="false">
            <appender-ref ref="Console"/>
            <appender-ref ref="SQL"/>
        </logger>
        <root level="info">
            <appender-ref ref="Console"/>
            <appender-ref ref="RollingFileInfo"/>
            <appender-ref ref="RollingFileWarn"/>
            <appender-ref ref="RollingFileError"/>
        </root>
    </loggers>

</Configuration>

Keywords: Java Apache Spring xml SQL

Added by mitchell on Sun, 01 Dec 2019 23:00:56 +0200