Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you create log files in log4j per program execution?

I am currently using the DailyRollingFileAppender Class in log4j to do daily log file appending but I want to have the log files separated in the following format:

DATA.log.<date>_<time>_<random_#>

This should be done once per program execution so I end up with something like...

DATA.log.2011-01-13_12-46-38_<26>
DATA.log.2011-01-13_12-46-38_<79>
DATA.log.2011-01-13_12-46-38_<590>

Where different log files from different environments can be pooled together.

Is there anyway to do this without extending the FileAppender Class? At least, is there a way to do:

DATA.log.<date>_<time>_<sequential_#>.log

Thanks

Edit: I am already using DailyRollingFileAppender to get something like DATA.log.2011-01-13. What I want to know how to do is get the log file to rollover after each program execution (or before each program execution) and add a random numeric string at the end.

like image 278
Kevin Avatar asked Jan 14 '11 14:01

Kevin


People also ask

What is log4j and how generate log files in selenium?

Log4j is a logging framework written in Java that provides an easy way for logging in Selenium. In a nutshell, the framework gives out information about everything that goes on during the software execution. Log4j also provides insight into anything that may have gone wrong during software execution or automation.

Where is the log4j log file?

The Log4j logging settings are stored in the file app_data /conf/server/log4j. properties, where app_data is the application data folder. You can edit this file directly on the server or open it by clicking Settings > Logging.


1 Answers

have a look at : Setting a log file name to include current date in Log4j

EDIT : Add this class to your project, and use it as appender :

import java.util.Random;

import org.apache.log4j.DailyRollingFileAppender;

public class MyAppender extends DailyRollingFileAppender {    
    @Override
    public void  setFile(String fileName) {
        if (fileName.indexOf("%rnd") >= 0) {
            Random r = new Random();
            fileName = fileName.replaceAll("%rnd", Integer.toString(r.nextInt()));
        }
        super.setFile(fileName);
    }
}

Then just set your appender's filename to something like : filename.%rnd.log

log4j.appender.R=MyAppender.MyAppender
log4j.appender.R.File=.\\test.%rnd.log
like image 147
Twister Avatar answered Oct 28 '22 14:10

Twister