單純的SpringMVC 整合log4j (slf4j)
單純的springMVC整合log4j
1.載入所需要的jar
2.web.xml配置新增:
<context-param>
<param-name>log4jConfiguration</param-name>
<param-value>/WEB-INF/config/log4j2.xml</param-value></context-param>
其中 log4j2.xml 為具體配置檔名稱。
3.log4j2.xml 檔案內容:
<?xml version="1.0" encoding="utf-8" ?>
<Configuration status="off" monitorInterval="1800">
<properties>
<property name="LOG_HOME">D:\logs</property>
<property name="ERROR_LOG_FILE_NAME">error</property>
</properties>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d %-5p (%F:%L) - %m%n" />
</Console>
<RollingRandomAccessFile name="ErrorLog"
fileName="${LOG_HOME}/${ERROR_LOG_FILE_NAME}.log"
filePattern="${LOG_HOME}/${ERROR_LOG_FILE_NAME}.log.%d{yyyy-MM-dd}.gz">
<PatternLayout pattern="%d %-5p (%F:%L) - %m%n"/>
<Policies>
<TimeBasedTriggeringPolicy/>
<SizeBasedTriggeringPolicy size="100MB"/>
</Policies>
<DefaultRolloverStrategy max="20"/>
</RollingRandomAccessFile>
</Appenders>
<Loggers>
<logger name="org.springframework.core" level="info"/>
<logger name="org.springframework.beans" level="info"/>
<logger name="org.springframework.context" level="info"/>
<logger name="org.springframework.web" level="info"/>
<logger name="org.newcih" level="error" includeLocation="true" additivity="false">
<appender-ref ref="ErrorLog"/>
<appender-ref ref="Console"/>
</logger>
<root level="info" includeLocation="true">
<appender-ref ref="Console"/>
</root>
</Loggers>
</Configuration>
4.測試使用:
在需要的類中新增:
private Logger logger = LoggerFactory.getLogger(TestController.class);
import 類如下:
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
至此,配置整合完畢,在需要的地方使用:
logger.info("---------------------aaaaaaaaaaaaaaaaaaaaaaaaaa-------------------------");