Re: Printing method class and line number
Saturday, September 10, 2005 04:27 PM
I follow a convention where in each class I get a 'private static transient Logger = Logger.getLogger(MyClassName.class);'.
Then in each method, I declare 'final String myLogName = this.getClass().getName() + ".myMethodName"; '. Or if the method is static, it has to be 'final String myLogName = MyClassName.class.getName() + ".myMethodName";'.
And finally, when I actually want to log something inside the method, I do 'logger.debug(myLogName + " blah blah blah.");'.
What comes out in the log is a very long line, but it includes the logger's name, which is actually the name of the class (fully qualified with package prefix) that got the logger, plus it also contains the name of the method, with its fully qualified class too.
There are cases where having both of these on the log line provides interesting information, e.g. when the same methods from common parent classes are invoked in multiple different subclasses, etc.
For getting a line number, I think you may have to do an ugly hack like throwing an Exception and catching it yourself and giving it to the logger as the 2nd arg so the stack trace gets logged.
('logger.debug(myLogName + " info on my current stack trace and line number:", e);' where "e" is the Exception that you threw and caught yourself)