开发者

Log4j configuration: How to create a new folder for each day?

开发者 https://www.devze.com 2023-01-28 14:16 出处:网络
So, we are using Log4j\'s DailyRollingFileAppender to create our log files.This is really nice as it can create a new file for each day or hour.We\'d like the appender to create a new folder based on

So, we are using Log4j's DailyRollingFileAppender to create our log files. This is really nice as it can create a new file for each day or hour. We'd like the appender to create a new folder based on the day, then new log files based on the hour. Is this possible?

Here is our current configuration... currently set up to dump ALL log f开发者_运维知识库iles (created hourly) into someFolder. I'd guess we can do something fancy in the "File" param to do what we want:

    <appender name="PlainFileAppender" class="org.apache.log4j.DailyRollingFileAppender">
        <param name="File" value="someFolder/someFileName_plain.log" />
        <param name="DatePattern" value=".yyyy-MM-dd-HH" />
            <param name="ImmediateFlush" value="true" />
            <layout class="org.apache.log4j.PatternLayout">
            <param name="ConversionPattern"
                value="%d %-5p [%t] %c{1}: %m%n" />
        </layout> 
            <filter class="com.trgr.cobalt.infrastructure.logging.ClassFilter">
                <param name="classNameFilter" value="com.trgr.cobalt.infrastructure.logging.LogRecord" />
                <param name="acceptOnMatch" value="false" /> 
                <param name="neutrality" value="true" />
            </filter>
    </appender>


This is not possible with that appender, since it always appends the date to the specified filename (so you cannot specify some special filename to achieve your result) and does not create missing directories, even if you specify some fancy date pattern.

So you may write an appender on your own or find such a custom log4j appender somewhere. Log4j's successor, logback, has such an appender.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号