Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

One logfile per run with log4j

How do you configure log4j.properties to have exactly one logfile per run of an app. I've read that you should use a timestamp in the filename but that will create many files per run as time goes by.

I tried DailyRollingFileAppender and RollingFileAppender but can't find a way to configure exctly one log per run. The log should not be broken into multiple logs and it shouldn't be truncated and files of old runs should be preserved.

Each class has a static org.slf4j.Logger for it's own class name:

private static final Logger log = LoggerFactory.getLogger(Foo.class);

This is my current log4j.properties

log4j.rootLogger=error, RootAppender, RootErrorAppender

#log4j.logger.com.example=info, qspaBackendAppender, stderr
log4j.logger.com.example=info, qspaBackendAppender
log4j.additivity.com.example=true

#log4j.logger.com.example.util=trace, qspaBackendAppender, stderr
#log4j.additivity.com.example.util=true

log4j.appender.qspaBackendAppender=org.apache.log4j.DailyRollingFileAppender
log4j.appender.qspaBackendAppender.file=logs/qspaBackend.log
log4j.appender.qspaBackendAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.qspaBackendAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n

log4j.appender.stderr=org.apache.log4j.ConsoleAppender
log4j.appender.stderr.Target=System.err
log4j.appender.stderr.layout=org.apache.log4j.PatternLayout
log4j.appender.stderr.layout.ConversionPattern=%-5p %c{1}:%L - %m%n

log4j.appender.RootAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootAppender.file=logs/root.log
log4j.appender.RootAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n

log4j.appender.RootErrorAppender=org.apache.log4j.RollingFileAppender
log4j.appender.RootErrorAppender.file=logs/rootError.log
log4j.appender.RootErrorAppender.layout=org.apache.log4j.PatternLayout
log4j.appender.RootErrorAppender.layout.ConversionPattern=<%d{yyyy-MM-dd HH:mm:ss}> %-5p : %C{1} %c{2} : %m%n
log4j.appender.RootErrorAppender.threshold=error
like image 398
lilalinux Avatar asked Oct 02 '13 08:10

lilalinux


People also ask

How do I create a new log file in every run?

File with system properties name and some prefix text if you want. This will create a log file with current date time, something like this Log4jDemoApp-dd-MM-yyyy-hh-mm-ss. log every time we run the application. It will create the new file because for every run we are setting current date stamp in system properties.

Is log4j and SLF4J same?

Comparison SLF4J and Log4j Unlike log4j, SLF4J (Simple Logging Facade for Java) is not an implementation of logging framework, it is an abstraction for all those logging frameworks in Java similar to log4J. Therefore, you cannot compare both.


1 Answers

I had troubles retrieving which Udo Klimaschewski's answer Udy was referring so I put here my solution. log4j.properties:

# Root logger option
log4j.rootLogger=INFO, fileout

# Direct log messages to file
log4j.appender.fileout=org.apache.log4j.FileAppender
log4j.appender.fileout.File=/logs/myapp_${current.date}.log
log4j.appender.fileout.ImmediateFlush=true
log4j.appender.fileout.Threshold=debug
log4j.appender.fileout.Append=false
log4j.appender.fileout.layout=org.apache.log4j.PatternLayout
log4j.appender.fileout.layout.conversionPattern=%5p | %d | %m%n

Then put in the main class this block:

public class Starter {
    static{
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd hhmmss");
        System.setProperty("current.date", dateFormat.format(new Date()));
    }
like image 128
Tobia Zambon Avatar answered Oct 12 '22 22:10

Tobia Zambon