logging-log4j-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Sheward, Barry" <Barry.Shew...@Sequation.com>
Subject methods to always log, regardless of the current priority
Date Wed, 28 Jan 2004 02:43:18 GMT
Hi,

We use Log4J extensively and find that sometimes we always want to log a message, but the
message is simply for informational purposes. Using Logger/Category.fatal() would potentially
frighten users, but using Logger/Category.info() may not show up if they have reconfigured
logging.

While it would be possible to save the priority for the category, change the priority for
the category, log the message and then restore the priority for the category, this seemed
very inefficient both in terms of lines of code and run-time performance.

So, I developed the following four (incredibly simple) methods, which regardless of the current
priority of the Logger/Category, will always log the message. The diff is from the 1.2.8 source.

I hereby donate this code to the Apache Software Foundation. Please consider adding these
methods to Log4J.

Regards,

Barry Sheward


1037a1038,1111
>
>       /**
>     Log a message object with the {@link Priority#INFO INFO} priority
>     regardless of whether <code>INFO</code> output is enabled for
>     this category.
>
>     <p>This method converts the message object
>     (passed as parameter) to a string by invoking the appropriate
>     {@link ObjectRenderer}. It then proceeds to call all the
>     registered appenders in this category and also higher in the
>     hierarchy depending on the value of the additivity flag.
>
>     <p><b>WARNING</b> Note that passing a {@link Throwable} to this
>     method will print the name of the <code>Throwable</code> but no
>     stack trace. To print a stack trace use the {@link #debug(Object,
>     Throwable)} form instead.
>
>     @param message the message object to log. */
>   public
>   void always(Object message) {
>       forcedLog(FQCN, Priority.INFO, message, null);
>   }
>
>
>   /**
>    Log a message object with the <code>INFO</code> priority including
>    the stack trace of the {@link Throwable} <code>t</code> passed as
>    parameter regardless of whether <code>INFO</code> priority output
>    is enabled for this category.
>
>    <p>See {@link #always(Object)} form for more detailed information.
>
>    @param message the message object to log.
>    @param t the exception to log, including its stack trace.  */
>   public
>   void always(Object message, Throwable t) {
>       forcedLog(FQCN, Priority.INFO, message, t);
>   }
>
>     /**
>     Log a message object with the specified priority
>     regardless of whether the specified priority output is enabled for
>     this Category.
>
>    <p>See {@link #always(Object)} form for more detailed information.
>
>     <p><b>WARNING</b> Note that passing a {@link Throwable} to this
>     method will print the name of the <code>Throwable</code> but no
>     stack trace. To print a stack trace use the {@link #debug(Object,
>     Throwable)} form instead.
>
>     @param message the message object to log.
>     @param priority the priority to log the message at. */
>   public
>   void always(Object message, Priority priority) {
>       forcedLog(FQCN, priority, message, null);
>   }
>
>
>   /**
>    Log a message object with the specified priority including
>    the stack trace of the {@link Throwable} <code>t</code> passed as
>    parameter regardless of whether specified priority output
>    is enabled for this Category.
>
>    <p>See {@link #debug(Object)} form for more detailed information.
>
>    @param message the message object to log.
>    @param t the exception to log, including its stack trace.
>    @param priority the priority to log the message at. */
>   public
>   void always(Object message, Throwable t, Priority priority) {
>       forcedLog(FQCN, priority, message, t);
>   }

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


Mime
View raw message