log4j配置

1) properties:

log4j.rootLogger = INFO ,  stdout ,  R
log4j.appender.stdout
= org.apache.log4j.ConsoleAppender
log4j.appender.stdout.layout
= org.apache.log4j.PatternLayout
# Pattern to output the caller's file name and line number.
#log4j.appender.stdout.layout.ConversionPattern
= %5p  [ %t ]  (%F:%L) - %m%n
# Print the date in ISO 
8601  format
log4j.appender.stdout.layout.ConversionPattern
= %d  [ %t ]  %-5p %c - %m%n
log4j.appender.R
= org.apache.log4j.RollingFileAppender
log4j.appender.R.File
= example.log
log4j.appender.R.MaxFileSize
= 100KB
# Keep one backup file
log4j.appender.R.MaxBackupIndex
= 1
log4j.appender.R.layout
= org.apache.log4j.PatternLayout
log4j.appender.R.layout.ConversionPattern
= %p %t %c - %m%n
# Print only messages of level WARN or above in the package com.foo.
log4j.logger.com.foo
= WARN 

2) xml

<? xml version="1.0" encoding="UTF-8" ?>   
<! DOCTYPE log4j:configuration SYSTEM "log4j.dtd" >   
< log4j:configuration  xmlns:log4j ='http://jakarta.apache.org/log4j/'   >   
    
< appender  name ="appendConsole"  class ="org.apache.log4j.ConsoleAppender" >   
        
< layout  class ="org.apache.log4j.PatternLayout" >   
            
< param  name ="ConversionPattern"      
                value
="%8r [%t] %-5p %c - %m%n"   />   
        
</ layout >
        
< filter  class ="org.apache.log4j.varia.LevelRangeFilter" >   
            
< param  name ="levelMin"  value ="debug"   />   
            
< param  name ="levelMax"  value ="fatal"   />   
            
< param  name ="AcceptOnMatch"  value ="true"   />   
        
</ filter >   
    
</ appender >   
  
    
< appender  name ="appendFile"  class ="org.apache.log4j.RollingFileAppender" >      
        
< param  name ="File"  value ="./log/monitorinfo.log"   />
        
< param  name ="Append"  value ="true"   />   
        
< param  name ="MaxBackupIndex"  value ="10"   />   
        
< param  name ="MaxFileSize"  value ="10MB"   />  
        
< layout  class ="org.apache.log4j.PatternLayout" >   
            
< param  name ="ConversionPattern"  value ="%d{yyyy-MM-dd HH:mm:ss SSS} [%t] %-5p %c - %m%n"   />   
        
</ layout >   
    
</ appender >
    
    
< appender  name ="appendDB"  class ="org.apache.log4j.jdbc.JDBCAppender" >      
         
< param  name ="driver"  value ="com.mysql.jdbc.Driver"   />   
         
< param  name ="URL"  value ="jdbc:mysql://hsm_db:3306/hsm_db"   />   
         
< param  name ="user"  value ="root"   />   
         
< param  name ="password"  value ="hillstone_root"   />   
         
< param  name ="bufferSize"  value ="100"   />   
         
< layout  class ="org.apache.log4j.PatternLayout" >   
             
< param  name ="ConversionPattern"   
                 value
="insert into t_HsmOperLog(logType,funId,objType,objID,operType,operResult,failureReason,operUserId, sessionId, recordTime, description) 
values('%X{logType}', '%X{funId}', '%X{objType}', '%X{objID}', '%X{operType}', '%X{operResult}', '%X{failureReason}', '%X{operUserId}', '%X{sessionId}', '%X{recordTime}', '%X{description}')"
/>
        
</ layout >  
         
         
< filter  class ="org.apache.log4j.varia.LevelRangeFilter" >   
             
< param  name ="levelMin"  value ="error"   />   
            
< param  name ="levelMax"  value ="fatal"   />   
            
< param  name ="AcceptOnMatch"  value ="true"   />  
         
</ filter >           
    
</ appender >
    
    
< logger  name ="org.springframework.beans" >
        
< level  value ="warn"   />
        
< appender-ref  ref ="appendConsole" />   
    
</ logger >
    
     
< logger  name ="org.hibernate" >
        
< level  value ="warn"   />
        
< appender-ref  ref ="appendConsole" />   
    
</ logger >
    
     
< logger  name ="org.springframework.security" >
        
< level  value ="warn"   />
        
< appender-ref  ref ="appendConsole" />   
    
</ logger >
  
    
< root >   
        
< priority  value  ="debug" />   
        
< appender-ref  ref ="appendConsole" />   
        
< appender-ref  ref ="appendFile" />      
    
</ root >   
</ log4j:configuration >   

3) load config in java class:

  

DOMConfigurator.configure( this .getClass().getClassLoader().getSystemResource( " conf/monitor/log4j.xml " ));
PropertyConfigurator.configure(
this .getClass().getClassLoader().getSystemResource( " conf/log4j.properties " ));

你可能感兴趣的:(log4j)