博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
log4j配置
阅读量:5910 次
发布时间:2019-06-19

本文共 3664 字,大约阅读时间需要 12 分钟。

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
"
));

转载于:https://www.cnblogs.com/zhongkl/archive/2011/07/14/2106569.html

你可能感兴趣的文章
Apache Spark源码走读之7 -- Standalone部署方式分析
查看>>
如何避免重构带来的危险
查看>>
小程序生命周期
查看>>
有序的双链表
查看>>
MSSQLServer的备份与还原
查看>>
Eclipse导入的项目中发现包的形式变成了文件夹的形式,需要将文件夹的形式变成包...
查看>>
使用MySQL yum源安装MySQL
查看>>
iOS8中使用CoreLocation定位
查看>>
R语言处理Time series
查看>>
HTML5 Audio/Video 标签,属性,方法,事件汇总
查看>>
mvn package时设置了maven.test.skip=true依旧执行单元测试
查看>>
Java学习笔记(一)背景知识
查看>>
PAT 1118 Birds in Forest [一般]
查看>>
Adapting to views using css or js
查看>>
三张图看懂 clientheight、offsetheight、scrollheight
查看>>
020PHP基础知识——函数(三)
查看>>
构造函数&&继承8.1
查看>>
bzoj千题计划232:bzoj4727: [POI2017]Turysta
查看>>
Codeforces 923 A. Primal Sport
查看>>
selenium 关于富文本的处理
查看>>