Skip to content

Instantly share code, notes, and snippets.

@wblakecaldwell
Last active December 27, 2015 18:09
Show Gist options
  • Save wblakecaldwell/7367755 to your computer and use it in GitHub Desktop.
Save wblakecaldwell/7367755 to your computer and use it in GitHub Desktop.
Java Logger Factory that uses the stack trace to infer the logger name, allowing copy/paste.
package net.blakecaldwell.safehibernate;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
* Factory for log messages, picking the enclosing class as the
* logger. This is safe from copypasta.
*
* Use: private static Logger logger = ClassLoggerFactory.make();
*/
public class ClassLoggerFactory
{
/**
* Make a logger for the calling class.
*/
public static Logger make()
{
Throwable t = new Throwable();
StackTraceElement caller = t.getStackTrace()[1];
return LoggerFactory.getLogger(caller.getClassName());
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment