You are viewing a plain text version of this content. The canonical link for it is here.
Posted to log4j-user@logging.apache.org by sa...@wipro.com on 2007/06/07 11:46:44 UTC

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>


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


Re: Logging Strategy: Using multiple Loggers in same Class file

Posted by Dave Levitt <da...@gmail.com>.
Wouldn't that application be the sort of thing to use 'nested
diagnostic context' on?

NDC.push("filterable trans start message");

and pop the context in the 'finally' after the commit or rollback.

On 6/7/07, sach.jain@wipro.com <sa...@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?
>
> 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
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


RE: public interface ErrorHandler extends OptionHandler

Posted by Kamal Ahmed <KA...@webMethods.com>.
Never mind, I found it

public class OnlyOnceErrorHandler implements ErrorHandler



-----Original Message-----
From: Kamal Ahmed [mailto:KAhmed@webmethods.com] 
Sent: Thursday, June 07, 2007 11:22 AM
To: Log4J Users List
Subject: public interface ErrorHandler extends OptionHandler 

Hi,

In Log4J version 1.2.13 , I could not find any implementation of
ErrorHandler. Is it upto the user to implement ErrorHandler interface?

Thanks,
-Kamal.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


JDBCAppender custom fields

Posted by "Shah, Tejas" <te...@bearingpoint.com>.
Hello,

I am using JDBCAppender to log information into a database. I need to
log several custom fields along with logging message and default fields
provided by JDBCAppender. I have searched about this issue and found out
a post from 2003 which does outline an alternative approach using
ExtendedPatternLayout. My question is if there is an easier way to
accomplish this. I mean easier than writing few lines of code which
would parse message string for fields and would just do a database
insert? 


Thanks for your help,

Tejas
***************************************************************************************************
The information in this email is confidential and may be legally privileged Access to this email by anyone other than the intended addressee is unauthorized. If you are not the intended recipient of this message, any review, disclosure, copying, distribution, retention, or any action taken or omitted to be taken in reliance on it is prohibited and may be unlawful. If you are not the intended recipient, please reply to or forward a copy of this message to the sender and delete the message, any attachments, and any copies thereof from your system.
***************************************************************************************************

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


public interface ErrorHandler extends OptionHandler

Posted by Kamal Ahmed <KA...@webMethods.com>.
Hi,

In Log4J version 1.2.13 , I could not find any implementation of
ErrorHandler. Is it upto the user to implement ErrorHandler interface?

Thanks,
-Kamal.

---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org


Re: Logging Strategy: Using multiple Loggers in same Class file

Posted by Maarten Bosteels <mb...@gmail.com>.
On 6/7/07, sach.jain@wipro.com <sa...@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
>
>