Add log configuration to maven web project of ssm framework

In this article, the maven web project of the ssm framework is added to the log configuration (supplement to the previous article). The log configuration is not added. When the project is started, the following information will appear

Let's talk about how to solve this problem (although this does not affect the previous test, the log in the program will not be printed)

Still the last directory map (javaEE switches to java mode)

  

1. Add the dependency configuration of log4j to pom.xml (it has been added in the previous project, you can add it if it has not been added), the configuration file is as follows:

       <properties>
            .
            .
            .
        <slf4j.version>1.7.7</slf4j.version>
            .
            .
            .
    </properties> 



<!-- 日志打印 -->
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>jcl-over-slf4j</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
        <dependency>
            <groupId>org.slf4j</groupId>
            <artifactId>slf4j-log4j12</artifactId>
            <version>${slf4j.version}</version>
        </dependency>
pom.xml

2. Read the configuration file from applicationContext.xml

<!-- Introduce *.properties property file --> 
< context:property-placeholder location ="classpath:db.properties" />
View Code

changed to

<!-- Introduce multiple configuration files ending with properties --> 
< context:property-placeholder location ="classpath:*.properties" />
View Code

3. Add log4j.properties

# Global logging configuration
log4j.rootLogger=info, stdout, E
# JDBC and MyBatis logging configuration...
log4j.logger.com.ibatis=debug
log4j.logger.com.ibatis.common.jdbc.SimpleDataSource=debug
log4j.logger.com.ibatis.common.jdbc.ScriptRunner=debug
log4j.logger.com.ibatis.sqlmap.engine.impl.SqlMapClientDelegate=debug
log4j.logger.java.sql.Connection=debug
log4j.logger.java.sql.Statement=debug
log4j.logger.java.sql.PreparedStatement=debug,stdout

# Console output...
log4j.appender.stdout=org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout=org.apache.log4j.PatternLayout
log4j.appender.stdout.layout.ConversionPattern=%5p [%t] - %m%n

# Save error message to file...
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender
log4j.appender.E.File = ../logs/error.log
log4j.appender.E.DatePattern=yyyy-MM-dd'.log'
log4j.appender.E.Append = true
log4j.appender.E.Threshold = ERROR
log4j.appender.E.layout = org.apache.log4j.PatternLayout
log4j.appender.E.layout.ConversionPattern = %-d{yyyy-MM-dd HH:mm:ss}  [ %t:%r ] - [ %p ]  %m%n
log4j.properties

The above file was copied from a project I randomly found. If you want to know what it specifically means, you can click this link: https://www.cnblogs.com/likui360/p/7992982.html

4. Write test method

package com.su.study.controller;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public  class LogTest {

    /**
     * log object
     */
    private static Logger LOG = LoggerFactory.getLogger(LogTest.class);


    public static void main(String[] args) {

        LOG.info( "Log Test~~~" );

    }

}
LogTest

5. Test

 Start the main method directly. If the log4j.properties file is not added, the warning message at the beginning of the article will still appear. After adding the configuration information, the console will print the log information normally.

The log warning message will no longer be reported when the project is started.

 

Guess you like

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