Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getting class name for logging

Tags:

java

logging

To log errors for a given class, I'm accessing the class name like so: Is this a 'good' way to return the class name as a String, so it can be used for logging?

private static final String CLASS_NAME = MyClass.class.getName();
logger.error("Error occurred in "+CLASS_NAME);
like image 357
blue-sky Avatar asked Jun 20 '12 08:06

blue-sky


1 Answers

Java 8 Solution

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.lang.invoke.MethodHandles;
...

private final static Logger LOG = 
                     LoggerFactory.getLogger(MethodHandles.lookup().lookupClass());
like image 123
MariuszS Avatar answered Nov 11 '22 17:11

MariuszS