• Java
    • JAXB Tutorial
      • What is JAXB
      • JAXB Marshalling Example
      • JAXB UnMarshalling Example
  • Spring Tutorial
    • Spring Core Tutorial
    • Spring MVC Tutorial
      • Quick Start
        • Flow Diagram
        • Hello World Example
        • Form Handling Example
      • Handler Mapping
        • BeanNameUrlHandlerMapping
        • ControllerClassNameHandlerMapping
        • SimpleUrlHandlerMapping
      • Validation & Exception Handling
        • Validation+Annotations
        • Validation+ResourceBundle
        • @ExceptionHandler
        • @ControllerAdvice
        • Custom Exception Handling
      • Form Tag Library
        • Textbox Example
        • TextArea Example
        • Password Example
        • Dropdown Box Example
        • Checkboxes Example
        • Radiobuttons Example
        • HiddenValue Example
      • Misc
        • Change Config file name
    • Spring Boot Tutorial
  • Hibernate Tutorial
  • REST Tutorial
    • JAX-RS REST @PathParam Example
    • JAX-RS REST @QueryParam Example
    • JAX-RS REST @DefaultValue Example
    • JAX-RS REST @Context Example
    • JAX-RS REST @MatrixParam Example
    • JAX-RS REST @FormParam Example
    • JAX-RS REST @Produces Example
    • JAX-RS REST @Consumes Example
    • JAX-RS REST @Produces both XML and JSON Example
    • JAX-RS REST @Consumes both XML and JSON Example
  • Miscellaneous
    • JSON Parser
      • Read a JSON file
      • Write JSON object to File
      • Read / Write JSON using GSON
      • Java Object to JSON using JAXB
    • CSV Parser
      • Read / Write CSV file
      • Read/Parse/Write CSV File – OpenCSV
      • Export data into a CSV File
      • CsvToBean and BeanToCsv – OpenCSV

JavaInterviewPoint

Java Development Tutorials

Spring Boot Log4j2 Example | XML + YAML + JSON

January 30, 2018 by javainterviewpoint Leave a Comment

Spring Boot by default uses Logback framework for logging when we use Spring Boot Starter dependency. Apache Log4j 2 is the successor of Log4j which provides significant improvements over its predecessor Log4j 1.x and provides many of the features available in Logback. In this Spring Boot Log4j2 Example, we will learn how to configure the log4j 2 framework in Spring boot application.

Spring boot provides a default starter for logging spring-boot-starter-logging. It is included by default in spring-boot-starter. In order to use Log4j2, we will be excluding spring-boot-starter-logging

Folder Structure:

Spring Boot Log4j2 Example

  • Create a Maven project (maven-archetype-quickstart) “SpringBootLog4j2” and create a package for our source files “com.javainterviewpoint” under  src/main/java 
  • Now add the following dependency in the POM.xml
    <project xmlns="http://maven.apache.org/POM/4.0.0"	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    	<modelVersion>4.0.0</modelVersion>
    
    	<groupId>com.javainterviewpoint</groupId>
    	<artifactId>SpringBootLog4j2</artifactId>
    	<version>0.0.1-SNAPSHOT</version>
    	<packaging>jar</packaging>
    
    	<name>SpringBootLog4j2</name>
    	<url>http://maven.apache.org</url>
    
    	<properties>
    		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    	</properties>
    
    	<parent>
    		<groupId>org.springframework.boot</groupId>
    		<artifactId>spring-boot-starter-parent</artifactId>
    		<version>1.5.1.RELEASE</version>
    	</parent>
    	<dependencies>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter</artifactId>
    			<exclusions>
    				<!-- <exclusion> 
    					    <groupId>org.springframework.boot</groupId> 
    					    <artifactId>spring-boot-starter-logging</artifactId> 
    					</exclusion> -->
    				<exclusion>
    					<groupId>org.springframework.boot</groupId>
    					<artifactId>spring-boot-starter-logging</artifactId>
    				</exclusion>
    				<exclusion>
    					<artifactId>logback-classic</artifactId>
    					<groupId>ch.qos.logback</groupId>
    				</exclusion>
    				<exclusion>
    					<artifactId>log4j-over-slf4j</artifactId>
    					<groupId>org.slf4j</groupId>
    				</exclusion>
    			</exclusions>
    		</dependency>
    		<dependency>
    			<groupId>org.springframework.boot</groupId>
    			<artifactId>spring-boot-starter-log4j2</artifactId>
    		</dependency>
    		<dependency>
    			<groupId>com.fasterxml.jackson.core</groupId>
    			<artifactId>jackson-databind</artifactId>
    			<version>2.7.4</version>
    		</dependency>
    
    		<dependency>
    			<groupId>com.fasterxml.jackson.dataformat</groupId>
    			<artifactId>jackson-dataformat-yaml</artifactId>
    			<version>2.7.4</version>
    		</dependency>
    	</dependencies>
    	<build>
    		<plugins>
    			<plugin>
    				<groupId>org.springframework.boot</groupId>
    				<artifactId>spring-boot-maven-plugin</artifactId>
    			</plugin>
    		</plugins>
    	</build>
    </project>
    
  • Adding the dependency jackson-dataformat-yaml, jackson-databind to read the log4j2.yaml and log4j2.json configuration files
  • Create the Java classes App.java under com.javainterviewpoint folder.
  • Create log4j2.xml / log4j2.json / log4j2.yaml file under src/main/resources directory.

