Last active
December 27, 2015 18:09
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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