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 orko <or...@yahoo.com> on 2007/10/11 20:38:59 UTC
method level logger, possible?
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
Re: method level logger, possible?
Posted by Matthew Kemp <ma...@gmail.com>.
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 <or...@yahoo.com> 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 <ma...@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.
>
> 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
> >
>
>
Re: method level logger, possible?
Posted by Curt Arnold <ca...@apache.org>.
On Oct 11, 2007, at 4:36 PM, Matthew Kemp wrote:
> @Curt
>
> Is the initial call to Logger.getLogger("foo") expensive or is
> every call to
> Logger.getLogger("foo") expensive?
>
Each call at least involves a Hashtable.get() call which would
involve a block on a mutex and hash table lookup. The first call
would also have additional overhead for creating and inserting the
node in the hierarchy. Depends on the rest of the method if that is
proportionately expensive or cheap.
If you have a function that is called frequently and where the log
requests do not typically satisfy the threshold, then it could be
highly beneficial to get the logger once and use it repeatedly. If
you have a function that is not called frequently or where most of
the log requests do satisfy the threshold or the method is time
consuming, then the proportional cost of the Logger.getLogger() call
might be inconsequential.
---------------------------------------------------------------------
To unsubscribe, e-mail: log4j-user-unsubscribe@logging.apache.org
For additional commands, e-mail: log4j-user-help@logging.apache.org
Re: method level logger, possible?
Posted by Matthew Kemp <ma...@gmail.com>.
@Curt
Is the initial call to Logger.getLogger("foo") expensive or is every call to
Logger.getLogger("foo") expensive?
On 10/11/07, Curt Arnold <ca...@apache.org> wrote:
>
>
> 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 <ma...@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
> (Another.class);
> private static final Logger testMethodLogger = Logger.getLogger
> ("test.first.Another.test");
> private static final Logger otherMethodLogger = Logger.getLogger
> ("test.first.Another.other");
>
> 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
>
>
Re: method level logger, possible?
Posted by Curt Arnold <ca...@apache.org>.
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 <ma...@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
(Another.class);
private static final Logger testMethodLogger = Logger.getLogger
("test.first.Another.test");
private static final Logger otherMethodLogger = Logger.getLogger
("test.first.Another.other");
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
Re: method level logger, possible?
Posted by orko <or...@yahoo.com>.
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 <ma...@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.
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
>
Re: method level logger, possible?
Posted by Matthew Kemp <ma...@gmail.com>.
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 <or...@yahoo.com> 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
>