Log4j Logging Strategy: Using multiple Loggers in same Class file

Hi,

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>

[1993 byte] By [sachin_sachina] at [2007-11-27 6:43:10]
# 1
yea, is there a question here?Is there a reason you chose Log4J over java.util.logging?
robtafta at 2007-7-12 18:13:42 > top of Java-index,Java Essentials,Java Programming...
# 2
Your approach sounds fine to me, I'd do the same.You need to decide between logger.getLogger( "ToolCli.class" ); and logger.getLogger(ToolCli.class);. I'd go for the version without the quotes.
OleVVa at 2007-7-12 18:13:42 > top of Java-index,Java Essentials,Java Programming...
# 3

Yeah that should work, and has the advantage of simplicity.

I'm not sure about making your loggers static though... mayhap's you'll run into contention problems with multiple instances of the object logging simultaneously on different threads logging using a single This.class as a lock.

Suck it and see.

Keith.

corlettka at 2007-7-12 18:13:42 > top of Java-index,Java Essentials,Java Programming...