Logging With Log4j Example Code

Logging With Log4j Example Code

Logging With Log4j Example Code Rating: 9,5/10 4345 reviews

The distribution comes with a lot of stuff: documentation, source code, examples but the only thing we need is the log4j-1.2.17.jar file – log4j’s JAR library. Add the log4j-1.2.17.jar into compilation classpath and runtime classpath of your project in order to have your code can be compiled and executed with log4j. Log4j – A Java-based Logging API. Moving on to the technical details about logging, let us discuss the origin of the API that we would be using throughout the log4j tutorial to generate logs. Log4j was a result of collaborative efforts of people at Secure Electronic Marketplace for Europe to develop a utility that would help us generating.

  • log4j Tutorial
With
  • log4j Useful Resources
  • Selected Reading

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. Xcb tutorial. 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 −

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.

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 −

  • log4j Tutorial
  • log4j Useful Resources
  • Selected Reading

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.

This tutorial has been prepared for beginners to help them understand the basic functionality of log4J logging framework. Origin 6 data analysis free download.

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.

Recent Articles

Logging With Log4j Example Code
© 2020