Other Posts which you may like …

  • Spring Boot HikariCP Connection Pool Example
  • Integrate Spring Data ElasticSearch in Spring Boot
  • Spring Boot Hello World Example with Maven
  • Spring Boot Hello World Example in Eclipse
  • Spring Boot RESTful Web Services Example
  • Spring Boot Kotlin RESTful Web Services CRUD Example
  • How to Create Deployable WAR | Spring Boot | SpringBootServletInitializer
  • Spring Boot – How to Change Embedded Tomcat default port
  • Fix missing src/main/java folder in Eclipse Maven Project
  • Spring MVC CRUD Example with MySql + JdbcTemplate
  • RESTful Java Client With Jersey Client
  • JAX-RS REST @Produces both XML and JSON Example
  • JAX-RS REST @Consumes both XML and JSON Example
  • RESTful Java Client With Jersey Client
  • RESTEasy Hello World Example with Apache Tomcat
  • RESTful Java client with RESTEasy client
  • Spring RESTful Web Services Hello World XML Example
  • Springfox Swagger 2 for Spring RESTful Web Services
  • Spring REST Hello World Example – JSON and XML responses
  • ClassNotFoundException: org.springframework.web.context.ContextLoaderListener
  • Fix missing src/main/java folder in Eclipse
  • Spring MVC CRUD Example with MySql + JdbcTemplate

Spring Boot Log4j2 Example

Dependency Tree

[INFO] ------------------------------------------------------------------------
[INFO] Building SpringBootLog4j2 0.0.1-SNAPSHOT
[INFO] ------------------------------------------------------------------------
[INFO] 
[INFO] --- maven-dependency-plugin:2.10:tree (default-cli) @ SpringBootLog4j2 ---
[INFO] com.javainterviewpoint:SpringBootLog4j2:jar:0.0.1-SNAPSHOT
[INFO] +- org.springframework.boot:spring-boot-starter:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot:jar:1.5.1.RELEASE:compile
[INFO] |  |  \- org.springframework:spring-context:jar:4.3.6.RELEASE:compile
[INFO] |  |     +- org.springframework:spring-aop:jar:4.3.6.RELEASE:compile
[INFO] |  |     +- org.springframework:spring-beans:jar:4.3.6.RELEASE:compile
[INFO] |  |     \- org.springframework:spring-expression:jar:4.3.6.RELEASE:compile
[INFO] |  +- org.springframework.boot:spring-boot-autoconfigure:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.springframework:spring-core:jar:4.3.6.RELEASE:compile
[INFO] |  \- org.yaml:snakeyaml:jar:1.17:compile
[INFO] +- org.springframework.boot:spring-boot-starter-log4j2:jar:1.5.1.RELEASE:compile
[INFO] |  +- org.apache.logging.log4j:log4j-slf4j-impl:jar:2.7:compile
[INFO] |  |  \- org.slf4j:slf4j-api:jar:1.7.22:compile
[INFO] |  +- org.apache.logging.log4j:log4j-api:jar:2.7:compile
[INFO] |  +- org.apache.logging.log4j:log4j-core:jar:2.7:compile
[INFO] |  +- org.slf4j:jcl-over-slf4j:jar:1.7.22:compile
[INFO] |  \- org.slf4j:jul-to-slf4j:jar:1.7.22:compile
[INFO] +- com.fasterxml.jackson.core:jackson-databind:jar:2.7.4:compile
[INFO] |  +- com.fasterxml.jackson.core:jackson-annotations:jar:2.8.0:compile
[INFO] |  \- com.fasterxml.jackson.core:jackson-core:jar:2.8.6:compile
[INFO] \- com.fasterxml.jackson.dataformat:jackson-dataformat-yaml:jar:2.7.4:compile

