log4j配置

log4j常用输出格式

%c列出logger名字空间的全称,如加上{<层数>}表示出从最内层算起的指定层数的名字空间

%X按MDC(MappedDiagnosticContext,线程映射表)输出日志。通常用于多个客户端连接同一台服务器,方便服务器区分是那个客户端访问留下来的日志。

%p日志信息级别

%d%d{<日期格式>}:日志信息产生时间,使用ISO8601定义的日期格式

%C日志信息所在地(全限类名)

%m产生的日志具体信息

%n输出日志信息换行

%F 显示调用logger的源文件名

%l输出日志事件的发生位置,包括类目名、发生的线程,以及在代码中的行数

%L显示调用logger的代码行

%M显示调用logger的方法名

%r显示从程序启动时到记录该条日志时已经经过的毫秒数

%t输出产生该日志事件的线程名

%% 显示一个

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-ddhh: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-ddhh: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:mumuURL

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=insertintolog4j(createdate,thread,level_,class,message)values(\'%d\',\'%t\',\'%-5p\',\'%c\',\'%m\')

createtablelog4j(createdatevarchar2(32),threadvarchar2(32),level_varchar2(32),classvarchar2(32),messagevarchar2(32));

0.XML模板

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPElog4j:configurationSYSTEM"log4j.dtd">

<log4j:configuration>

<appendername="ConsoleAppender"class="org.apache.log4j.ConsoleAppender">

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%d{yyyy-MM-ddhh:mm:ss}:%p%t%c-%m%n"/>

</layout>

</appender>

<appendername="RollingFileAppender"class="org.apache.log4j.RollingFileAppender">

<paramname="Append"value="false"/>

<paramname="MaxFileSize"value="1KB"/>

<paramname="File"value="dom/my.log"/>

<paramname="MaxBackupIndex"value="3"/>

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%d{yyyy-MM-ddhh:mm:ss}:%p%t%c-%m%n"/>

</layout>

</appender>

<appendername="JDBCAppender"class="org.apache.log4j.jdbc.JDBCAppender">

<paramname="URL"value="jdbc:oracle:thin:@127.0.0.1:1521:mumu"/>

<paramname="user"value="liulibo"/>

<paramname="password"value="liulibo"/>

<paramname="driver"value="oracle.jdbc.driver.OracleDriver"/>

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="insertintolog4j(createdate,thread,level_,class,message)values(\'%d\',\'%t\',\'%-5p\',\'%c\',\'%m\')"/>

</layout>

</appender>

<root>

<priorityvalue="debug"/>

<appender-refref="ConsoleAppender"/>

<appender-refref="RollingFileAppender"/>

<appender-refref="JDBCAppender"/>

</root>

</log4j:configuration>

加载方式在web.xml中添加

<context-param>

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

<param-value>classpath:runtimecfg/log4j.xml</param-value>

</context-param>

<context-param>

<param-name>log4jRefreshInterval</param-name>

<param-value>3600000</param-value>

</context-param>

<listener>

<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>

</listener>

现在使用的配置文件:

<?xmlversion="1.0"encoding="UTF-8"?>

<!DOCTYPElog4j:configurationSYSTEM"log4j.dtd">

<log4j:configurationxmlns:log4j='http://jakarta.apache.org/log4j/'>

<appendername="STDOUT"class="org.apache.log4j.ConsoleAppender">

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%5p,%M(%F%L),%d{yyyy-MM-dd_HH:mm:ss},%m%n"/>

</layout>

</appender>

<appendername="DEBUG"class="org.apache.log4j.DailyRollingFileAppender">

<paramname="File"value="d:/debug.log"/><!--暂时路径,上线后修改为线上路径-->

<paramname="Append"value="true"/>

<paramname="DatePattern"value="'.'yyyy-MM-dd-HH"/>

<paramname="BufferedIO"value="true"/>

<!--8K为一个写单元-->

<paramname="BufferSize"value="8192"/>

<paramname="MaxFileSize"value="2GB"/>

<paramname="MaxBackupIndex"value="99999"/>

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%5p,%M(%F%L),%d{yyyy-MM-dd_HH:mm:ss},%m%n"/>

</layout>

<filterclass="org.apache.log4j.varia.LevelRangeFilter">

<paramname="LevelMin"value="DEBUG"/>

<paramname="LevelMax"value="DEBUG"/>

</filter>

</appender>

<appendername="INFO"class="org.apache.log4j.DailyRollingFileAppender">

<paramname="File"value="d:/info.log"/>

<paramname="Append"value="true"/>

<paramname="DatePattern"value="'.'yyyy-MM-dd-HH"/>

<paramname="BufferedIO"value="true"/>

<!--8K为一个写单元-->

<paramname="BufferSize"value="8192"/>

<paramname="MaxFileSize"value="2GB"/>

<paramname="MaxBackupIndex"value="99999"/>

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%5p,%M(%F%L),%d{yyyy-MM-dd_HH:mm:ss},%m%n"/>

</layout>

<filterclass="org.apache.log4j.varia.LevelRangeFilter">

<paramname="LevelMin"value="INFO"/>

<paramname="LevelMax"value="INFO"/>

</filter>

</appender>

<appendername="WARN"class="org.apache.log4j.DailyRollingFileAppender">

<paramname="File"value="d:/warn.log"/>

<paramname="Append"value="true"/>

<paramname="DatePattern"value="'.'yyyy-MM-dd-HH"/>

<paramname="BufferedIO"value="true"/>

<!--8K为一个写单元-->

<paramname="BufferSize"value="8192"/>

<paramname="MaxFileSize"value="2GB"/>

<paramname="MaxBackupIndex"value="99999"/>

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%5p,%M(%F%L),%d{yyyy-MM-dd_HH:mm:ss},%m%n"/>

</layout>

<filterclass="org.apache.log4j.varia.LevelRangeFilter">

<paramname="LevelMin"value="WARN"/>

<paramname="LevelMax"value="WARN"/>

</filter>

</appender>

<appendername="ERROR"class="org.apache.log4j.DailyRollingFileAppender">

<paramname="File"value="d:/error.log"/>

<paramname="Append"value="true"/>

<paramname="DatePattern"value="'.'yyyy-MM-dd-HH"/>

<paramname="BufferedIO"value="true"/>

<!--8K为一个写单元-->

<paramname="BufferSize"value="8192"/>

<paramname="MaxFileSize"value="2GB"/>

<paramname="MaxBackupIndex"value="99999"/>

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%5p,%M(%F%L),%d{yyyy-MM-dd_HH:mm:ss},%m%n"/>

</layout>

<filterclass="org.apache.log4j.varia.LevelRangeFilter">

<paramname="LevelMin"value="ERROR"/>

<paramname="LevelMax"value="ERROR"/>

</filter>

</appender>

<appendername="FATAL"class="org.apache.log4j.DailyRollingFileAppender">

<paramname="File"value="d:/fatal.log"/>

<paramname="Append"value="true"/>

<paramname="DatePattern"value="'.'yyyy-MM-dd-HH"/>

<paramname="BufferedIO"value="true"/>

<!--8K为一个写单元-->

<paramname="BufferSize"value="8192"/>

<paramname="MaxFileSize"value="2GB"/>

<paramname="MaxBackupIndex"value="99999"/>

<layoutclass="org.apache.log4j.PatternLayout">

<paramname="ConversionPattern"value="%5p,%M(%F%L),%d{yyyy-MM-dd_HH:mm:ss},%m%n"/>

</layout>

<filterclass="org.apache.log4j.varia.LevelRangeFilter">

<paramname="LevelMin"value="FATAL"/>

<paramname="LevelMax"value="FATAL"/>

</filter>

</appender>

<root>

<priorityvalue="DEBUG"/>

<appender-refref="STDOUT"/>

<appender-refref="DEBUG"/>

<appender-refref="INFO"/>

<appender-refref="WARN"/>

<appender-refref="ERROR"/>

<appender-refref="FATAL"/>

</root>

</log4j:configuration>

相关推荐