Petr Nechvátal
Petr Nechvátal

Reputation: 89

Transitioning from log4net to log4j 2

I am working on a project in java which uses log4j2 and want to setup logging the same as I had in older project in .NET using log4net.

There are some things I cant figure out, how to do in the log4j2.

How can I force log4j2 appender to put the current date to the log file and create new log file every day? I tried to use TimeBasedTriggeringPolicy and it allowed me to put date in the name of older logs but the current ones never have the date. I tried using %d{yyyyMMdd} in fileName but it did not work. Is there some eqivalent to the datePattern in log4j2?

Here is the original appender from .NET project.

<appender name="DebugRollingFileAppender" type="log4net.Appender.RollingFileAppender">
    <lockingModel type="log4net.Appender.FileAppender+MinimalLock" />
    <file value="c:\ConcertCTS\Logs\" />
    <appendToFile value="true" />
    <datePattern value="yyyyMMdd'_debug.log'" />
    <staticLogFileName value="false" />
    <encoding value="utf-8" />
    <maximumFileSize value="50MB" />
    <rollingStyle value="Composite" />
    <maxSizeRollBackups value="-1" />
    <layout type="log4net.Layout.PatternLayout">
        <conversionPattern value="%date{hh:mm:ss.fffzzz} [Thread: %-2thread] %-5level - %message%newline%exception" />
    </layout>
</appender>

And here is what I came up with for log4j2.

    <RollingFile name="DebugRollingAppender" fileName="/EnvoyLogs/debug.log"
        filePattern="/EnvoyLogs/%d{yyyyMMdd}_debug.%i.log">
        <PatternLayout>
            <Pattern>%d [%t] %p %c{8.} %m%n</Pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy />
            <SizeBasedTriggeringPolicy size="50 MB" />
        </Policies>
        <DefaultRolloverStrategy max="100"
            compressionLevel="0" />
    </RollingFile>

I also dislike that it puts number one on my older logs I know it is the %i in filePattern what does this but I do not want to put it away as I need logs to be splited and numbered when they exceed certain size. Can I do something with that?

Thanks in advance!

Upvotes: 0

Views: 435

Answers (1)

Remko Popma
Remko Popma

Reputation: 36754

Log4j2 currently (v 2.1) cannot do what you describe. I recommend raising a feature request on the log4j2 Jira issue tracker. I like the idea of having the date in the file name from the beginning, makes perfect sense.

Upvotes: 1

Related Questions