log4j – Sample Program ”; Previous Next We have seen how to create a configuration file. This chapter describe how to generate debug messages and log them in a simple text file. Following is a simple configuration file created for our example. Let us revise it once again: 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. The contents of log4j.properties file are as follows − # 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 log4j.appender.FILE.File=${log}/log.out # Define the layout for file appender log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%m%n Using log4j in Java Program The following Java class is a very simple example that initializes, and then uses, the log4j logging library for Java applications. 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 a debug message”); log.info(“Hello this is an info message”); } } Compile and Execute Here are the steps to compile and run the above-mentioned program. Make sure you have set PATH and CLASSPATH appropriately before proceeding for the compilation and execution. All the libraries should be available in CLASSPATH and your log4j.properties file should be available in PATH. Follow the steps give below − Create log4j.properties as shown above. Create log4jExample.java as shown above and compile it. Execute log4jExample binary to run the program. You would get the following result inside /usr/home/log4j/log.out file − Hello this is a debug message Hello this is an info message Print Page Previous Next Advertisements ”;
Category: log4j
log4j – Logging Methods
log4j – Logging Methods ”; Previous Next Logger class provides a variety of methods to handle logging activities. The Logger class does not allow us to instantiate a new Logger instance but it provides two static methods for obtaining a Logger object − public static Logger getRootLogger(); public static Logger getLogger(String name); The first of the two methods returns the application instance”s root logger and it does not have a name. Any other named Logger object instance is obtained through the second method by passing the name of the logger. The name of the logger can be any string you can pass, usually a class or a package name as we have used in the last chapter and it is mentioned below − static Logger log = Logger.getLogger(log4jExample.class.getName()); 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. # Methods and Description 1 public void debug(Object message) It prints messages with the level Level.DEBUG. 2 public void error(Object message) It prints messages with the level Level.ERROR. 3 public void fatal(Object message) It prints messages with the level Level.FATAL. 4 public void info(Object message) It prints messages with the level Level.INFO. 5 public void warn(Object message) It prints messages with the level Level.WARN. 6 public void trace(Object message) It prints messages with the level Level.TRACE. All the levels are defined in the org.apache.log4j.Level class and any of the above mentioned methods 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 the following result − Debug Message! Info Message! Warn Message! Error Message! Fatal Message! All the debug messages make more sense when they are used in combination with levels. We will cover levels in the next chapter and then, you would have a good understanding of how to use these methods in combination with different levels of debugging. Print Page Previous Next Advertisements ”;
log4j – Logging Levels
log4j – Logging Levels ”; Previous Next The org.apache.log4j.Level levels. You can also define your custom levels by sub-classing the Level class. Level Description ALL All levels including custom levels. DEBUG Designates fine-grained informational events that are most useful to debug an application. INFO Designates informational messages that highlight the progress of the application at coarse-grained level. WARN Designates potentially harmful situations. ERROR Designates error events that might still allow the application to continue running. FATAL Designates very severe error events that will presumably lead the application to abort. OFF The highest possible rank and is intended to turn off logging. TRACE Designates finer-grained informational events than the DEBUG. How do Levels Works? A log request of level p in a logger with level q is enabled if p >= q. This rule is at the heart of log4j. It assumes that levels are ordered. For the standard levels, we have ALL < DEBUG < INFO < WARN < ERROR < FATAL < OFF. The Following example shows how we can filter all our DEBUG and INFO messages. This program uses of logger method setLevel(Level.X) to set a desired logging level: This example would print all the messages except Debug and Info: import org.apache.log4j.*; public class LogClass { private static org.apache.log4j.Logger log = Logger.getLogger(LogClass.class); public static void main(String[] args) { log.setLevel(Level.WARN); 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 the LogClass program, it would generate the following result − Warn Message! Error Message! Fatal Message! Setting Levels using Configuration File log4j provides you configuration file based level setting which sets you free from changing the source code when you want to change the debugging level. Following is an example configuration file which would perform the same task as we did using the log.setLevel(Level.WARN) method in the above example. # Define the root logger with appender file log = /usr/home/log4j log4j.rootLogger = WARN, FILE # Define the file appender log4j.appender.FILE=org.apache.log4j.FileAppender log4j.appender.FILE.File=${log}/log.out # Define the layout for file appender log4j.appender.FILE.layout=org.apache.log4j.PatternLayout log4j.appender.FILE.layout.conversionPattern=%m%n Let us now use our following program − import org.apache.log4j.*; 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!”); } } Now compile and run the above program and you would get following result in /usr/home/log4j/log.out file − Warn Message! Error Message! Fatal Message! Print Page Previous Next Advertisements ”;
log4j – Home
log4j Tutorial PDF Version Quick Guide Resources Job Search Discussion log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages. Audience This tutorial has been prepared for beginners to help them understand the basic functionality of log4J logging framework. Prerequisites As you are going to use Log4J logging framework in various Java-based application development, it is imperative that you should have a good understanding of Java programming language. Print Page Previous Next Advertisements ”;