First I'd make this minor change to your log4j config - to avoid double
logging:

<logger name="test.first.Another.main">
  <!-- I'm not sure about TRACE support, maybe you should try DEBUG or ALL
-->
  <level value="TRACE"/>
</logger>
<root>
  <priority value="ERROR" />
  <appender-ref ref="CONSOLE"/>
</root>

Here's how your class should be setup:

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.

Matt

On 10/11/07, orko <[EMAIL PROTECTED]> 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>
> 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
>
> Matthew Kemp <[EMAIL PROTECTED]> 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.
>
> On 10/11/07, orko  wrote:
> >
> > Hi all,
> > I am trying to implement log4j for my application. I can set the logger
> to
> > class level. But, is it possible to set logger for a particular method
> under
> > a class (I/user may not need to view all the logs from different
> methods)?
> > Thanks in advance,
> >
> > -Orko
> >
>
>

Reply via email to