Log4j Tutorial
Log4j Tutorial
loggers: Responsible for capturing logging information. appenders : Responsible for publishing logging information to various preferred destinations. layouts: Responsible to format logging information in different styles.
log4j Features:
log4j is thread-safe. log4j is optimized for speed. log4j is based on a named logger hierarchy. log4j supports multiple output appenders per logger. log4j supports internationalization. log4j is not restricted to a predefined set of facilities. Logging behavior can be set at runtime using a configuration file. log4j is designed to handle Java Exceptions from the start. log4j uses multiple levels, namely ALL, TRACE, DEBUG, INFO, WARN, ERROR and FATAL. The format of the log output can be easily changed by extending the Layout class. The target of the log output as well as the writing strategy can be altered by implementations of the Appender interface. log4j is fail-stop. However, altough it certainly strives to ensure delivery, log4j does not guarantee that each log statement will be delivered to its destination.
log4j Installation:
Log4j API package is distributed under the Apache Software License, a fully-fledged open source license certified by the open source initiative. The latest log4j version, including full-source code, class files and documentation can be found at http://logging.apache.org/log4j/. Once downloaded apache-log4j-x.x.x.tar.gz, follow the given steps at log4j - Installation.
ERROR
FATAL
INFO
Logging Methods:
Once we obtain an instance of a named logger, we can use several methods of the logger to log messages. The Logger class has the following methods for printing the logging information. SN 1 Methods with Description public void debug(Object message) This method prints messages with the level Level.DEBUG. public void error(Object message) This method prints messages with the level Level.ERROR. public void fatal(Object message); This method prints messages with the level Level.FATAL. public void info(Object message); This method prints messages with the level Level.INFO. public void warn(Object message); This method prints messages with the level Level.WARN.
2|Page
All the levels are defined in the org.apache.log4j.Level class and any of the above mentioned method can be called as follows:
import org.apache.log4j.Logger; public class LogClass { private static org.apache.log4j.Logger log = Logger .getLogger(LogClass.class); public static void main(String[] args) { log.trace("Trace Message!"); log.debug("Debug Message!"); log.info("Info Message!"); log.warn("Warn Message!"); log.error("Error Message!"); log.fatal("Fatal Message!"); } }
When you compile and run LogClass program it would generate following result:
Debug Message! Info Message! Warn Message! Error Message! Fatal Message!
DateLayout HTMLLayout ( Explained in this tutorial ) PatternLayout. ( Explained in this tutorial ) SimpleLayout XMLLayout
Apart from these abstract methods, the Layout class provides concrete implementation for the methods listed below: S.N. 1 Method & Description public String getContentType() Returns the content type used by the Layout objects. The base class returns text/plain as the default content type. public String getFooter() Specifies the footer information of the logging message. public String getHeader() Specifies the header information of the logging message.
Each subclass can return class-specific information by overriding the concrete implementation of these methods.
The level of the root logger is defined as DEBUG and attaches appender named FILE to it. The appender FILE is defined as org.apache.log4j.FileAppender and writes to a file named "log.out" located in the log directory. The layout pattern defined is %m%n, which means the printed logging message will be followed by a newline character.
# Define the root logger with appender file log = /usr/home/log4j log4j.rootLogger = DEBUG, FILE # Define the file appender log4j.appender.FILE=org.apache.log4j.FileAppender
4|Page
import org.apache.log4j.Logger; import java.io.*; import java.sql.SQLException; import java.util.*; public class log4jExample{ /* Get actual class name to be printed on */ static Logger log = Logger.getLogger( log4jExample.class.getName()); public static void main(String[] args) throws IOException,SQLException{ log.debug("Hello this is an debug message"); log.info("Hello this is an info message"); } }
5|Page
Learn JSP Learn Servlets Learn log4j Learn iBATIS Learn Java Learn JDBC Java Examples Learn Best Practices Learn Python Learn Ruby Learn Ruby on Rails Learn SQL Learn MySQL Learn AJAX Learn C Programming Learn C++ Programming Learn CGI with PERL Learn DLL Learn ebXML Learn Euphoria Learn GDB Debugger Learn Makefile Learn Parrot Learn Perl Script Learn PHP Script Learn Six Sigma Learn SEI CMMI Learn WiMAX Learn Telecom Billing
Learn ASP.Net Learn HTML Learn HTML5 Learn XHTML Learn CSS Learn HTTP Learn JavaScript Learn jQuery Learn Prototype Learn script.aculo.us Web Developer's Guide Learn RADIUS Learn RSS Learn SEO Techniques Learn SOAP Learn UDDI Learn Unix Sockets Learn Web Services Learn XML-RPC Learn UML Learn UNIX Learn WSDL Learn i-Mode Learn GPRS Learn GSM Learn WAP Learn WML Learn Wi-Fi
webmaster@TutorialsPoint.com
6|Page