Log4j日志文件常用配置

本文详细介绍了如何使用Spring的Log4jConfigListener实现日志级别的热修改,设置日志文件的编码格式为UTF-8,并通过配置限制DailyRollingFileAppender保留的日志文件个数。

 

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"/>

  

转载于:https://www.cnblogs.com/xiaomaohai/archive/2013/02/15/6157769.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值