log4j configuration

One: Introduce the dependent JAR package of log4j. If it is not a maven project, download the JAR package directly and put it in the LIB directory of the project.

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

Two: Add the file log4j.properties to the src directory of the project, and add the content

 

###Setup ###
log4j.rootLogger = debug , console , I , D , E , F  
  
### Output information to control console ### 
log4j.appender.console = org.apache.log4j.DailyRollingFileAppender  
log4j.appender. console.Target = System.out  
log4j.appender.console.layout = org.apache.log4j.PatternLayout  
log4j.appender.console.layout.ConversionPattern = [%-5p] %d{yyyy-MM-dd HH\:mm\ :ss,SSS} Console\:%m - %l %n  
  
### Output logs above DEBUG level to =E://logs/error.log ###  
log4j.appender.D = org.apache.log4j. DailyRollingFileAppender  
log4j.appender.D.File = E\://logs/debug.log  
log4j.appender.D.Append = true  
log4j.appender.D.Threshold = DEBUG   
log4j.appender.D.layout = org.apache.log4j .PatternLayout  
log4j.appender.D.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss}  [ %t\:%r ] - [ %p ]  %m - %l %n  
  
 ### 输出ERROR 级别以上的日志到=E://logs/error.log ###  
log4j.appender.E = org.apache.log4j.DailyRollingFileAppender  
log4j.appender.E.File =E\://logs/error.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 - %l %n  


### 输出INFO 级别以上的日志到=E://logs/info.log ###   
log4j.appender.I = org.apache.log4j.DailyRollingFileAppender  
log4j.appender.I.File =E\://logs/info.log   
log4j.appender.I.Append = true  
log4j.appender.I.Threshold = INFO   
log4j.appender.I.layout = org.apache.log4j.PatternLayout  
log4j.appender.I.layout.ConversionPattern = %-d{yyyy-MM-dd HH\:mm\:ss}  [ %t\:%r ] - [ %p ]  %m - %l %n  


### 输出FATAL 级别以上的日志到=E://logs/fatal.log ###   
log4j.appender.F = org.apache.log4j.DailyRollingFileAppender  
log4j.appender.F.File =E\://logs/fatal.log   
log4j.appender.F.Append = true  
log4j.appender.F.Threshold = FATAL   
log4j.appender.F.layout = org.apache.log4j.PatternLayout  
log4j.appender.F.layout.ConversionPattern =%-d{yyyy-MM-dd HH\:mm\:ss}  [ %t\:%r ] - [ %p ]  %m - %l %n

 

Five levels of logs are defined above, including the format of output to the console. Each type of log has a generation strategy, that is, the above configuration

 

Three: web.xml adds log4j configuration

<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>classpath:log4j.properties</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>

 

Four: call the method and generate the object in the class

private Logger logger = Logger.getLogger(this.getClass().getName()); 

logger.error("message");

logger.info("message");

 

In this way, you can go to the E drive to view the generated log. This default configuration method is sufficient for general projects. Basically, no changes are required. For more strategic information, you can find out about it on Baidu. In fact, this configuration defines these types of logs, how they are generated, and how they are generated.

Guess you like

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