Wednesday, December 12, 2012

Logging Best Practices


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:

        set JAVA_OPTS=%JAVA_OPTS% -Dlog.path=c:\logs\temp\


Some other log4j best practices are here : http://juliusdavies.ca/logging.html

Hibernate : Enable Logging of the SQL Statements

Define a seperate appender

         name="SQL" class="org.apache.log4j.DailyRollingFileAppender">
                 name="File" value="c:/logs/sql.log" />
                 name="Append" value="false" />
                 name="Threshold" value="TRACE" />
                 name="DatePattern" value="'.'yyyy-MM-dd" />
                 class="org.apache.log4j.PatternLayout">
                         name="ConversionPattern" value="%d{ISO8601} %-5p [%c] %m%n" />
                
        
define the log categories
         name="org.hibernate.type">
                 value="TRACE" />
                 ref="SQL"/>
        
         name="org.hibernate.SQL">
                 value="TRACE" />
                 ref="SQL"/>
        

Basic Git Commands

Creating a local git repository

git init

Adding your changes to the local git repository

git add .

git commit -m"initial import"

Tagging your changes to the remote server

git tag -a v1.1 -m "Version 1.1 is waiting for review"

Commiting changes to the remote server

git push origin master

Commiting tags to the remote server

git push --tags


LinkWithin

Related Posts Plugin for WordPress, Blogger...