10
The Log4E logging plug-in -Gagandeep Singh

Log4e

Embed Size (px)

Citation preview

Page 1: Log4e

The Log4E logging plug-in

-Gagandeep Singh

Page 2: Log4e

What is logging good for?

• Tracing program execution during development• Debugging• Providing an audit trail for a program• Recording soft-errors (lost connections, etc.) for

monitoring performance and troubleshooting• A logging framework lets you use different logging

levels– You can turn different types of messages on and off– You can send different types of messages to different

destinations

Page 3: Log4e

Adding logging code

• Replaces System.out.println() calls throughout• Don’t need to be removed• Like adding System.out.println() calls, it’s

somewhat tedious to add logging code• Utility of logging code is improved if it’s

consistent—logs can be searched with scripts, for example

Page 4: Log4e

The Log4E plug-in

• Provides wizards that automate the addition of logging code

• Add import and declaration for logger• Insert logging at start and end of methods• Insert logging of variables and method

parameters

Page 5: Log4e

What Log4E doesn’t do

• Log4E does not provide a logging framework• Doesn’t configure your framework• Doesn’t make a fresh pot of coffee

Page 6: Log4e

Choosing a logging framework• Log4E supports three logging frameworks:– Log4J– Java 1.4 Logging– Jakarta Commons Logging

• Log4J is the original logging framework that Java 1.4 logging is modeled after

• Jakarta Commons Logging is actually a wrapper for logging frameworks

• We’ll use Log4J in this example because it’s mature, well-documented and supported, and easier to configure

Page 7: Log4e

Installing Log4J

• Download a zip or compressed tarfile from: Apache.org website

• Uncompress to a local directory• In your Eclipse project’s properties– Locate Java Build Path– On Libraries page, click on Add External Jars– Browse for your Log4J directory, and locate the log4J Jar

file in the /dist/lib directory– Press Open, followed by OK

Page 8: Log4e

Create a Log4J configuration file

• The easiest way to configure Log4J is to add a log4j.properties file to your source directory.

• There are many options available which we won’t cover here, but essentially you need to define:– A logger– An appender– A pattern layout

• The following example uses the default root logger, appends to the console and prints the date, time, message priority, thread and message

Page 9: Log4e

A sample log4j.properties file

# Logger

log4j.rootLogger=DEBUG, ConApp

# Appender

log4j.appender.ConApp=org.apache.log4j.ConsoleAppender

# PatternLayout

log4j.appender.ConApp.layout=org.apache.log4j.PatternLayout

log4j.appender.ConApp.layout.ConversionPattern=%d [%t] %-5p %c - %m%n

Page 10: Log4e

Demo

• Create a simple Java application• Ensure log4j is on class path• Use Log4E to add several types of logging

messages• Run program