Académique Documents
Professionnel Documents
Culture Documents
Course Schedule
Day 1 of 1
Log4j Concepts
-Logger
-Appender
-Layout
Installation of Log4j
Configuring Log4j
Demo examples on logging with Log4j , in core/web java apps
Lab exercises
Course Objectives
Understanding Logging with Log4j
Using Log4j within java applications
Understanding Logging
What is Logging
Why Logging is required
Logging requirements
What is Log4j
Log4j Concepts
Installation of Log4j
Configuring Log4j
Logging Examples
What is Logging
Logging is writing the state of a program at various stages of its
execution to some repository such as a log file
Logging requirements
An easy-to-use and efficient open source utility is required to
solve the Logging problems such as ..
- Logging , an overhead for the programmers
- Messy codes due to Logging statements
- Inserting log statements manually is tedious and timeconsuming
- Due to the ongoing upgrading and bug-fixing process for
application code ,managing the log statements in the code is
tedious
What is Log4j
Log4j is an open source logging API for Java
Log4j
- handles inserting log statements in application code and
managing them externally without touching application code,
by using external configuration files
- categorizes log statements according to user-specified
criteria and assigns different priority levels to these log
statements
What is Log4j
Log4j
- lets users choose from several destinations for log statements,
such as console, file, database, SMTP servers, GUI
components etc.
- facilitates creation of customized formats for log output and
provides default formats
Log4j Concepts
Log4j is comprised of three main components:
- Logger
- Appender
- Layout
The functionalities of each of these components are accessible
through Java classes of the same name
Users can extend these basic classes to create their own loggers,
appenders, and layouts
Logger - Example
MyClass is a class in com.foo.sampleapp application package
It can have a logger named com.foo.sampleapp.MyClass
Instantiated using
Logger.getLogger("com.foo.sampleapp.MyClass")
Installation Log4j
Download log4j-1.2.4.jar from http://logging.apache.org/log4j
Extract the log4j-1.2.4.jar at any desired location and include
its absolute path in the application's CLASSPATH
Now ,log4j API is accessible to user's application classes and can
be used for logging
Running Log4j
public MyClass()
{
myLogger.setLevel(Level.INFO); ...
myLayout = new SimpleLayout();
myAppender = new ConsoleAppender(myLayout);
...
myLogger.addAppender(myAppender); ...
}
public void do_something( int a, float b)
{
...
if (x == null)
{myLogger.error("enabled & logged ,since ERROR > INFO");...}
} public static void main(String args[]) { new MyClass().do_something(1,(float)1.2);}
}
Configuring Log4j
The log4j can be configured both programmatically and
externally using special configuration files
External configuration is most preferred, because to take effect
it doesn't require change in application code, recompilation, or
redeployment
Configuration files can be XML files or Java property files that
can be created and edited using any text editor or XML editor,
respectively
Configuring Log4j
The simplest configuration file will contain following
specifications that can be modified, both programmatically and
externally, to suit specific logging requirements
Programmatic Configuration
SimpleLayout myLayout;
public MyClass()
{ myLogger.setLevel(Level.INFO); ...
myLayout = new SimpleLayout();
myAppender = new ConsoleAppender(myLayout);
myLogger.addAppender(myAppender); ...
}
...
log4j.rootLogger=debug, myAppender
# The appender's type specified as ConsoleAppender, i.e. log output
# written to console
log4j.appender.myAppender=org.apache.log4j.ConsoleAppender
# The appender is assigned a layout SimpleLayout
# SimpleLayout will include only priority level of the log
# statement and the log statement itself in log output
log4j.appender.myAppender.layout=org.apache.log4j.SimpleLayout
...
if (x == null)
{myLogger.error("enabled & logged ,since ERROR > INFO");...}
}
}
//Execute the file as
// java -Dlog4j.configuration=config-sample.propertis
com.patni.sampleapp.MyClass
...
if (x == null)
{myLogger.error("enabled & logged ,since ERROR > INFO");...}
}
}
</root>
</log4j:configuration>
public MyClass()
{
...
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.SimpleLayout;
import org.apache.log4j.FileAppender;
import org.apache.log4j.Level;
import org.apache.log4j.Logger;
import org.apache.log4j.HTMLLayout;
import org.apache.log4j.WriterAppender;
public class htmlandwrite
{ static Logger logger = Logger.getLogger(htmlandwrite.class);
public static void main(String args[])
{ HTMLLayout layout = new HTMLLayout();
WriterAppender appender = null;
try { FileOutputStream output = new FileOutputStream("output2.html");
appender = new WriterAppender(layout,output); } catch(Exception e) {}
logger.addAppender(appender); logger.setLevel((Level) Level.DEBUG);
logger.debug("Here is some DEBUG"); logger.info("Here is some INFO");
logger.warn("Here is some WARN"); logger.error("Here is some ERROR");
logger.fatal("Here is some FATAL"); } }
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.apache.log4j.ConsoleAppender;
Resources
Log4j is freely available to download from The Apache Jakarta
Project Web site http://logging.apache.org/log4j/
For documentation of the log4j API visit
http://jakarta.apache.org/log4j/docs/api/index.html
Thank you