Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it possible to configure log4j to create a new file with every run of the application?

Tags:

java

log4j

For example, the first time I run an application (or immediately after I clear out the /logs directory), I want log4j to write the application's logs to a file called log.0. Then, I exit the application and restart it, I want the logs to be written to log.1. And so on.

I would like to keep this in the configuration file, although if I can't, I guess I could always do it in my application, when log4j is set up.

Is this possible? If so, how?

like image 890
Thomas Owens Avatar asked Sep 18 '09 13:09

Thomas Owens


People also ask

Does log4j create log file?

Following is a sample configuration file log4j. properties to generate log files rolling over at midday and midnight of each day. If you wish to have an XML configuration file, you can generate the same as mentioned in the initial section and add only additional parameters related to DailyRollingFileAppender.

How do I change the log4j configuration at runtime?

Use LogManager. resetConfiguration(); to clear the current config and configure it again. Another approach is to build a new appender and replace the old one with it (most appenders don't support changing their config).

How to create new log file every time you run Log4j?

But in Log4j, there is no FileAppender to create new log file every time you run your code. But we can use some hacks to create a new log file every time we run the code. Let us see how to do that. First, we need to create new System properties and need to load it with a current time stamp like this:

What is the use of Log4j?

Suppose today is 2012-11-04, and at midnight, log4j will back up the app.log file into app.log.2012-11-04, and the app.log file become logging for the new day, 2012-11-05, and so on. This is very useful when there is a need for tracking log files based on some interval of time.

Is there an XML configuration file equivalent to the above Log4j?

If you wish to have an XML configuration file equivalent to the above log4j.properties file, then here is the content: You can try log4j - Sample Program with the above configuration. You may want to write your log messages into multiple files for certain reasons, for example, if the file size reached to a certain threshold.

What time does Log4j roll over?

Roll over on the first day of each week depending upon the locale. Following is a sample configuration file log4j.properties to generate log files rolling over at midday and midnight of each day.


3 Answers

May be this is what you are looking for

http://veerasundar.com/blog/2009/08/how-to-create-a-new-log-file-for-each-time-the-application-runs/

**Edit:**I got one more solution! But no idea whether it works or not,but you can try http://www.mail-archive.com/[email protected]/msg02132.html

like image 51
Umesh Avatar answered Oct 13 '22 11:10

Umesh


Solution with log4j2:

        <RollingFile name="RollingFile" fileName="${log-path}/GScraper.log"
                 filePattern="${log-path}/GScraper_%d{yyyy-MM-dd}_%i.log">
        <ThresholdFilter level="WARN" onMatch="ACCEPT" onMismatch="DENY"/>
        <PatternLayout>
            <pattern>%level\t%d{yyyy-MM-dd HH:mm:ss} %c: %m%n</pattern>
        </PatternLayout>
        <Policies>
            <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            <SizeBasedTriggeringPolicy size="32 MB" />
            <OnStartupTriggeringPolicy/>
        </Policies>
    </RollingFile>

Note OnStartupTriggeringPolicy and %i in filePattern. This way, Log4j will create new log file with index each time you run the App.

like image 35
Nazar Ostryzhniuk Avatar answered Oct 13 '22 12:10

Nazar Ostryzhniuk


See logback's manual on uniquely named files (by timestamp).

like image 41
Ceki Avatar answered Oct 13 '22 10:10

Ceki