slg+log4j configuration

JAVA programs are inseparable from the support of logs, especially the generation environment server. When checking problems, the support of logs is inseparable. Now, write the configuration of integrating slf+log4j into the spring framework:

The project structure is as follows:



 

1. Add the log4j configuration file and listener in web.xml

<context-param>

  <param-name>log4jConfigLocation</param-name>

  <param-value>classpath:resources/log4j.properties</param-value>

</context-param>

<listener>  

        <listener-class>  

            org.springframework.web.util.Log4jConfigListener  

        </listener-class>  

 </listener>

 

Second, set the log4j configuration file, the location src/resources/log4j.properties, the content is as follows:

log4j.rootLogger=DEBUG,stdout,InfoFile,ErrorFile

 

#kong zhi tai shu chu she ding

log4j.appender.stdout=org.apache.log4j.ConsoleAppender

log4j.appender.stdout.layout=org.apache.log4j.PatternLayout

log4j.appender.stdout.Threshold=DEBUG

log4j.appender.stdout.layout.ConversionPattern=Log4j:[%d{yyyy-MM-dd HH:mm:ss}] %5p %c{1}:%L - %m%n

 

#info 

log4j.appender.InfoFile = org.apache.log4j.DailyRollingFileAppender

log4j.appender.InfoFile.Threshold=DEBUG

log4j.appender.InfoFile.file=${catalina.base}/logs/activitiLogs/InfoFile.log

log4j.appender.InfoFile.DatePattern='.'yyyy-MM-dd

log4j.appender.InfoFile.layout=org.apache.log4j.PatternLayout

log4j.appender.InfoFile.layout.ConversionPattern=Log4j:[%d{yyyy-MM-dd HH:mm:ss}] %5p %c{1}:%L - %m%n

 

#error

log4j.appender.ErrorFile = org.apache.log4j.DailyRollingFileAppender

log4j.appender.ErrorFile.Threshold=Error

log4j.appender.ErrorFile.File=${catalina.base}/logs/activitiLogs/ErrorFile.log

log4j.appender.ErrorFile.DatePattern='.'yyyy-MM-dd

log4j.appender.ErrorFile.layout=org.apache.log4j.PatternLayout

log4j.appender.ErrorFile.layout.ConversionPattern=Log4j:[%d{yyyy-MM-dd HH:mm:ss}] %5p %c{1}:%L - %m%n

 

The configuration mainly sets the output of the console, info log file and error log file:
Console: Output Debug and above log information

info, error log files:

Output DEBUG and above log information;

Generate a new log file every day;

${catalina.base} represents the root directory of the tomcat server (the parent directory of the bin folder and the conf folder);

Start the Tomcat plug-in service directly in eclipse, and the log output will be in workspace\.metadata\.plugins\org.eclipse.wst.server.core \ tmp3 \logs\activitiLogs ( tmp3 will change, the rule is tmp* );

The info-level log will only be entered in the InfoFile.log file, and the error-level log will appear in both the InfoFile.log and error File.log files;

 

The configuration file can also set the log information generated by a single class to be output to a log file separately. There is no such detailed configuration here, but it is simply distinguished according to the level.

 

3. Call in java code

 

package com.pb.modult.admin.biz.service;

 

import org.apache.log4j.Logger;

import org.apache.log4j.spi.LoggerFactory;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Service;

 

import com.pb.modult.admin.biz.bo.AdminBO;

/**

 * Administrator

 * @author Administrator

 *

 */

@Service

public class AdminService {

@Autowired

private AdminBO adminBO;

 

Logger logger = Logger.getLogger(AdminService.class);

 

org.slf4j.Logger logger2 = org.slf4j.LoggerFactory.getLogger("InfoFile");

 

public void login(){

logger.info("info log");

logger.error("error log");

logger2.debug("debug log, primary key {}", "primary key value");

}

}

 

slf can set placeholders to directly concatenate strings

 

 

Guess you like

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