Spring boot Log4j2 Configuration

Spring Boot automatically configures Log4j2 the moment it finds a file named log4j2.xml or log4j2.json or log4j2.yaml in the classpath.

log4j2.xml

Create log4j2.xml file under src/main/resources folder

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
	 <Appenders>
		<Console name="ConsoleAppender" target="SYSTEM_OUT">
			<PatternLayout pattern="%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n" />
		</Console>
		
		<File name="FileAppender" fileName="D:/JIP/Application.log">
			<PatternLayout pattern="%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n" />
		</File>
	</Appenders>
	
	<Loggers>
		<!-- Logging custom package -->
		<Logger name="com.javainterviewpoint" level="debug" additivity="false">
			<AppenderRef ref="ConsoleAppender"></AppenderRef>
			<AppenderRef ref="FileAppender"></AppenderRef>
		</Logger>
		<!-- Logging spring boot package -->
		<Logger name="org.springframework.boot" level="debug" additivity="false">
      		<AppenderRef ref="ConsoleAppender"></AppenderRef>
			<AppenderRef ref="FileAppender"></AppenderRef>
   		</Logger>
		<Root level="warn">
      		<AppenderRef ref="ConsoleAppender"></AppenderRef>
			<AppenderRef ref="FileAppender"></AppenderRef>
    	</Root>
	</Loggers>
</Configuration>

log4j2.yaml

Spring Boot requires  “jackson-dataformat-yaml” dependency to pick up the yaml file.

Configutation:
  name: Default

  Appenders:

    Console:
      name: ConsoleAppender
      target: SYSTEM_OUT
      PatternLayout:
        pattern: "%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n"

    File:
      name: FileAppender
      fileName: D:/JIP/Application.log
      PatternLayout:
        pattern: "%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n"

  Loggers:

      Root:
        level: debug
        AppenderRef:
          - ref: ConsoleAppender

      Logger:
        - name: com.javainterviewpoint
          level: debug
          AppenderRef:
            - ref: FileAppender
              level: debug

log4j2.json

Add “jackson-databind” dependency so that spring boot reads the json configuration

{
  "configuration": {
    "name": "Default",
    "appenders": {
      "Console": {
        "name": "ConsoleAppender",
        "target": "SYSTEM_OUT",
        "PatternLayout": {
          "pattern": "%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n"
        }
      },
      "File": {
        "name": "FileAppender",
        "fileName": "D:/JIP/Application.log",
        "PatternLayout": {
          "pattern": "%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n"
        }
      }
    },
    "loggers": {
      "logger": {
        "name": "com.javainterviewpoint",
        "level": "debug",
        "appender-ref": [{"ref": "ConsoleAppender", "level":"debug"},{"ref": "FileAppender", "level":"debug"}]
      },
	  "logger": {
        "name": "org.springframework.boot",
        "level": "debug",
        "appender-ref": [{"ref": "ConsoleAppender", "level":"debug"},{"ref": "FileAppender", "level":"debug"}]
      },
      "root": {
        "level": "debug",
        "appender-ref": {"ref": "ConsoleAppender"}
      }
    }
  }
}

Log4j2 properties (log4j2.properties)

You can still add have the tradition Log4j2 properties file for configuring Log4j 2

name=PropertiesConfig
appenders = console, file

appender.console.type = Console
appender.console.name = ConsoleAppender
appender.console.layout.type = PatternLayout
appender.console.layout.pattern = %d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n

appender.file.type = File
appender.file.name = FileAppender
appender.file.fileName=D:/JIP/Application_properties.log
appender.file.layout.type=PatternLayout
appender.file.layout.pattern= %d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n

loggers=file
logger.file.name=com.javainterviewpoint
logger.file.level = debug
logger.file.appenderRefs = file
logger.file.appenderRef.file.ref = FileAppender

rootLogger.level = debug
rootLogger.appenderRefs = stdout
rootLogger.appenderRef.stdout.ref = ConsoleAppender

App.java

package com.javainterviewpoint;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class App implements ApplicationRunner
{
    private static Logger logger = LogManager.getLogger(App.class);

    public static void main(String[] args) 
    {
        SpringApplication.run(App.class, args);
    }
    @Override
    public void run(ApplicationArguments applicationArguments) throws Exception
    {
        logger.debug("Debugging log");
        logger.info("Info log");
        logger.warn("Warning log");
        logger.error("Error log");
        logger.fatal("Fatal log");
    }
}

