手机版
你好,游客 登录 注册
背景:
阅读新闻

Log4j日志文件常用配置

[日期:2013-02-25] 来源:Linux社区  作者:Linux [字体:大 中 小]

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

linux
相关资讯       Log4j  Log4j配置文件 
本文评论   查看全部评论 (0)
表情: 表情 姓名: 字数

       

评论声明
  • 尊重网上道德,遵守中华人民共和国的各项有关法律法规
  • 承担一切因您的行为而直接或间接导致的民事或刑事法律责任
  • 本站管理人员有权保留或删除其管辖留言中的任意内容
  • 本站有权在网站内转载或引用您的评论
  • 参与本评论即表明您已经阅读并接受上述条款