Log4j Tutorial

Log4j is a simple and flexible logging framework. Logging equips the developer with detailed context for application failures. With log4j it is possible to enable logging at runtime without modifying the application binary.

This page lists down all tutorials on this website available on this topic. This page will be updated in time to time, as a new post is published. Feel free to suggest topics you want to read more on.

1. Log4j Configuration Examples

Maven configuration example

Learn the most basic steps to configure log4j logging support in your project using maven.

Properties configuration using PropertyConfigurator

Two most common configuration options are in practice industry wide i.e. using xml file configuration and using properties file configuration.
This this post, I am showing the example code for configuring log4j using properties file.

Xml configuration using DomConfigurator

In previous example, we learned about configuring log4j using properties file. This this tutorial, I am showing the example code for configuring log4j using xml configuration.

2. Log4j Layout examples

Create logs in html format : HTMLLayout example

Log4j comes with multiple options to format log files created by framework. It can create simple log files, html log files or xml log files also.
In this post, I am showing the example code for configuring log4j to produce logs in html format.

Create logs in xml format : XMLLayout example

Log4j comes with multiple options to format log files created by the framework. It can create simple log files, HTML log files, or XML log files also. In this post, I am showing the example code for configuring log4j to produce logs in XML format.

3. Log4j Appenders examples

Programmatically configure appenders example

Log4j allows logging requests to print to multiple destinations. In log4j speak, an output destination is called an appender. They vary from console, files, GUI components, remote socket servers to JMS. Let’s learn about programmatic way to configure appenders in log4j.

Store logs in database : JDBCAppender example

Log4j comes with multiple options to format log files created by framework. It can create simple log files, html log files or xml log files also. It also insert log statements into database also, using mysql statements. Let’s see the example code for configuring log4j to produce logs in database table.

Store logs in socket server : SocketAppender example

This this tutorial, I am showing the example code for configuring log4j to record logs events in a simple socket server (packaged inside log4j.jar itself) over the network location.

4. Log4j Logging levels

Logging levels in log4j

The amount and type of information shown in the system and event logs is controlled by the log4j “level” setting in configuration file. Remember, each message on the log is prefixed by the level of the message. Read more about them here.

5. Misc Examples

Reload logging levels in runtime

Excessive logging is a common cause of performance degrade of application. It is one of best practices to ensure proper logging within your Java EE application implementation. However, be careful with the logging level that you enable in your production environment. Excessive logging will trigger high IO on your server and increase CPU utilization.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

3 thoughts on “Log4j Tutorial”

  1. Hi Lokesh,
    I have got a scenario that if we have 3 components A,B,C and A and B are two components where as C is common component which is used by both A and B .Now I have all the log files in all the components .I don’t want to have a separate log file in C(common component). I need to have only one log file when A component uses C(common component).How can i do this one?I am generating these logs in Java Programming .Can anyone help me in this .Thank you

  2. Hi Lokesh,

    I had real confusion where to log error message in layered architecture.I’m developing a web application using spring3 i have 3 layers Controller,Servicelayer,DAOlayer. When an exception occured in DAO layer i’m wrapping it to custom exception and re throwing to service layer. My question is where should i log error message in service layer or dao layer or in controller. I read somewhere it is not good practice place both logger.error & throw in same method if it is case why so please explain??

    • Let me tell you my friend, it depends on use-cases when actually you write the code. In general I prefer to delegate logging to the caller. Usually it is caller who knows how to handle an exception, retry? notify the user? log? forget?
      You are right that logging exception and throwing it further is actually an anti-pattern. The rule of thumb is: if you can’t do anything useful to handle the exception (logging is not handling the exception in most cases), let your framework/container do it for you.
      Summarizing everything. In my opinion, log the exception immediately before handling it, be it in dao or service or in controller.

      Above was theory and must be honored. But, practically you categorize exceptions and log them in separate log files. e.g. one log file for dao layer, second for web layer and another for transactions and so on. So, you need to decide in which log file you want to log any particular exception.

      Basically, you have to walk on a fine line between theory and practical.

      Read more: https://howtodoinjava.com/best-practices/java-exception-handling-best-practices/

Comments are closed.

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.