Getting class name for logging
If you initialize the logger this way:
private static Logger logger = Logger.getLogger(MyClass.class.getName())>
then the name of the class will be present in every logging event, you don't need to explicitly put it in every log call:
logger.error("Error occured while doing this and that");
The you can configure the logging service (in logging.properties in case of java.util.logging
, or log4j.properties if you use Apache log4j) to include the class name in every log message.
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());
You can set up your logging parameters in log4j.xml itself.
For exp -
<appender name="swcd-web" class="org.apache.log4j.DailyRollingFileAppender">
<param name="Threshold" value="DEBUG"/>
<param name="Append" value="true"/>
<param name="File" value="${catalina.home}/logs/swcd-web.log"/>
<layout class="org.apache.log4j.PatternLayout">
<param name="ConversionPattern" value="%d [%t] %-5p %c - %m%n"/>
</layout>
</appender>
It would log exceptions like this in swcd-web.log file -
2012-05-23 16:34:51,632 [main] ERROR com.idc.sage.sso.dynamo.SsoDbStorage - cannot get configuration for max SSO age