The App class main() method is the triggering point of our application, it in-turn calls Spring Boot’s SpringApplication class run() method which bootstrap our App application. We need to pass our App.class as an argument to our run() method. We have printed some logging statements.

Output:

Run the spring boot application using “mvn spring-boot:run”

 .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v1.5.1.RELEASE)

01:25 21:53:43.364 [main] [INFO] [com.javainterviewpoint.App] - Starting App on Jack-PC with PID 4228 (D:\sts-3.8.3.RELEASE\Workspace\SpringBootLog4j2\target\classes started by Jack in D:\sts-3.8.3.RELEASE\Workspace\SpringBootLog4j2)
01:25 21:53:43.365 [main] [DEBUG] [com.javainterviewpoint.App] - Running with Spring Boot v1.5.1.RELEASE, Spring v4.3.6.RELEASE
01:25 21:53:43.365 [main] [INFO] [com.javainterviewpoint.App] - No active profile set, falling back to default profiles: default
01:25 21:53:43.365 [main] [DEBUG] [org.springframework.boot.SpringApplication] - Loading source class com.javainterviewpoint.App
01:25 21:53:44.094 [main] [DEBUG] [org.springframework.boot.admin.SpringApplicationAdminMXBeanRegistrar$SpringApplicationAdmin] - Application Admin MBean registered with name 'org.springframework.boot:type=Admin,name=SpringApplication'
01:25 21:53:44.158 [main] [DEBUG] [org.springframework.boot.autoconfigure.logging.AutoConfigurationReportLoggingInitializer] - 


=========================
AUTO-CONFIGURATION REPORT
=========================


Positive matches:
-----------------

   GenericCacheConfiguration matched:
      - Cache org.springframework.boot.autoconfigure.cache.GenericCacheConfiguration automatic cache type (CacheCondition)

   JmxAutoConfiguration matched:
      - @ConditionalOnClass found required class 'org.springframework.jmx.export.MBeanExporter'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)
      - @ConditionalOnProperty (spring.jmx.enabled=true) matched (OnPropertyCondition)

   JmxAutoConfiguration#mbeanExporter matched:
      - @ConditionalOnMissingBean (types: org.springframework.jmx.export.MBeanExporter; SearchStrategy: current) did not find any beans (OnBeanCondition)

   JmxAutoConfiguration#mbeanServer matched:
      - @ConditionalOnMissingBean (types: javax.management.MBeanServer; SearchStrategy: all) did not find any beans (OnBeanCondition)

   JmxAutoConfiguration#objectNamingStrategy matched:
      - @ConditionalOnMissingBean (types: org.springframework.jmx.export.naming.ObjectNamingStrategy; SearchStrategy: current) did not find any beans (OnBeanCondition)

Negative matches:
-----------------

   ActiveMQAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.ActiveMQConnectionFactory' (OnClassCondition)

   AopAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.aspectj.lang.annotation.Aspect', 'org.aspectj.lang.reflect.Advice' (OnClassCondition)

   ArtemisAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'javax.jms.ConnectionFactory', 'org.apache.activemq.artemis.jms.client.ActiveMQConnectionFactory' (OnClassCondition)

   BatchAutoConfiguration:
      Did not match:
         - @ConditionalOnClass did not find required classes 'org.springframework.batch.core.launch.JobLauncher', 'org.springframework.jdbc.core.JdbcOperations' (OnClassCondition)

   CacheAutoConfiguration:
      Did not match:
         - @ConditionalOnBean (types: org.springframework.cache.interceptor.CacheAspectSupport; SearchStrategy: all) did not find any beans (OnBeanCondition)
      Matched:
         - @ConditionalOnClass found required class 'org.springframework.cache.CacheManager'; @ConditionalOnMissingClass did not find unwanted class (OnClassCondition)

Exclusions:
-----------

    None

Unconditional classes:
----------------------
    org.springframework.boot.autoconfigure.context.ConfigurationPropertiesAutoConfiguration

    org.springframework.boot.autoconfigure.web.WebClientAutoConfiguration

    org.springframework.boot.autoconfigure.context.PropertyPlaceholderAutoConfiguration

    org.springframework.boot.autoconfigure.info.ProjectInfoAutoConfiguration


