YuriR
YuriR

Reputation: 1311

How to configure log4j to roll by time,size and on start?

I need to configure logger to roll the logs by time (hourly or daily), by size and on start. I searched a lot and red log4j docs, now i am confused. Looks like it can be done by following ways:

  1. using Simon library
  2. creating custom classes
  3. using log4j 2 beta

I can't add new 3rd party libs to my project (log4j 2 beta is already added) so i consider the last two options. What exactly classes should i create if i go with the 2nd option? Should it be appender, rollingPolicy or triggeringPolicy?

Will log4j2 really support that?

Thanks for help, Yuri

Upvotes: 1

Views: 5039

Answers (1)

Remko Popma
Remko Popma

Reputation: 36844

I don't think you need to create any classes, you should be able to achieve what you want with configuration only. The Log4J2 documentation provides a number of examples. Here is a good place to start: http://logging.apache.org/log4j/2.x/manual/appenders.html#RollingFileAppender

If this is not sufficient, don't hesitate to ask a question on the log4j-2 user mailing list.

Config example:

<?xml version="1.0" encoding="UTF-8"?><configuration name="install" status="info">

<appenders>
    <!-- ################# InstallAppender ############################### -->
    <RollingFile name="InstallAppender"
                 fileName="${sys:installation.path}/installation/logs/post_install.log"
                 filePattern="${sys:installation.path}/installation/logs/post_install.log">
        <PatternLayout>
            <pattern>%d{dd/MM/yyyy HH:mm:ss} %-5p [%t] [%c{1}] %m%n</pattern>
        </PatternLayout>
        <Policies>
            <OnStartupTriggeringPolicy/>
        </Policies>
        <DefaultRolloverStrategy max="1"/>
    </RollingFile>
</appenders>

<loggers>
    <root level="info">
        <appender-ref ref="InstallAppender"/>
    </root>
</loggers>

Upvotes: 1

Related Questions