spring boot configuration log4j log

In fact, spring boot uses logback by default. And if we want to use log4j, we can... Because the built-in Tomcat can be recorded directly using the log4j class For example:

import org.apache.log4j.Logger;

private Logger log = Logger.getLogger(this.getClass()); 

Of course, only in application.yml 
Here's a brief explanation: 
logging:
  level: warn
  file: logs/guns.log 

It can be used simply. Of course, if you don't specify which log to use, the default is logback.

For example: 
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

  protected final static Logger logger = LoggerFactory.getLogger(GunsApplication.class);

It can be used simply. But for larger systems, this log configuration is too simple...

This shows that the org.slf4j.Logger class will automatically print the log using the default log type of the current project... Like jdbc, you don't care what database you use.

I'm going to introduce log4j here. I'm going to use log4j.xml here. Personally, I prefer the way of xml. You can use log4j.properties, which is Baidu's most popular way. This watcher likes it. All of them are the same, the log4j configuration file Just put it in the resource package resources. At the same time, we need to introduce pom

<!--Not applicable to default logback Record log introduction log4j Record  -->
         <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>
     <version>${log4j.version}</version>
    </dependency>

When Baidu is online, they say they don't need to bring the version number of log4j, but I can't find jar because of maven's error. So I added... In this way, it can be used directly.

Below is my configuration file in xml form, you can refer to


<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/">

    <appender name="CONSOLE" class="org.apache.log4j.ConsoleAppender">
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="[GUN.com] %-d{yyyy-MM-dd HH:mm:ss} %p [%t] %c{1}.%M(%L) | %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="DEBUG" /> 
            <param name="LevelMax" value="ERROR" />
        </filter>
    </appender>   

    <appender name="WARN" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="/web/logs/gun/warn.log"/>
        <param name="Append" value="true"/>
        <param name="Encoding" value="utf-8"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[GUN.com] %-d{yyyy-MM-dd HH:mm:ss} %-5p %-15c{1}: %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="WARN" />
            <param name="LevelMax" value="WARN" />
        </filter>
    </appender>

    <appender name="ERROR" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="/web/logs/gun/error.log"/>
        <param name="Append" value="true"/>
        <param name="Encoding" value="utf-8"/>
        <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern" value="[GUN.com] %-d{yyyy-MM-dd HH:mm:ss} %-5p %-15c{1}: %m%n"/>
        </layout>
        <filter class="org.apache.log4j.varia.LevelRangeFilter">
            <param name="LevelMin" value="ERROR" />
            <param name="LevelMax" value="ERROR" />
        </filter>
    </appender>

    <!-- Actually, these are logger It's also possible not to configure because the root node already has the basic content of printing and recording all warnings and errors.  -->

    <logger name="org.springframework.web.servlet.DispatcherServlet">
         <level value="DEBUG" /> 
         <!--Print to the corresponding log file  -->
            <appender-ref ref="CONSOLE"/>  
    </logger>

    <!-- logger That is to enter the log under the corresponding class or package.  -->
   <logger name="com.stylefeng.guns"> 
       <level value="DEBUG" /> 
         <!--Print to the corresponding log file  -->
        <appender-ref ref="CONSOLE"/>  
    </logger>

    <logger name="com.alibaba.druid"> 
        <level value="DEBUG" /> 
         <!--Print to the corresponding log file  -->
         <appender-ref ref="CONSOLE"/>   
    </logger>

    <!--
        root Role(Equivalent to the overall meaning):
        1.[priority ]:Specify the default global output level
        2.[appender-ref ]:Specify some defaults append(No special packages or classes are specified, that is, those that are not specified<logger>Elemental append)Output;
    -->
    <root>
        <priority value="debug"/>

        <!-- <appender-ref ref="CONSOLE"/>  -->

        <appender-ref ref="WARN"/>
        <appender-ref ref="ERROR"/>

    </root>

</log4j:configuration>

To illustrate,

If you put <appender-ref ref="CONSOLE"/> into root There will be a lot of, bug records, too many, ugly consoles, especially when debugging, people don't like this... So you can customize some logger s to control printing some information to the console.

Logger is a logger that controls where to print or enter the levels of the logs of those classes or the classes below the package.

The <appender-ref ref="ERROR"/> below root is global, and the corresponding level information will be recorded in the corresponding log file whether or not the logger is configured.

Keywords: log4j Apache Spring xml

Added by vboctor on Thu, 20 Jun 2019 01:46:13 +0300