01:25 21:53:44.174 [main] [DEBUG] [com.javainterviewpoint.App] - Debugging log
01:25 21:53:44.175 [main]  [INFO] [com.javainterviewpoint.App] - Info log
01:25 21:53:44.175 [main] [WARN] [com.javainterviewpoint.App] - Warning log
01:25 21:53:44.176 [main] [ERROR] [com.javainterviewpoint.App] - Error log
01:25 21:53:44.176 [main] [FATAL] [com.javainterviewpoint.App] - Fatal log
01:25 21:53:44.177 [main] [INFO] [com.javainterviewpoint.App] - Started App in 1.244 seconds (JVM running for 2.269)

Spring Boot Rolling File Appender

If you want a new log file to be created whenever the file reaches a certain threshold size, then you can go for RollingFile Appender, in order to add a RollingFile add the below code in the log4j2.xml

<RollingFile name="FileAppender" fileName="D:/JIP/Application_Roll.log"
	filePattern="D:/JIP/Application_Roll-%d{yyyy-MM-dd}-%i.log">
	<PatternLayout>
		<Pattern>%d{MM:dd HH:mm:ss.SSS} [%t] [%level] [%logger{36}] - %msg%n</Pattern>
	</PatternLayout>
		<Policies>
			<SizeBasedTriggeringPolicy size="1 MB" />
		</Policies>
		<DefaultRolloverStrategy max="5" />
</RollingFile>

As per the above configuration, a new file will be created whenever the log file size reaches 1 MB since we have set up the SizeBasedTriggeringPolicy to 1 MB, The <DefaultRollOverStrategy max=”5″ /> element defines the maximum number of log files which will be kept.

Happy Learning 🙂

   Download Source Code

Filed Under: J2EE, Java, Spring, Spring Boot, Spring Tutorial Tagged With: Log4j2, Spring Boot, Spring Boot Log4j2

Leave a Reply Cancel reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Java Basics

  • JVM Architecture
  • Object in Java
  • Class in Java
  • How to Set Classpath for Java in Windows
  • Components of JDK
  • Decompiling a class file
  • Use of Class.forName in java
  • Use Class.forName in SQL JDBC

Oops Concepts

  • Inheritance in Java
  • Types of Inheritance in Java
  • Single Inheritance in Java
  • Multiple Inheritance in Java
  • Multilevel Inheritance in Java
  • Hierarchical Inheritance in Java
  • Hybrid Inheritance in Java
  • Polymorphism in Java – Method Overloading and Overriding
  • Types of Polymorphism in java
  • Method Overriding in Java
  • Can we Overload static methods in Java
  • Can we Override static methods in Java
  • Java Constructor Overloading
  • Java Method Overloading Example
  • Encapsulation in Java with Example
  • Constructor in Java
  • Constructor in an Interface?
  • Parameterized Constructor in Java
  • Constructor Chaining with example
  • What is the use of a Private Constructors in Java
  • Interface in Java
  • What is Marker Interface
  • Abstract Class in Java

Java Keywords

  • Java this keyword
  • Java super keyword
  • Final Keyword in Java
  • static Keyword in Java
  • Static Import
  • Transient Keyword

Miscellaneous

  • newInstance() method
  • How does Hashmap works internally in Java
  • Java Ternary operator
  • How System.out.println() really work?
  • Autoboxing and Unboxing Examples
  • Serialization and Deserialization in Java with Example
  • Generate SerialVersionUID in Java
  • How to make a class Immutable in Java
  • Differences betwen HashMap and Hashtable
  • Difference between Enumeration and Iterator ?
  • Difference between fail-fast and fail-safe Iterator
  • Difference Between Interface and Abstract Class in Java
  • Difference between equals() and ==
  • Sort Objects in a ArrayList using Java Comparable Interface
  • Sort Objects in a ArrayList using Java Comparator

Follow

  • Coding Utils

Useful Links

  • Spring 4.1.x Documentation
  • Spring 3.2.x Documentation
  • Spring 2.5.x Documentation
  • Java 6 API
  • Java 7 API
  • Java 8 API
  • Java EE 5 Tutorial
  • Java EE 6 Tutorial
  • Java EE 7 Tutorial
  • Maven Repository
  • Hibernate ORM

About JavaInterviewPoint

javainterviewpoint.com is a tech blog dedicated to all Java/J2EE developers and Web Developers. We publish useful tutorials on Java, J2EE and all latest frameworks.

All examples and tutorials posted here are very well tested in our development environment.

Connect with us on Facebook | Privacy Policy | Sitemap

Copyright ©2023 · Java Interview Point - All Rights Are Reserved ·