Logging

10 Tips for Application Logging


This blog is just fabulous for people who want to understand Application logging !!

10 tips for Application Logging – By Thomas Nurkiewicz

Regards,
The Nonsense Blogger !!

Advertisements

log4j Application using Maven


First, the pom.xml that describes the Application build.

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.arun.demo</groupId>
  <artifactId>DemoLogger</artifactId>
  <version>0.0.1-SNAPSHOT</version>

  <dependencies>
  	    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.6.1</version>
        <scope>runtime</scope>
    	</dependency>
  </dependencies>
</project>

Now the ‘log4j.properties’ file that should be placed in the ‘resources’ folder in the project.

#------------------------------------------------------------------------------
#
#  The following properties set the logging levels and log appender.  The
#  log4j.rootCategory variable defines the default log level and one or more
#  appenders.  For the console, use 'S'.  For the daily rolling file, use 'R'.
#  For an HTML formatted log, use 'H'.
#
#  To override the default (rootCategory) log level, define a property of the
#  form (see below for available values):
#
#        log4j.logger. =
#
#    Available logger names:
#      TODO
#
#    Possible Log Levels:
#      FATAL, ERROR, WARN, INFO, DEBUG, TRACE
#
#------------------------------------------------------------------------------

log4j.rootCategory=DEBUG, S, ALL, ERROR

log4j.logger.com.dappit.Dapper.parser=ERROR
log4j.logger.org.w3c.tidy=FATAL

#------------------------------------------------------------------------------
#
#  The following properties configure the console (stdout) appender.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.S = org.apache.log4j.ConsoleAppender
log4j.appender.S.layout = org.apache.log4j.PatternLayout
log4j.appender.S.layout.ConversionPattern = [%d{dd-MM-yyyy HH:mm:ss.SSS}] [%p] [%c]: %m%n

#------------------------------------------------------------------------------
#
#  The following properties configure the Daily Rolling File appender which includes all the log level messages.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.ALL = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ALL.File = C:/Users/Arun/workspace/DemoLogger/AppLog.log
log4j.appender.ALL.Append = true
log4j.appender.ALL.DatePattern = '.'yyyy-MM-dd
log4j.appender.ALL.layout = org.apache.log4j.PatternLayout
log4j.appender.ALL.layout.ConversionPattern = [%d{dd-MM-yyyy HH:mm:ss.SSS}] [%p] [%c{1}]: %m%n

#------------------------------------------------------------------------------
#
#  The following properties configure the Daily Rolling File appender which includes all the log level messages.
#  See http://logging.apache.org/log4j/docs/api/index.html for details.
#
#------------------------------------------------------------------------------
log4j.appender.ERROR = org.apache.log4j.DailyRollingFileAppender
log4j.appender.ERROR.File = C:/Users/Arun/workspace/DemoLogger/AppErrorLog.log
log4j.appender.ERROR.Append = true
log4j.appender.ERROR.Threshold = ERROR
log4j.appender.ERROR.DatePattern = '.'yyyy-MM-dd
log4j.appender.ERROR.layout = org.apache.log4j.PatternLayout
log4j.appender.ERROR.layout.ConversionPattern = [%d{dd-MM-yyyy HH:mm:ss.SSS}] [%p] [%c{1}]: %m%n

Now the ‘DemoLogger.java’ file which contains the demo logging statements.

package com.arun.demo.logger;

import org.apache.log4j.Logger;

public class DemoLogger {

	/* Creating the static logger */
	static Logger logger = Logger.getLogger(DemoLogger.class);

	public static void main(String args[]) {

		int a=3,b;

		logger.debug("Test Debug Message");
		try {

			logger.debug("Before Exception");
			b=a/0;
			System.out.println("b : "+b);
		}
		catch(Exception e) {

			logger.error("Exception occured :"+e.getMessage());
		}

	}
}

Here all the logs are printed out on the console. They also get written on the rolling file ‘AppLog.log”. The ‘error’ level logs get written to ‘AppErrorLog.log’

Find below, the ‘Output grab’ from the console.

[15-04-2011 01:32:47.763] [DEBUG] [com.arun.demo.logger.DemoLogger]: Test Debug Message
[15-04-2011 01:32:47.789] [DEBUG] [com.arun.demo.logger.DemoLogger]: Before Exception
[15-04-2011 01:32:47.822] [ERROR] [com.arun.demo.logger.DemoLogger]: Exception occured :/ by zero

Find more on log4j at http://logging.apache.org/log4j/1.2/manual.html

Thanks,

The nonsense logger 🙂 🙂