Including log file name in log entry in Log4j

I have a requirement for the name of the log file in the log entry itself.

For example, the final name of the log file is something like the trx_log.2014-09-22-12-42

log entries I am printing, this log should have the same name. Below is an example of a log entry.

123456|test value|xyz|trx_log.2014-09-22-12-42


I am using Log4j DailyRollingFileAppender

to print a log at the moment. Is there any way I can implement this requirement using some log4j / logback configuration.


source to share

2 answers

Not that I know.

But there is still a solution: write your own custom extension DailyRollingFileAppender


Please note that although the filename will only be available to your custom application: in case you want to use such information in another application (the only use case, I can think this might be useful), you need a more confusing solution using a shared data store (shared memory, filesystem, database, etc.), with the simplest solution being a static member of the newly created application. In this case, another appender (lat say Console) needs to be added to add new information to the log statement.



Use this method logger.getName ()

logger.log (Level.SEVERE, "Exception at" + e.getMessage () + logger.getName ());



All Articles