log4j常用输出格式
%c 列出logger名字空间的全称,如加上{<层数>}表示出从最内层算起的指定层数的名字空间
%X 按MDC(Mapped Diagnostic Context,线程映射表)输出日志。通常用于多个客户端连接同一台服务器,方便服务器区分是那个客户端访问留下来的日志。
%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-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));
0.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>
加载方式 在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>
现在使用的配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n "/>
</layout>
</appender>
<appender name="DEBUG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/debug.log"/> <!--暂时路径,上线后修改为线上路径 -->
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n "/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="DEBUG"/>
</filter>
</appender>
<appender name="INFO" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/info.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n "/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<appender name="WARN" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/warn.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN"/>
<param name="LevelMax" value="WARN"/>
</filter>
</appender>
<appender name="ERROR" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/error.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="ERROR"/>
</filter>
</appender>
<appender name="FATAL" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/fatal.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="FATAL"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="FATAL"/>
</root>
</log4j:configuration>
%c 列出logger名字空间的全称,如加上{<层数>}表示出从最内层算起的指定层数的名字空间
%X 按MDC(Mapped Diagnostic Context,线程映射表)输出日志。通常用于多个客户端连接同一台服务器,方便服务器区分是那个客户端访问留下来的日志。
%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-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));
0.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>
加载方式 在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>
现在使用的配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE log4j:configuration SYSTEM "log4j.dtd">
<log4j:configuration xmlns:log4j='http://jakarta.apache.org/log4j/'>
<appender name="STDOUT" class="org.apache.log4j.ConsoleAppender">
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n "/>
</layout>
</appender>
<appender name="DEBUG" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/debug.log"/> <!--暂时路径,上线后修改为线上路径 -->
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n "/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="DEBUG"/>
<param name="LevelMax" value="DEBUG"/>
</filter>
</appender>
<appender name="INFO" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/info.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n "/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="INFO"/>
<param name="LevelMax" value="INFO"/>
</filter>
</appender>
<appender name="WARN" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/warn.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="WARN"/>
<param name="LevelMax" value="WARN"/>
</filter>
</appender>
<appender name="ERROR" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/error.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="ERROR"/>
<param name="LevelMax" value="ERROR"/>
</filter>
</appender>
<appender name="FATAL" class="org.apache.log4j.DailyRollingFileAppender">
<param name="File" value="d:/fatal.log"/>
<param name="Append" value="true"/>
<param name="DatePattern" value="'.'yyyy-MM-dd-HH"/>
<param name="BufferedIO" value="true"/>
<!-- 8K为一个写单元 -->
<param name="BufferSize" value="8192"/>
<param name="MaxFileSize" value="2GB"/>
<param name="MaxBackupIndex" value="99999"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%5p, %M(%F %L), %d{yyyy-MM-dd_HH:mm:ss}, %m%n"/>
</layout>
<filter class="org.apache.log4j.varia.LevelRangeFilter">
<param name="LevelMin" value="FATAL"/>
<param name="LevelMax" value="FATAL"/>
</filter>
</appender>
<root>
<priority value="DEBUG"/>
<appender-ref ref="STDOUT"/>
<appender-ref ref="DEBUG"/>
<appender-ref ref="INFO"/>
<appender-ref ref="WARN"/>
<appender-ref ref="ERROR"/>
<appender-ref ref="FATAL"/>
</root>
</log4j:configuration>