Declaring a logger
private static final Logger LOG = Logger.getLogger(MyClassName.class.getName())
Log Levels
TRACE
In general, trace-level logging should be used only for tracing the flow of execution through a program and for flagging particular positions in a program (i.e. to ensure they have been reached). While you may want to trace every method of a class in early stages of development, you should ensure that you are only tracing key aspects of the flow when it is time to pass the code onto other developers. An overabundance of TRACE statements (especially in simple getter and setter methods) can hinder others rather than help them.
Entering a method
public void myMethod(){
if(LOG.isTraceEnabled()){
LOG.trace(">> myMethod()");
}
...
Exiting a method
....
if(LOG.isTraceEnabled()){
LOG.trace("<< myMethod()");
}
}
Constructors
public OntModelService(){
if(LOG.isTraceEnabled()){
LOG.trace("** OntModelService()");
}
}
DEBUG
Use DEBUG to report results to yourself and other developers
INFO
Use INFO to report results to Administrators and Users
WARN
Dangerious for the application but do not interup the execution
ERROR
Application can no longer continue to work correctly
USING ENVIRONMENT FILE in log4j.properties
1. Define a parameter in the log4.properties e.g.
log4j.appender.R=org.apache.log4j.RollingFileAppender log4j.appender.R.File=${log.path}/logs/myApp.log log4j.appender.R.MaxFileSize=500MB log4j.appender.R.MaxBackupIndex=1000 log4j.appender.R.layout=org.apache.log4j.PatternLayout log4j.appender.R.layout.ConversionPattern=%d{ISO8601}[ %-5p] - - [%X{sessionId}](%C.%M.java:%L) - %m%n
2. AND set the following JVM Parameter on the Server (this is JAVA_OPT in catalina script)
-Dlog.path="c:/temp/"
You can also set this variable as a JAVA_OPTS environement variable.This variable will be read by tomcat automatically and will be given to the JVM as parameter
Linux Example:
set JAVA_OPTS="$JAVA_OPTS -Dlog. path=/logs/temp/"
Windows Example:
Some other log4j best practices are here : http://juliusdavies.ca/logging.html