Log4J Tutorial - Log4J Examples in Java
In this tutorial you will learn about Log4J, which is one of the most used Logging API by the Java programmers. It is fast and easy to use and provides customizable log formats. Here we are providing many examples of the Log4J framework.
Log4J is an Open Source logging framework from the apache foundation. It's a Java based framework and can be used in any java program. It was originally developed by Ceki Gülcü and now its part of Apache Software project.
It can be used by the Java programmers to add logging functionality in the projects. These days most of the Java projects uses Apache Log4J for logging.
It provides powerful yet efficient and fast API for the Java programmers. Java programmers easily uses Log4J in their code to log the error and information messages into a log file.
Log4j simply inserts a log statement in the application code and manage them externally without going back to its application source code. Programmer's can control the logging message with the help of external configuration file e.g. log4.xml or log4j.properties file. The external properties file can be used to set the log format as well as the level of logging (DEBUG, INFO, FATAL etc..)
Let's get started with the Log4J framework:
-
Log4J is leading logging Framework in Java
- Log4J Architecture
In this section we will understand the architecture of Log4J framework.
- A simple example of log4j
This Example shows you how to create a log in a program.
- log4j example
This Example shows you how to create a log in a Servlet.
- Use of BasicConfigurator in Log4j logging
In earlier example you have seen that we can define properties of log4j logging file with the help of log4j.properties. Properties can also be defined within log4j.xml file.
- log4j.xml Example
In earlier example you have seen that we can define properties of log4j logging file with the help of log4j.properties. Properties can also be defined within log4j.xml file.
- Console Appender in Log4j
In this log4j console appender tutorial you will be introduced with ConsoleAppender which is used in Log4j for appending output on the console each and every time when any info(), debug(), error() method is invoked by the logger.
- FileAppender in Log4j
In previous section we have used ConsoleAppender to append log events. We can also append these log events to a file by using FileAppender.
- WriterAppender in Log4j
In our previous section we have seen that we can append log events in simple file (plain/text) file and to the console also. If we want to write files then we have to use WriterAppender.
- Layouts in Log4j
Users more often wish to customize their output according to their output format. Layout can be set by attaching it with the appender. The Layout is responsible for formatting the logging output.
- Use of SimpleLayout in Log4j
In the previous example we have studied that there are many Layout classes for displaying logging output in the desired format. In this section we are going to describe you one of them, SimpleLayout, the simplest of all.
- Use of XMLLayout in Log4j
In the previous sections we have used PatternLayout and SimpleLayout, now we are going to describe you how to use XMLLayout class to produce logging output in the XML format.
- TTCCLayout in Log4j
In the previous example we have studied that there are many Layout classes for displaying logging output in the desired format.
- Use of HTMLLayout in Log4j
In this section we will describe you about HTMLLayout class. This layout outputs events in a HTML table. Appenders using this layout should have their encoding set to UTF-8 or UTF-16, otherwise events containing non ASCII characters could result in corrupted log files.
-
A simple example of log4j for Servlet
This Example shows you how to create a log in a Servlet.