在项目中使用log4j,大致过程如下:
1.web.xml中配置对log4j配置文件的加载。如下
<context-param>
<param-name>logbackConfigLocation</param-name>
<param-value>file:${config.root}/logback.xml</param-value>
</context-param>
或
<context-param>
<param-name>log4jConfigLocation</param-name>
<param-value>/WEB-INF/conf/log4j.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
2.代码中对log4j的使用,进行日志的打印。
有两种使用方式,一种是别名应用,一种是包引用。
别名使用就是在logback.xml配置文件中配置别名,然后在代码中引用别名,从而把日志打印到对应别名所在的目录下。如下:
protected static final Logger logger = Logger.getLogger("quartz");
包引用相对比较简单,在代码中只需要获得类的实例即可。日志的打印位置为logback.xml配置中对应包所在的路径。如下:
private static final Logger logger = Logger.getLogger(PCITransferServiceImpl.class);
3.Logback.xml配置详解
3.1.别名配置:
打印日志的路径:
<appender name="EXT-INTEGRATION-P2P-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>UTF-8</encoder>
<file>/opt/logs/appserver/p2p/ext_integration_p2p.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/opt/logs/appserver/p2p/ext_integration_p2p.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- 当文件大小达到1GB时,会自动按照fileNamePattern规定格式生成新的日志文件 -->
<maxFileSize>1GB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%date{yyyy-MM-ddHH:mm:ss}] %p %C %t %X{logPrefix}, %m%n</Pattern>
</layout>
</appender>
别名对路径的引用,别名为quartz:
<logger name=" quartz " level="info" additivity="false">
<appender-ref ref=" EXT-INTEGRATION-P2P-APPENDER " />
<appender-ref ref="ERROR-P2P-APPENDER" />
</logger>
日志都打印在ext_integration_p2p.log中。
3.2.包引用配置
<appender name="CORE-SERVICE-P2P-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>UTF-8</encoder>
<file>/opt/logs/appserver/p2p/core_service_p2p.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/opt/logs/appserver/p2p/core_service_p2p.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- 当文件大小达到1GB时,会自动按照fileNamePattern规定格式生成新的日志文件 -->
<maxFileSize>1GB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%date{yyyy-MM-ddHH:mm:ss}] %p %C %t %X{logPrefix}, %m%n</Pattern>
</layout>
</appender>
对路径的引用:
<logger name="com.netfinworks.appserver.p2p" level="info" additivity="false">
<appender-ref ref="CORE-SERVICE-P2P-APPENDER" />
<appender-ref ref="ERROR-P2P-APPENDER" />
</logger>
com.netfinworks.appserver.p2p下的日志都打印在core_service_p2p.log中。
错误日志配置:
<appender name="ERROR-P2P-APPENDER" class="ch.qos.logback.core.rolling.RollingFileAppender">
<encoder>UTF-8</encoder>
<file>/opt/logs/appserver/p2p/error.log</file>
<filter class="ch.qos.logback.classic.filter.LevelFilter">
<level>ERROR</level>
<onMatch>ACCEPT</onMatch>
<onMismatch>DENY</onMismatch>
</filter>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>/opt/logs/appserver/p2p/error.log.%d{yyyy-MM-dd}.%i</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- 当文件大小达到1GB时,会自动按照fileNamePattern规定格式生成新的日志文件 -->
<maxFileSize>1GB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%date{yyyy-MM-ddHH:mm:ss}] %p %C %t %X{logPrefix}, %m%n</Pattern>
</layout>
</appender>
打印控制台日志配置:
<appender name="CONSOLE-APPENDER" class="ch.qos.logback.core.ConsoleAppender">
<encoder>UTF-8</encoder>
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>[%date{yyyy-MM-ddHH:mm:ss}] %p %C %t %X{logPrefix}, %m%n</Pattern>
</layout>
</appender>
引用配置:
<root level="info">
<appender-ref ref="DEFAULT-APPENDER" />
<appender-ref ref="ERROR-APPENDER" />
</root>