Log4j Logging Strategy: Using multiple Loggers in same Class file
807606Jun 7 2007 — edited Jun 7 2007Hi,
In my application, I wanted to use logging this way:
1. Use general log4j logging statement which goes to a project log file (It works fine). For e.g logger.getLogger( "ToolCli.class" );
2. A special log which needs to be written to a separate file. Here we are not concerned about the log levels. We need only one level.
Basically we need to log the summary of a particular transaction of our application. And those log statement for that particular transaction would come from different Java class files.
To achive this I create another unique named logger and get this logger in the classes wherever I wanted to use it. This would result in having
2 Logger.getLogger in my class files. It solves my purpose but just wondering if my approach is fine?
Please provide if you have any comments/suggestions. Thanks.
Below is the sample code snippet:
public class ToolCli
{
private static Logger logger = Logger.getLogger( "ToolCli.class );
private static Logger statusLogger = Logger.getLogger( "JobStatus" );
....
void myMethod() {
statusLogger.info("Started Job...");
}
void myMethod2() {
logger.info("something to be logged...");
}
}
public class JobHandler
{
private static Logger logger = Logger.getLogger( "JobHandler.class );
private static Logger statusLogger = Logger.getLogger( "JobStatus" );
....
void myMethod() {
statusLogger.info("Aborted Job...");
}
void myMethod2() {
logger.info("something to be logged...");
}
}
In log4j.xml apart from root logger JobStatus is defined as :
<logger name="JobStatus" additivity="false" >
<level value="DEBUG" />
<appender-ref ref="job"/>
</logger>
<root>
<appender-ref ref="ConsoleAppender"/>
<appender-ref ref="SyncTool"/>
</root>