log4j应用配置

log4j配置模板:

log4j.properties:

log4j.rootLogger=info,CONSOLE,RFILE,FILE,DB 设置级别和三个输出端
log4j.appender.CONSOLE=org.apache.log4j.ConsoleAppender 
log4j.appender.CONSOLE.Target=System.out 控制台类型
log4j.appender.CONSOLE.layout=org.apache.log4j.PatternLayout 
log4j.appender.CONSOLE.layout.ConversionPattern= %4p [%t] (%F:%L) - %m%n

log4j.appender.FILE=org.apache.log4j.FileAppender 
log4j.appender.FILE.File=/help/my.properties 目标文件
log4j.appender.FILE.Append=false 是否追加
log4j.appender.FILE.layout=org.apache.log4j.PatternLayout 布局模式
log4j.appender.FILE.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %c:%L - %m%n 格式化布局

log4j.appender.RFILE=org.apache.log4j.RollingFileAppender 
log4j.appender.RFILE.File=/help/my.properties 目标文件
log4j.appender.RFILE.MaxFileSize=1KB 最大长度
log4j.appender.RFILE.MaxBackupIndex=3 最多备份
log4j.appender.RFILE.layout=org.apache.log4j.PatternLayout 布局模式
log4j.appender.RFILE.layout.ConversionPattern=%d{yyyy-MM-dd hh:mm:ss}:%p %c:%L - %m%n 格式化布局

log4j.appender.DB=org.apache.log4j.jdbc.JDBCAppender 
log4j.appender.DB.URL=jdbc:oracle:thin:@127.0.0.1:1521:mumu URL
log4j.appender.DB.driver=oracle.jdbc.driver.OracleDriver 驱动
log4j.appender.DB.user=liulibo 用户名
log4j.appender.DB.password=liulibo 密码
log4j.appender.DB.layout=org.apache.log4j.PatternLayout 布局模式
log4j.appender.DB.layout.ConversionPattern=insert into log4j(createdate,thread,level_,class,message) values(\'%d\',\'%t\',\'%-5p\',\'%c\',\'%m\')

create table log4j(createdate varchar2(32),thread varchar2(32),level_ varchar2(32),class varchar2(32),message varchar2(32));

 log4j.xml:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">

<log4j:configuration>

<appender name="ConsoleAppender" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n"/>
</layout>
</appender>

<appender name="RollingFileAppender" class="org.apache.log4j.RollingFileAppender">
<param name="Append" value="false"/>
<param name="MaxFileSize" value="1KB"/> 
<param name="File" value="dom/my.log"/> 
<param name="MaxBackupIndex" value="3"/> 
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d{yyyy-MM-dd hh:mm:ss}:%p %t %c - %m%n"/>
</layout>
</appender>

<appender name="JDBCAppender" class="org.apache.log4j.jdbc.JDBCAppender">
<param name="URL" value="jdbc:oracle:thin:@127.0.0.1:1521:mumu"/>
<param name="user" value="liulibo"/> 
<param name="password" value="liulibo"/> 
<param name="driver" value="oracle.jdbc.driver.OracleDriver"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="insert into log4j(createdate,thread,level_,class,message) values(\'%d\',\'%t\',\'%-5p\',\'%c\',\'%m\')"/>
</layout>
</appender>

<root>
<priority value ="debug" />
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="RollingFileAppender"/>
<appender-ref ref="JDBCAppender"/>
</root>

</log4j:configuration>

如果文件不完整总是会出现

log4j:ERROR No appender named [consoleAppender] could be found.

log4j:WARN No appenders could be found for logger (org.springframework.test.context.junit4.SpringJUnit4ClassRunner).

log4j:WARN Please initialize the log4j system properly.

log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

SLF4J: Failed to load class "org.slf4j.impl.StaticLoggerBinder".

SLF4J: Defaulting to no-operation (NOP) logger implementation

SLF4J: See http://www.slf4j.org/codes.html#StaticLoggerBinder for further details.

这样的错误。

 

 (1). 输出方式appender一般有5种: 

org.apache.log4j.RollingFileAppender(滚动文件,自动记录最新日志) 

org.apache.log4j.ConsoleAppender (控制台) 
org.apache.log4j.FileAppender (文件) 
org.apache.log4j.DailyRollingFileAppender (每天产生一个日志文件) 
org.apache.log4j.WriterAppender (将日志信息以流格式发送到任意指定的地方) 

 

(2). 日记记录的优先级priority,优先级由高到低分为 
OFF ,FATAL ,ERROR ,WARN ,INFO ,DEBUG ,ALL。 
Log4j建议只使用FATAL ,ERROR ,WARN ,INFO ,DEBUG这五个级别。 

(3). 格式说明layout中的参数都以%开始,后面不同的参数代表不同的格式化信息(参数按字母表顺序列出): 
%c 输出所属类的全名,可在修改为 %d{Num} ,Num类名输出的维(如:"org.apache.elathen.ClassName",%C{2}将输出elathen.ClassName) 
%d 输出日志时间其格式为 %d{yyyy-MM-dd HH:mm:ss,SSS},可指定格式 如 %d{HH:mm:ss} 
%l 输出日志事件发生位置,包括类目名、发生线程,在代码中的行数 
%n 换行符 
%m 输出代码指定信息,如info(“message”),输出message 
%p 输出优先级,即 FATAL ,ERROR 等 
%r 输出从启动到显示该log信息所耗费的毫秒数 
%t 输出产生该日志事件的线程名

 

 参考:http://www.blogjava.net/hello-yun/archive/2011/10/09/360283.html

你可能感兴趣的:(log4j)