How to add log files in SSM project in LOG4J

The first step: After the project is established, add the log4j plug-in package to the pom file

        <dependency>
            <groupId>log4j</groupId>
            <artifactId>log4j</artifactId>
            <version>1.2.17</version>
        </dependency>

Step 2: Create a new file log4j.properties in the resources file of the project, and copy the following content to the file (log4j.properties).

# priority  :debug<info<warn<error
#you cannot specify every priority with different file for log4j
#这里描述了是日志的输出格式
log4j.rootLogger=debug,stdout,info,debug,warn,error 
#
#console控制台输出内容
log4j.appender.stdout=org.apache.log4j.ConsoleAppender 
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout 
#在这一行可以修改日期格式以及加入创作者的名称
log4j.appender.stdout.layout.ConversionPattern= [Author-->%d{
    
    yyyy-MM-dd HH:mm:ss a}]:%p %l%m%n
#info log----这里是info的基本信息输出模式
log4j.logger.info=info
log4j.appender.info=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.info.DatePattern='_'yyyy-MM-dd'.log'
#这里是日志文件保存的地址,也就是项目里面的src文件夹里面
log4j.appender.info.File=./src/com/hp/log/info.log
log4j.appender.info.Append=true
log4j.appender.info.Threshold=INFO
log4j.appender.info.layout=org.apache.log4j.PatternLayout 
log4j.appender.info.layout.ConversionPattern=Author-->%d{
    
    yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#debug log----这里是代码默认信息输出模式
log4j.logger.debug=debug
log4j.appender.debug=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.debug.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.debug.File=./src/com/hp/log/debug.log
log4j.appender.debug.Append=true
log4j.appender.debug.Threshold=DEBUG
log4j.appender.debug.layout=org.apache.log4j.PatternLayout 
log4j.appender.debug.layout.ConversionPattern=Author-->%d{
    
    yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#warn log----这里是warn代码中警告信息的输出模式
log4j.logger.warn=warn
log4j.appender.warn=org.apache.log4j.DailyRollingFileAppender 
log4j.appender.warn.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.warn.File=./src/com/hp/log/warn.log
log4j.appender.warn.Append=true
log4j.appender.warn.Threshold=WARN
log4j.appender.warn.layout=org.apache.log4j.PatternLayout 
log4j.appender.warn.layout.ConversionPattern=Author-->%d{
    
    yyyy-MM-dd HH:mm:ss a} [Thread: %t][ Class:%c >> Method: %l ]%n%p:%m%n
#error
log4j.logger.error=error
log4j.appender.error = org.apache.log4j.DailyRollingFileAppender
log4j.appender.error.DatePattern='_'yyyy-MM-dd'.log'
log4j.appender.error.File = ./src/com/hp/log/error.log 
log4j.appender.error.Append = true
log4j.appender.error.Threshold = ERROR 
log4j.appender.error.layout = org.apache.log4j.PatternLayout
log4j.appender.error.layout.ConversionPatter

Step 3: Create a new file mybatis-config.xml in the resources file, and copy the following content into it. The most important thing is to set the content in the settings to make the log file effective.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE configuration
        PUBLIC "-//mybatis.org//DTD Config 3.0//EN"
        "http://mybatis.org/dtd/mybatis-3-config.dtd">
<configuration>
    <settings>
        <setting name="logImpl" value="LOG4J"/>
    </settings>
    <!--核心配置文件-->
</configuration>

Step 4: In your java file that needs to be added to the log, write the following line of statements to get the logger object (pay special attention to import org.apache.log4j.Logger;)

static Logger logger=Logger.getLogger(test.class);

The following is the test code: (You can add the following three lines of statements to any code, provided that the above line of code can be realized)

 @Test
    public void testlog4j(){
    
    
        logger.info("infor进入了testLog4j");
        logger.debug("debug进入了testLog4j");
        logger.error("error进入了testLog4j");
    }

Friendly reminder @Test This annotation needs to be added to the following to run, otherwise you use the mian method to execute

 <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.12</version>
    </dependency>

Guess you like

Origin blog.csdn.net/qq_34134299/article/details/113838288