/  11
 
Chapter 3 – Logging and Configuration, Part ILogging
Logging enables you to capture important events inside your program and can aid you during thedebugging process. It offers finer granularity and better control over the
System.out.println()
method. If you are only interested with IDK logging, you can skip ahead to section 2 of thischapter.Depending on which platform (.NET or Java), you can find a wealth of logging utilities. One of the leading tools is the Apache Log4J framework. (http://logging.apache.org/) It is based on theApache License, allowing you to use the Log4J library inside your software, including proprietary applications. There are also related projects for .NET, PHP, C#, C++, etc.
1
1Log4J
1.1Setup
To use the Log4J library, you must include the appropriate jar file. The latest version (at the timeof writing this guide) is log4-1.2.14.jar. It is about 359 KB in size. I just add it as a library in theIDE, allowing me to reuse it on several projects without additional setup effort. (You may alsoadd the API docs in the setup process by pointing to the appropriate folder or the API jar file.)
1
1
 
1.2Concep
The Log4J library introduces several important concepts:1.Logging granularity – Allowing you greater control over the log output than
System.out.println()
. (Debug < Info < Warn < Error < Fatal)a.Debug – Detailed application and parameter tracing. b.Info – Normal application event recordingc.Warn – Minor problemd.Error – Significant problem, but is not a showstopper.e.Fatal - Major problem, could potential crash your application.2.Appenders & Layout – Where to send your log output, appenders include,a.FileAppender – Subclass: RollingFileAppender, DailyRollingFileAppende b.ConsoleAppendec.JDBCAppender d.JMSAppender e.NTEventLogAppender f.SMTPAppender g.SocketAppender h.Nullappender,i.Etc.Layout lets you specify the format of your log out. e.g. Text output
log4j.appender.R.layout=org.apache.log4j.PatternLayout%d{dd MMM yyyy HH:mm:ss} -- %p -- %c -- %m%n----------Results in----------27 Sep 2006 18:00:22 -- INFO -- com.uhg.ovations.pbmui.InitServlet --InitServlet has been started.
XML output can be used with Chainsaw utility.
log4j.appender.R.layout=org.apache.log4j.xml.XMLLayout----------Results in----------<log4j:event logger="logging.LogMe" timestamp="1160422555484"level="INFO" thread="main"><log4j:message><![CDATA[LogMe has been started.]]></log4j:message></log4j:event><log4j:event logger="logging.LogMe" timestamp="1160422555484"level="DEBUG" thread="main"><log4j:message><![CDATA[calling debug method.]]></log4j:message>
3.Logger hierarchy – Loggers are stored in a hierarchical structure, e.g. com.foo.bar.Myclass.When you set the logging level (Info) on a package (com.foo.bar), it automatically flowdownward to its child, (com.foo.bar.Class1 and com.foo.bar.Class2), unless you explicitlyoverride it. There is always a root logger available,
Logger.getRootLogger()
.
1.3How to use Log4
Import the Log4J library.
//For Java class:
2
 
import org.apache.log4j.Logger;//For JSP:<%@page import="org.apache.log4j.*"%>
You can get a logger object by calling the
Logger.getLogger()
method.
// Log4J does not make assumption to have a default appender. Later wewill use a different Log4j Configurator.BasicConfigurator.configure();Logger ul = Logger.getLogger(“a descriptive string”);Logger ul = Logger.getLogger(Myclass.class);
You may start logging by invoking one the logging methods, e.g.
info(), fatal(), warn(),
… Youmay set logging level as well. Level object has predefined constants, e.g. info, fatal, warn, etc.
//Set logging level, e.g. Level.All, Level.ERROR, Level.INFO.ul.setLevel(Level.INFO);//Start loggingul.info(“This is my logging output.”);ul.warn(“This is a warning.”);ul.debug(“This debug message will not appear”);
By using BasicConfigurator, the logger writes output to the console. However, you can specify adifferent appender, e.g. FileAppender. The following code shows you how to use the
PropertyConfigurator 
class.
// You may use PropertyConfigurator to load a Log4J configuration file.// This tells the Log4J to watch the property file and reload when thefile has been modified.PropertyConfigurator.configureAndWatch(propertyfile);System.out.println("Loading the log4j property file: -> " +propertyfile);// After setting the Log4J configuraiton, you may use the logger.Logger ul = Logger.getLogger(Myclass.class);ul.inf(“logging to the output file specified by theconfiguration.
The Log4J configuration can use an external properties file. An example is shown below.
## Log for Java configuration don't change unless you know what you doing# The possible values here are debug, info, warn, error, fatal#log4j.rootCategory=info, R## Using Rolling File Appender#log4j.appender.R=org.apache.log4j.RollingFileAppender## This is the file that becomes the log file. Older log files are renamed asfileName.log.1 fileName.log.2 etc.#log4j.appender.R.File=C:\\MyDocuments\\g6_book\\java_src\\logging\\logs\\out.log## The maximum size of the log file, good idea to keep the size small.
3

Share & Embed

More from this user

Add a Comment

Characters: ...