歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

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 name="FILEOUT" class="org.apache.log4j.DailyRollingFileAppender">

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

        <param name="Encoding" value="UTF-8" />

        <layout class="org.apache.log4j.PatternLayout">

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

        </layout>

        <filter class="org.apache.log4j.varia.LevelRangeFilter">

            <param name="LevelMin" value="DEBUG" />

            <param name="LevelMax" value="DEBUG" />

        </filter>

    </appender>

3日志文件保留時間

通過下面配置,可以限制DailyRollingFileAppender保留的文件個數。

 

<param name="MaxBackupIndex"value="10"/>

Copyright © Linux教程網 All Rights Reserved