Log4j日志文件常用配置

1日志级别热修改

web.xml

===============================================================================

<listener>

<listener-class>

org.springframework.web.util.Log4jConfigListener

</listener-class>

</listener>

<context-param>

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

<param-value>5000</param-value>

</context-param>

===============================================================================

Spring的Log4jConfiglistener类会调用Log4j的PropertyConfigurator.configureAndWatch(),将刷新间隔作为参数传入。

public static void initLogging(String location, long refreshInterval) throws FileNotFoundException {

String resolvedLocation =SystemPropertyUtils.resolvePlaceholders(location);

File file = ResourceUtils.getFile(resolvedLocation);

if (!file.exists()) {

throw new FileNotFoundException("Log4j config file [" +resolvedLocation + "] not found");

}

if(resolvedLocation.toLowerCase().endsWith(XML_FILE_EXTENSION)) {

DOMConfigurator.configureAndWatch(file.getAbsolutePath(),refreshInterval);

}

else {

PropertyConfigurator.configureAndWatch(file.getAbsolutePath(),refreshInterval);

}

2日志文件编码格式

<appender>

<param value="${vc.log.path}/cdai_ssh.log" />

<param value="UTF-8" />

<layout>

<param value="[CDAI_SSH]%d{yyy-MM-dd HH:mm:ss}%-5p%m%n" />

</layout>

<filter>

<param value="DEBUG" />

<param value="DEBUG" />

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:http://www.heiqu.com/30d43163ca74ca89d8a7f82b886541f4.html