logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jacob Kjome <h...@visi.com>
Subject Re: Name of the class showing up in logs
Date Mon, 07 Jan 2008 05:32:57 GMT


Yaakov Chaikin wrote:
> Hi,
> 
> I am using log4j 1.2.14.
> 
> I have a class where I've declared the logger as follows:
> protected Logger logger = Logger.getLogger(SomeClass.class);
> 
> In one of the methods in that class, I pass the 'logger' to a static
> method of another class, like this:
> SomeOtherClass.doMethod(logger);
> 
> The output that I get is showing that the logging is coming from
> SomeOtherClass instead of from SomeClass, which is strange to me. I've
> been using log4j for a long time, but it's possible that I've never
> noticed this before.
> 

I think you must be somehow confused.  Keep in mind that using FQCN of the class 
for the logger name is just a common convention.  A logger name is just an 
arbitrary String.  If you create a logger with a given name and you pass it to 
another method to use, it will use the name associated with that logger instance. 
  Log4j doesn't try to track the class the logging is coming from.  It just knows 
it's own logger name and uses it.  Whoops.  It's your PatternLayout.  Read more 
below...

> Is there a way to tell log4j to use the originally initialized class
> instead of the one it's executing from? I am not even 100% how it
> knows which class it's in. I was assuming that the logging is always
> done from the class that it was initialized with. Am I configuring
> something wrong?
> 
> My appender is configured as follows:
> 
>     <appender name="testing-stdout" class="org.apache.log4j.ConsoleAppender">
>         <layout class="org.apache.log4j.PatternLayout">
>             <param name="ConversionPattern"
>                 value="***-> %-5p %C{1} - %m%n"/>
>         </layout>
>     </appender>
> 

Oh, well that explains it.  You aren't spitting out the category/logger name. 
You're spitting out the class name.  You should be using a lower-case "c", for 
"category", instead of "C", for "class".  When the pattern layout uses this, Log4j 
throws an exception and parses the stack trace for every log call.  I hope you 
aren't doing this in production!  Please read the Javadoc for PatternLayout [1]. 
You probably want something like...

<param name="ConversionPattern" value="***-> %-5p %c{1} - %m%n"/>

[1] http://logging.apache.org/log4j/1.2/apidocs/org/apache/log4j/PatternLayout.html


Jake

> 
> Any help would be appreciated.
> 
> Thanks,
> Yaakov.
> 
> ---------------------------------------------------------------------
> 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


Mime
View raw message