log4j xml configuration example

Logging is essential part of programming. It helps developer to track code workflow and fix bugs efficiently. If we get any issue in the code, we check logs for that workflow or functionality.

Log4j is fast , reliable logging framework and can be easily integrated with the code. It is possible to enable logging at run time without using

There are two ways, you can do logging using log4j.

  1. log4j.properties
  2. log4j.xml

In this post, we will see how to configure using log4.xml

1) Create simple maven java project.

2) Put log4j entry in pom.xml .

3) Update maven project to download require jar

Right click on project-> Maven -> update project

4) Create log4j.xml file

This file is main file for log4j configuration. It has information about log levels, log appenders.

Log to console:

If you want to log to console, you need to have following lines in log4j.xml.
Log to file :
If you want to log to console, you need to have following lines in log4j.xml and also you need to provide log file name too.
Log to console and file both :

If you want to log on both to console and file, you can use below code:

5) Sample class for Logger

Create a class called “LoggingMain.java” in src/main/java in package org.arpit.java2blog

6) Run program:

When you run above program , you will get below output to console and file(appLogs.log)

Bingo!! we have successfully configured log4j using xml file in maven project.

Related Posts

  • How to print an array in PHP
    19 October

    How to print an array in PHP

    Table of ContentsIntroductionHow to print an array in PHP?1) print_r() function:2) var_dump() function:3) json_encode() function:4) foreach() loop: Introduction An array is one kind of data structure, which can store multiple items of related data types. The printing of an array is one of the fundamental functions of PHP. How to print an array in PHP? […]

  • Multiple classes in one file in Java
    18 October

    Multiple classes in one file in Java

    Table of ContentsIntroductionMethods to Implement Multiple Classes In One Java Program1) Nested classes2) Multiple non-static nested classes In this post, we will see how to have multiple classes in one file in java. Introduction You need to have any number of classes in a single Java file, but there is a restriction that you can […]

  • How to initialize empty array in PHP
    18 October

    Declare empty array in php

    Table of ContentsUsing square brackets to declare empty array in PHPUsing array() to declare empty array in PHP An array is a data structure where you can store the same or different types of data in a single variable. In PHP, you can declare empty array using square brackets ([]) or using array() function. Using […]

  • Find duplicate elements in the Stream
    17 October

    Java 8 – Find duplicate elements in Stream

    Table of ContentsIntroductionUsing distinct()Using Collections.frequency()Using Collectors.toSet()Using Collectors.toMap()Using Collectors.groupingBy()Conclusion Introduction When working with a collection of elements in Java, it is very common to have duplicate elements, and Java provides different APIs that we can use to solve the problem. Java 8 Stream provides the functionality to perform aggregate operations on a collection, and one of […]

  • 17 October

    Java AES 256 Encryption Decryption Example

    Table of ContentsIntroductionGenerate a shared keyEncrypt a random textDecrypt the encrypted textConclusion Introduction Advanced encryption standard (AES) is the most secure encryption standard compared to RSA, which is vulnerable to brute force attacks. This is the main reason that AES was established by the National Institute of Standard and Technology (NIST) in 2001 and is […]

  • 16 October

    Java AES Encryption Decryption Example

    Table of ContentsIntroductionGenerate a shared keyEncrypt a random textDecrypt the encrypted textConclusion Introduction AES stands for advanced encryption standard and is the most commonly used symmetric algorithm to encrypt sensitive data and can be used in both software and hardware. The AES algorithm is symmetric, meaning that it uses only one key for encryption and […]

Leave a Reply

Your email address will not be published. Required fields are marked *

Subscribe to our newletter

Get quality tutorials to your inbox. Subscribe now.