logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Maarten Bosteels" <mbosteels....@gmail.com>
Subject Re: Logging Strategy: Using multiple Loggers in same Class file
Date Thu, 07 Jun 2007 14:20:48 GMT
On 6/7/07, sach.jain@wipro.com <sach.jain@wipro.com> wrote:
>
>
> 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?


It's perfect :-)

Naming your loggers after your classes is a very common pattern.
But log4j is designed to support your use case too.

Maarten

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>
>
>
> The information contained in this electronic message and any attachments
> to this message are intended for the exclusive use of the addressee(s) and
> may contain proprietary, confidential or privileged information. If you are
> not the intended recipient, you should not disseminate, distribute or copy
> this e-mail. Please notify the sender immediately and destroy all copies of
> this message and any attachments.
>
> WARNING: Computer viruses can be transmitted via email. The recipient
> should check this email and any attachments for the presence of viruses. The
> company accepts no liability for any damage caused by any virus transmitted
> by this email.
>
> www.wipro.com
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
> For additional commands, e-mail: log4j-user-help@logging.apache.org
>
>

Mime
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message