logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Curt Arnold <carn...@apache.org>
Subject Re: method level logger, possible?
Date Thu, 11 Oct 2007 21:06:38 GMT

On Oct 11, 2007, at 2:47 PM, orko wrote:

> Thanks for the reply. I have tried this, but unfortunately it did  
> not work. May be I am doing something wrong. A sample of my  
> approach is given below.
> Regards,
> Orko
>   <logger name="test.first.Another.main">
>       <level value="TRACE"/>
>     <appender-ref ref="CONSOLE"/>
>   </logger>
>    <root>
>       <priority value="ERROR" />
>       <appender-ref ref="CONSOLE"/>
>    </root>

As previously mentioned, you are attaching the CONSOLE logger at two  
places in the configuration which means that you will get the message  
at each place that the conditions are satisfied.  So an ERROR or  
higher logged to "test.first.Another.main" will show up twice, a  
TRACE, DEBUG, INFO or WARN logged there will show up once and an  
ERROR logged anywhere else will show up once.

> It supposed to show all logs from main, and error logs otherwise.  
> (If my understanding is right). but I am getting following output  
> for %5p [%c] [%C.%M] (%F:%L) - %m%n format:
> ERROR [test.first.Another] [test.first.Another.main] (Another.java: 
> 19) - error message
> ERROR [test.first.Another] [test.first.Another.testAMethod]  
> (Another.java:34) - another error Msg

That appears like you are logging to the same "test.first.Another"  
logger.  If you were logging to distinct loggers, then the logger  
name in the first bracket would differ.

> Matthew Kemp <mattkemp@gmail.com> wrote: One solution (although I'm  
> not sure about the performance impacts) would be
> to create loggers that are children of the class logger. For  
> example if you
> had a class 'Bar' in package 'foo', the your logger for the class  
> would be '
> foo.Bar'. If Bar has a method bar() and baz() you could create a  
> loggers '
> foo.Bar.bar' and 'foo.Bar.baz' that could each have their own level.

Logger names are only associated with class names by convention.  The  
string used to create the logger is the only significant issue and  
that string may or may not be associated with a class name.  The  
Logger.getLogger(Class) method is only a convenience and calls the  
getName() method of the class to get its name.

> package test.first;
> //imports
> public class Another {
>   //alternatively you could use Logger.getLogger 
> ("test.first.Another");
>   private static final Logger classLogger = Logger.getLogger 
> (Another.class);
>   //I'm assuming you're main method is this
>   public static void main(String args[]) {
>     final Logger methodLogger = Logger.getLogger 
> ("test.first.Another.main");
>     methodLogger.info("error message from main");
>   }
>   public void test() {
>     final Logger methodLogger = Logger.getLogger 
> ("test.first.Another.test");
>     methodLogger.info("error message from test");
>   }
>   public void testClassLogger() {
>     classLogger.info("error message from class");
>   }
> }
> You should only see the "error message from main" get logged. Hope  
> this
> helps.

The call to Logger.getLogger() is relatively expensive.  But there is  
nothing stopping you from creating multiple logger members in the  
class, like:

public class Another {
     private static final Logger logger = Logger.getLogger 
     private static final Logger testMethodLogger = Logger.getLogger 
     private static final Logger otherMethodLogger = Logger.getLogger 

    public static final main(String[] args) {
	logger.info("Hello, World");

    public void test() {
        testMethodLogger.info("Another message");

If you used the variable name logger at lot in your existing code,  
then you could avoid having a static member named logger and assign a  
local variable logger with the appropriate method level logger.

    public void test() {
         private final Logger logger = testMethodLogger;
         logger.info("Another message");

Again, associating logger names with class names is only a convention  
that you are free to use or disregard at your discretion.

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

View raw message