logging-log4j-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Thorbjørn Ravn Andersen <thunderax...@gmail.com>
Subject Re: HOw to replace the system.out.println
Date Tue, 02 Sep 2008 15:34:21 GMT
MY POI skrev  den 02-09-2008 14:34:
> Thanks a lot..but here we are having hundreads of classes..
>
> in this case any solution is there
>   
This might help you getting started:

helpers/FooBar.java
=====
package helpers;

public class FooBar {
    public static void main(String[] args) {
        System.out.println("Hello World");
        System.out.println("Hello World");
        org.slf4j.LoggerFactory.getLogger(Me.callerClass()).info("Hello 
World");
        org.slf4j.LoggerFactory.getLogger(Me.callerClass()).info("Hello 
World");
    }
}
=====

helpers/Me.java
====
package helpers;

public class Me {

    public static String callerClass() {
        StackTraceElement[] stackTrace = 
Thread.currentThread().getStackTrace();
        int level = 0;
        while 
(stackTrace[level].getClassName().equals(Me.class.getCanonicalName()) == 
false) {
            level = level + 1;
        }
        // level is now *HERE*
        StackTraceElement element = stackTrace[level + 1];
        return element.getClassName();
    }
}
====

When FooBar is run, it generates this output:

===
Hello World
Hello World
0 INFO  helpers.FooBar Hello World
0 INFO  helpers.FooBar Hello World
===

Hence you should be able to globally replace instances of 
"System.out.println" / System.err.println with
"org.slf4j.LoggerFactory.getLogger(helpers.Me.callerClass()).info" in 
your text files and have something working. 

This requires Java 5 and is slower than actually creating a static 
logger instance but allows you to postpone the actual nitty gritty stuff 
for later.

-- 
  Thorbjørn Ravn Andersen          "... plus... Tubular Bells!"


---------------------------------------------------------------------
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