Java logger that automatically determines caller's class name

Viewed 75069
public static Logger getLogger() {
    final Throwable t = new Throwable();
    final StackTraceElement methodCaller = t.getStackTrace()[1];
    final Logger logger = Logger.getLogger(methodCaller.getClassName());
    logger.setLevel(ResourceManager.LOGLEVEL);
    return logger;
}

This method would return a logger that knows the class it's logging for. Any ideas against it?

Many years later: https://github.com/yanchenko/droidparts/blob/master/droidparts/src/org/droidparts/util/L.java

21 Answers

Creating a stack trace is a relatively slow operation. Your caller already knows what class and method it is in, so the effort is wasted. This aspect of your solution is inefficient.

Even if you use static class information, you should not fetch the Logger again for each message. From the author of Log4j,Ceki Gülcü:

The most common error in wrapper classes is the invocation of the Logger.getLogger method on each log request. This is guaranteed to wreak havoc on your application's performance. Really!!!

This is the conventional, efficient idiom for getting a Logger is during class initialization:

private static final Logger log = Logger.getLogger(MyClass.class);

Note that this gives you a separate Logger for each type in a hierarchy. If you come up with a method that invokes getClass() on an instance, you will see messages logged by a base type showing up under the subtype's logger. Maybe this is desirable in some cases, but I find it confusing (and I tend to favor composition over inheritance anyway).

Obviously, using the dynamic type via getClass() will require you to obtain the logger at least once per instance, rather than once per class like the recommended idiom using static type information.

I guess it adds a lot of overhead for every class. Every class has to be 'looked up'. You create new Throwable objects to do that... These throwables don't come for free.

Assuming you are keeping static refs to the loggers, here's a standalone static singleton:

public class LoggerUtils extends SecurityManager
{
    public static Logger getLogger()
    {
        String className = new LoggerUtils().getClassName();
        Logger logger = Logger.getLogger(className);
        return logger;
    }

    private String getClassName()
    {
        return getClassContext()[2].getName();
    }
}

Usage is nice and clean:

Logger logger = LoggerUtils.getLogger();

For every class that you use this with, you're going to have to look up the Logger anyway, so you might as well just use a static Logger in those classes.

private static final Logger logger = Logger.getLogger(MyClass.class.getName());

Then you just reference that logger when you need to do your log messages. Your method does the same thing that the static Log4J Logger does already so why reinvent the wheel?

Then the best thing is mix of two .

public class LoggerUtil {

    public static Level level=Level.ALL;

    public static java.util.logging.Logger getLogger() {
        final Throwable t = new Throwable();
        final StackTraceElement methodCaller = t.getStackTrace()[1];
        final java.util.logging.Logger logger = java.util.logging.Logger.getLogger(methodCaller.getClassName());
        logger.setLevel(level);

        return logger;
    }
}

And then in every class:

private static final Logger LOG = LoggerUtil.getLogger();

in code :

LOG.fine("debug that !...");

You get static logger that you can just copy&paste in every class and with no overhead ...

Alaa

I prefer creating a (static) Logger for each class (with it's explicit class name). I than use the logger as is.

You don't need to create a new Throwable object. You can just call Thread.currentThread().getStackTrace()[1]

Google Flogger logging API supports this e.g.

private static final FluentLogger logger = FluentLogger.forEnclosingClass();

See https://github.com/google/flogger for more details.

A nice way to do this from Java 7 onwards:

private static final Logger logger = LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());

The logger can be static and that fine. Here its using the SLF4J API

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

But in principal can be used with any logging framework. If the logger needs a string argument add toString()

Simple and trivial OLD SCHOOL:

Just create your own class and pass there class name, method name + comment (if class /method changed they're refactored automatically Shift+F6)

public class MyLogs {    
    public static void LOG(String theClass, String theMethod, String theComment) {
        Log.d("MY_TAG", "class: " + theClass + " meth : " + theMethod + " comm : " + theComment);
    }
}

and just use it anywhere in the app (no context required, no initialzation, no extra libs and no look up) - can be used for any programing language!

MyLogs.LOG("MainActivity", "onCreate", "Hello world");

this will print in your console:

MY_TAG class: MainActivity meth: onCreate comm: Hello world

Why not?

public static Logger getLogger(Object o) {
  final Logger logger = Logger.getLogger(o.getClass());
  logger.setLevel(ResourceManager.LOGLEVEL);
  return logger;
}

And then when you need a logger for a class:

getLogger(this).debug("Some log message")

This mechanism puts in a lot of extra effort at runtime.

If you use Eclipse as your IDE, consider using Log4e. This handy plugin will generate logger declarations for you using your favourite logging framework. A fraction more effort at coding time, but much less work at runtime.

Unless you really need your Logger to be static, you could use

final Logger logger = LoggerFactory.getLogger(getClass());
Related