{"id":2413,"date":"2016-04-07T09:09:58","date_gmt":"2016-04-07T13:09:58","guid":{"rendered":"http:\/\/springframework.guru\/?p=2413"},"modified":"2024-10-21T08:58:14","modified_gmt":"2024-10-21T12:58:14","slug":"using-log4j-2-spring-boot","status":"publish","type":"post","link":"https:\/\/springframework.guru\/using-log4j-2-spring-boot\/","title":{"rendered":"How to Use Log4J 2 with Spring Boot"},"content":{"rendered":"<p>Similar to accompanying application development activities, such as unit testing and documentation, logging is an integral part of any enterprise application. And Spring Boot applications are no different. Through logging, you get to see what the Spring Boot application code is really doing during monitoring, troubleshooting, and debugging. Spring Boot makes using a logging framework effortless. Out of the box, Spring Boot is very easy to use with <a title=\"Logback\" href=\"https:\/\/logback.qos.ch\/manual\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">Logback<\/a>. With Spring Boot&#8217;s default configuration, you can start writing out log messages\u00a0using the Logback framework.<\/p>\n<p><em>But what if you want to use Log4J 2?<\/em> Naturally, Spring Boot has support for Log4J 2 and a number of other popular logging frameworks. If you are new to Log4J 2, I wrote\u00a0an introductory post on Log4J 2 <a title=\"Log4J 2 Introduction\" href=\"http:\/\/springframework.guru\/introducing-log4j-enterprise-class-logging\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p>Configuring Spring Boot to use Log4J 2 is easy to do. In this post, I will explain how to configure Spring Boot to use Log4J 2 over the <a title=\"Simple Logging Facade for Java (SL4J)\" href=\"http:\/\/www.slf4j.org\/manual.html\" target=\"_blank\" rel=\"noopener noreferrer\">Simple Logging Facade for Java (SL4J)<\/a> logging fa\u00e7ade.<\/p>\n<h2>What is the SL4J Fa\u00e7ade?<\/h2>\n<p>Logging has been an important topic in the Java community for a long time. There is actually a fair amount of competition among the various logging frameworks. Determining which logging framework is the best has been a vigorously debated topic in the Java community. I&#8217;m not entering the &#8220;which logging framework is best&#8221; debate in this post. Each framework has its own merits and faults, and the landscape is always evolving.<\/p>\n<p>When developing enterprise class applications using the Spring Framework, a common theme is to avoid tight coupling. For example, using Spring&#8217;s dependency injection, it&#8217;s common to build an application which can use different data sources. It&#8217;s fairly common to use H2 in memory database for development and MySQL for your production database. This is made easy via the <a href=\"https:\/\/docs.oracle.com\/javase\/8\/docs\/api\/javax\/sql\/DataSource.html\" target=\"_blank\" rel=\"noopener noreferrer\">DataSource<\/a> interface, which is part of the standard Java API. H2 has an implementation of the DataSource interface. MySQL has an implementation of the DataSource interface. Through Spring configuration, we tell the IoC container which one we want to use at run time.<\/p>\n<p>Logging in Java has been the wild west for a long time.\u00a0Sun \/ Oracle did include a logging API as part of Java, but <a href=\"https:\/\/vectorbeta.wordpress.com\/2014\/08\/26\/the-java-logging-quagmire\/\" target=\"_blank\" rel=\"noopener noreferrer\">it never really caught on<\/a> in the Java community. I&#8217;m honestly having a hard time remembering ever seeing it used.<\/p>\n<p>While the Open Source community does offer a number of great logging solutions, as a developer, you don&#8217;t want to couple your code to a specific logging framework. This is where SLF4J steps in.<\/p>\n<p>SL4J is a fa\u00e7ade for commonly used logging frameworks, such as <a title=\"Java Util Logging\" href=\"http:\/\/docs.oracle.com\/javase\/7\/docs\/api\/java\/util\/logging\/package-summary.html\" target=\"_blank\" rel=\"noopener noreferrer\">Java Util Logging<\/a>, Log4J, Log4J 2, and Logback. As a developer, you write logging code against the SL4J API. At deployment time, you have the flexibility to plug-in your desired logging framework. This is made possible through an intermediate bridge\/adapter layer, like this.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/log4j2-sl4j-bridge.png\" rel=\"attachment wp-att-2414\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2414 size-full\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/log4j2-sl4j-bridge.png\" alt=\"Log4J 2 SL4J Bridge\" width=\"696\" height=\"214\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/log4j2-sl4j-bridge.png 696w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/log4j2-sl4j-bridge-300x92.png 300w\" sizes=\"(max-width: 696px) 100vw, 696px\" \/><\/a><\/p>\n<p>SL4J does not provide a complete logging solution. With SL4J, you cannot perform operations such as configuring appenders or setting logging levels. You perform such configurations through a configuration file of the logging framework in use. As the configuration file remains external to the application, no dependencies exist between your application code and the underlying logging framework.<\/p>\n<p>Should you not provide a logging configuration to SL4J, it won&#8217;t complain. You&#8217;re logging simply becomes a no-op operation. Which is nice since it won&#8217;t unintentionally cause errors in your application. But also something to be aware of, if you&#8217;re not getting logging output.<\/p>\n<figure id=\"attachment_4223\" aria-describedby=\"caption-attachment-4223\" style=\"width: 300px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/bit.ly\/2yhpu6x\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4223 size-medium\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02-300x156.png\" alt=\"Spring Framework 5\" width=\"300\" height=\"156\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02-300x156.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02.png 560w\" sizes=\"(max-width: 300px) 100vw, 300px\" \/><\/a><figcaption id=\"caption-attachment-4223\" class=\"wp-caption-text\">Click here to learn Spring Framework 5!<\/figcaption><\/figure>\n<h2>Maven Dependencies<\/h2>\n<p>To use Log4J 2 in a Spring Boot application, we need to add the required dependencies to the Maven POM. The required steps are:<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"1\">\n<li>Use the latest Spring Boot version. At the time of writing this post, the Spring Boot version is 1.3.3.RELEASE.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">. . .\n&lt;parent&gt;\n   &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n   &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n   &lt;version&gt;1.3.3.RELEASE&lt;\/version&gt;\n   &lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;\n&lt;\/parent&gt;\n. . .\n<\/pre>\n<p><strong>Note<\/strong>: Spring Boot 1.2.4.RELEASE contains a bug, <a title=\"Issue #3550\" href=\"https:\/\/github.com\/spring-projects\/spring-boot\/issues\/3550\" target=\"_blank\" rel=\"noopener noreferrer\">issue #3550<\/a>. When you define a log level through <strong>application.properties<\/strong>, the bug changes the parent logger level, and in worst case the root logger level. Although the bug was fixed in 1.2.6.RELEASE, I suggest using the 1.2.8.RELEASE if you want to stick with 1.2.x.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"2\">\n<li>Add the SL4J dependencies.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">. . .\n&lt;dependency&gt;\n   &lt;groupId&gt;org.slf4j&lt;\/groupId&gt;\n   &lt;artifactId&gt;slf4j-api&lt;\/artifactId&gt;\n   &lt;version&gt;1.7.19&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n   &lt;groupId&gt;org.apache.logging.log4j&lt;\/groupId&gt;\n   &lt;artifactId&gt;log4j-slf4j-impl&lt;\/artifactId&gt;\n   &lt;version&gt;2.5&lt;\/version&gt;\n&lt;\/dependency&gt;\n. . .\n<\/pre>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"3\">\n<li>Add the Log4J 2 dependencies.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">. . .\n&lt;dependency&gt;\n   &lt;groupId&gt;org.apache.logging.log4j&lt;\/groupId&gt;\n   &lt;artifactId&gt;log4j-api&lt;\/artifactId&gt;\n   &lt;version&gt;2.5&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n   &lt;groupId&gt;org.apache.logging.log4j&lt;\/groupId&gt;\n   &lt;artifactId&gt;log4j-core&lt;\/artifactId&gt;\n   &lt;version&gt;2.5&lt;\/version&gt;\n&lt;\/dependency&gt;\n&lt;dependency&gt;\n   &lt;groupId&gt;org.apache.logging.log4j&lt;\/groupId&gt;\n   &lt;artifactId&gt;log4j-jcl&lt;\/artifactId&gt;\n   &lt;version&gt;2.5&lt;\/version&gt;\n&lt;\/dependency&gt;\n. . .\n<\/pre>\n<p>In the configuration code above, the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">log4j-jcl<\/code> artifact is <a title=\"Apache Log4J Commons Logging Bridge\" href=\"https:\/\/logging.apache.org\/log4j\/2.12.x\/log4j-jcl\/index.html\" target=\"_blank\" rel=\"noopener noreferrer\">Apache Log4J Commons Logging Bridge<\/a>. It is required if you intend to route Spring Boot <strong>application.properties<\/strong> logging configuration to a Log4J 2 file appender.<\/p>\n<ol>\n<li style=\"list-style-type: none;\">\n<ol start=\"4\">\n<li>Exclude the built-in Logback dependency. This is necessary as Spring Boot will pick and use Logback if present in the classpath.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">. . .\n&lt;dependency&gt;\n   &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n   &lt;artifactId&gt;spring-boot-starter-security&lt;\/artifactId&gt;\n   &lt;exclusions&gt;\n      &lt;exclusion&gt;\n         &lt;groupId&gt;ch.qos.logback&lt;\/groupId&gt;\n         &lt;artifactId&gt;logback-classic&lt;\/artifactId&gt;\n      &lt;\/exclusion&gt;\n   &lt;\/exclusions&gt;\n&lt;\/dependency&gt;\n. . .\n<\/pre>\n<p>The complete Maven POM is this.<\/p>\n<h4>pom.xml<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;project xmlns=\"http:\/\/maven.apache.org\/POM\/4.0.0\" xmlns:xsi=\"http:\/\/www.w3.org\/2001\/XMLSchema-instance\"\n  xsi:schemaLocation=\"http:\/\/maven.apache.org\/POM\/4.0.0 http:\/\/maven.apache.org\/xsd\/maven-4.0.0.xsd\"&gt;\n  &lt;modelVersion&gt;4.0.0&lt;\/modelVersion&gt;\n\n  &lt;groupId&gt;guru.springframework&lt;\/groupId&gt;\n  &lt;artifactId&gt;blogposts&lt;\/artifactId&gt;\n  &lt;version&gt;0.0.1-SNAPSHOT&lt;\/version&gt;\n  &lt;packaging&gt;jar&lt;\/packaging&gt;\n\n  &lt;name&gt;Blog Posts&lt;\/name&gt;\n  &lt;description&gt;Misc Blog Posts&lt;\/description&gt;\n\n  &lt;parent&gt;\n    &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n    &lt;artifactId&gt;spring-boot-starter-parent&lt;\/artifactId&gt;\n    &lt;version&gt;1.2.3.RELEASE&lt;\/version&gt;\n    &lt;relativePath\/&gt; &lt;!-- lookup parent from repository --&gt;\n  &lt;\/parent&gt;\n\n  &lt;properties&gt;\n    &lt;project.build.sourceEncoding&gt;UTF-8&lt;\/project.build.sourceEncoding&gt;\n    &lt;start-class&gt;guru.springframework.blog.BlogPostsApplication&lt;\/start-class&gt;\n    &lt;java.version&gt;1.8&lt;\/java.version&gt;\n  &lt;\/properties&gt;\n\n  &lt;dependencies&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n      &lt;artifactId&gt;spring-boot-starter&lt;\/artifactId&gt;\n      &lt;exclusions&gt;\n        &lt;exclusion&gt;\n          &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n          &lt;artifactId&gt;spring-boot-starter-logging&lt;\/artifactId&gt;\n        &lt;\/exclusion&gt;\n      &lt;\/exclusions&gt;\n    &lt;\/dependency&gt;\n    \n    &lt;dependency&gt;\n      &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n      &lt;artifactId&gt;spring-boot-starter-test&lt;\/artifactId&gt;\n      &lt;scope&gt;test&lt;\/scope&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.apache.logging.log4j&lt;\/groupId&gt;\n      &lt;artifactId&gt;log4j-api&lt;\/artifactId&gt;\n      &lt;version&gt;2.5&lt;\/version&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.apache.logging.log4j&lt;\/groupId&gt;\n      &lt;artifactId&gt;log4j-core&lt;\/artifactId&gt;\n      &lt;version&gt;2.5&lt;\/version&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;com.h2database&lt;\/groupId&gt;\n      &lt;artifactId&gt;h2&lt;\/artifactId&gt;\n      &lt;scope&gt;runtime&lt;\/scope&gt;\n    &lt;\/dependency&gt;\n    &lt;dependency&gt;\n      &lt;groupId&gt;org.hibernate&lt;\/groupId&gt;\n      &lt;artifactId&gt;hibernate-core&lt;\/artifactId&gt;\n      &lt;version&gt;4.3.10.Final&lt;\/version&gt;\n    &lt;\/dependency&gt;\n  &lt;\/dependencies&gt;\n  &lt;build&gt;\n    &lt;plugins&gt;\n      &lt;plugin&gt;\n        &lt;groupId&gt;org.springframework.boot&lt;\/groupId&gt;\n        &lt;artifactId&gt;spring-boot-maven-plugin&lt;\/artifactId&gt;\n      &lt;\/plugin&gt;\n    &lt;\/plugins&gt;\n  &lt;\/build&gt;\n\n&lt;\/project&gt;<\/pre>\n<h2>Logging Messages in a Spring Boot Application<\/h2>\n<p>We will write log messages in the controller class of the Spring Boot application. As I have mentioned earlier, we will write log messages against the SL4J API. The code of the controller class is this.<\/p>\n<h4>IndexController.java<\/h4>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">package guru.springframework.blog.controllers;\n\n\nimport org.slf4j.Logger;\nimport org.slf4j.LoggerFactory;\nimport org.springframework.stereotype.Controller;\nimport org.springframework.web.bind.annotation.RequestMapping;\n\n@Controller\npublic class IndexController {\n    private final Logger logger = LoggerFactory.getLogger(this.getClass());\n    @RequestMapping(\"\/\")\n    String index(){\n        logger.debug(\"This is a debug message\");\n        logger.info(\"This is an info message\");\n        logger.warn(\"This is a warn message\");\n        logger.error(\"This is an error message\");\n        return \"index\";\n    }\n}<\/pre>\n<p>In the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">IndexController<\/code> class above, we used the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">LoggerFactory<\/code> and <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">Logger<\/code> classes of SL4J to log debug, info, warn, and error messages.<\/p>\n<h2>Configuring Logging in application.properties<\/h2>\n<p>In a Spring Boot application, you can externalize configuration to work with the same application code in different environments. The <strong>application.properties<\/strong> file is one of the many ways to externalize configuration. Let&#8217;s use it to externalize logging configuration.<\/p>\n<p>In the default structure of a Spring Boot web application, you can locate the <strong>application.properties<\/strong> file under the <strong>Resources<\/strong> directory. In the <strong>application.properties<\/strong> file, you can define log levels of Spring Boot, application loggers, Hibernate, Thymeleaf, and lots more. You can also define a log file to write log messages in addition to the console. An example of an <strong>application.properties<\/strong> file with logging configuration is this.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\">logging.level.org.springframework.web=INFO\nlogging.level.guru.springframework.blogs.controllers=INFO\nlogging.level.org.hibernate=ERROR\nlogging.file=logs\/spring-boot-logging.log\nlog4j.logger.org.thymeleaf=DEBUG\n<\/pre>\n<p>When you run the main class, <strong>INFO<\/strong> and higher level log messages of Spring Boot startup are logged to the console and the <strong>logs\/spring-boot-logging.log<\/strong> file.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file.png\" rel=\"attachment wp-att-2417\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2417 size-large\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file-1024x492.png\" alt=\"Spring Boot Startup Log Messages from Log4J 2\" width=\"863\" height=\"415\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file-1024x492.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file-300x144.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file-768x369.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file.png 1252w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<p><strong>Note<\/strong>: There is also a <strong>logging.path<\/strong> property to specify a path for a logging file. If you use it, Spring Boot creates a <strong>spring.log<\/strong> file in the specified path. However, you cannot specify both the <strong>logging.file<\/strong> and <strong>logging.path<\/strong> properties together. If specified together, Spring Boot will ignore both.<\/p>\n<p>Once the application starts, access it with the URL, <strong>http:\/\/localhost:8080<\/strong>. Spring Boot outputs <strong>INFO<\/strong> and higher level log messages of <strong>IndexController<\/strong> to the console and file.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file2.png\" rel=\"attachment wp-att-2418\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-2418 size-large\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file2-1024x492.png\" alt=\"Application Log Messages in Console and File from Log4J 2\" width=\"863\" height=\"415\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file2-1024x492.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file2-300x144.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file2-768x369.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/properties_configuration_console_file2.png 1249w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<h2>Using a Log4J 2 Configuration File<\/h2>\n<p>Log4J 2 configuration through <strong>application.properties<\/strong> file might be sufficient for simple applications. But, enterprise applications have far more complex logging requirements. Log4J 2 supports advanced logging configurations through configuration files, such as properties, XML, JSON, and YAML. I have explained how to configure Log4J 2 using XML <a title=\"Log4J 2 Configuration: using XML\" href=\"http:\/\/springframework.guru\/log4j-2-configuration-using-xml\/\" target=\"_blank\" rel=\"noopener noreferrer\">here<\/a>.<\/p>\n<p>In a Spring Boot application, you can specify a Log4J 2 XML configuration file as <strong>log4j2.xml<\/strong> or <strong>log4j2-spring.xml<\/strong> in the project classpath. The Spring Boot team however recommends using the <strong>-spring<\/strong> variant for your logging configuration. That is <strong>log4j2-spring.xml<\/strong> rather than <strong>log4j2.xml<\/strong>. If you use the standard <strong>log4j2.xml<\/strong> configuration, Spring Boot might not be able to completely control log initialization.<\/p>\n<p>The code of the <strong>log4j2-spring.xml<\/strong> file, is this.<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"java\">&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n&lt;Configuration monitorInterval=\"60\"&gt;\n    &lt;Properties&gt;\n        &lt;Property name=\"log-path\"&gt;applogs&lt;\/Property&gt;\n    &lt;\/Properties&gt;\n    &lt;Appenders&gt;\n        &lt;Console name=\"Console-Appender\" target=\"SYSTEM_OUT\"&gt;\n            &lt;PatternLayout&gt;\n                &lt;pattern&gt;\n                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n\n                &lt;\/pattern&gt;&gt;\n            &lt;\/PatternLayout&gt;\n        &lt;\/Console&gt;\n        &lt;File name=\"App-File-Appender\" fileName=\"${log-path}\/app_log.log\" &gt;\n            &lt;PatternLayout&gt;\n                &lt;pattern&gt;\n                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n\n                &lt;\/pattern&gt;\n            &lt;\/PatternLayout&gt;\n        &lt;\/File&gt;\n        &lt;File name=\"SpringBoot-File-Appender\" fileName=\"${log-path}\/springboot_log.log\" &gt;\n            &lt;PatternLayout&gt;\n                &lt;pattern&gt;\n                    [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n\n                &lt;\/pattern&gt;\n            &lt;\/PatternLayout&gt;\n        &lt;\/File&gt;\n    &lt;\/Appenders&gt;\n    &lt;Loggers&gt;\n        &lt;Logger name=\"org.springframework.web\" level=\"info\" additivity=\"false\"&gt;\n            &lt;AppenderRef ref=\"SpringBoot-File-Appender\"\/&gt;\n            &lt;AppenderRef ref=\"Console-Appender\"\/&gt;\n        &lt;\/Logger&gt;\n        &lt;Logger name=\"guru.springframework.blog.controllers\" level=\"info\" additivity=\"false\"&gt;\n            &lt;AppenderRef ref=\"App-File-Appender\"\/&gt;\n            &lt;AppenderRef ref=\"Console-Appender\"\/&gt;\n         &lt;\/Logger&gt;\n        &lt;Root&gt;\n            &lt;AppenderRef ref=\"Console-Appender\"\/&gt;\n        &lt;\/Root&gt;\n    &lt;\/Loggers&gt;\n&lt;\/Configuration&gt;<\/pre>\n<p>In the configuration code above:<\/p>\n<ul>\n<li>Line 6 &#8211; Line 28: We defined a console appender and two file appenders.<\/li>\n<li>Line 30 &#8211; Line 33: We configured Spring Boot logging to log <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">INFO<\/code> and higher level messages to the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">springboot_log.log<\/code> file and the console.<\/li>\n<li>Line 34 &#8211; Line 37: We configured the loggers of the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">guru.springframework.blog.controllers<\/code> package and its sub packages to log <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">INFO<\/code> and higher level messages to the <code class=\"EnlighterJSRAW\" data-enlighter-language=\"java\" data-enlighter-theme=\"git\" data-enlighter-linenumbers=\"false\">app_log.log<\/code> file and the console.<\/li>\n<\/ul>\n<p><strong>Note<\/strong>: Spring Boot expects the <strong>log4j2-spring.xml<\/strong> configuration file to be on the classpath. However, you can store it in a different location and point to it using the <strong>logging.config<\/strong> property in <strong>application.properties<\/strong>.<\/p>\n<p>When you run and access the application now, Spring Boot loads the <strong>log42-spring.xml<\/strong> file and uses the configuration to log messages to the <strong>springboot_log.log<\/strong> file, <strong>app_log.log<\/strong> file, and console.<br \/>\n<a href=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/xml_configuration_console_file.png\" rel=\"attachment wp-att-2419\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter size-large wp-image-2419\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/xml_configuration_console_file-1024x489.png\" alt=\"XML Logging Configuration Output\" width=\"863\" height=\"412\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/xml_configuration_console_file-1024x489.png 1024w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/xml_configuration_console_file-300x143.png 300w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/xml_configuration_console_file-768x367.png 768w, https:\/\/springframework.guru\/wp-content\/uploads\/2016\/04\/xml_configuration_console_file.png 1250w\" sizes=\"(max-width: 863px) 100vw, 863px\" \/><\/a><\/p>\n<h2>Summary<\/h2>\n<p>Earlier in this post, I mentioned about the Log4J 2 supported configurations. I have written the following posts for each of the configuration which\u00a0also apply\u00a0to Spring Boot applications.<\/p>\n<ul>\n<li><a title=\"Log4J 2 Configuration: Using Properties File\" href=\"http:\/\/springframework.guru\/log4j-2-configuration-using-properties-file\/\" target=\"_blank\" rel=\"noopener noreferrer\">Log4J 2 Configuration: Using Properties File<\/a><\/li>\n<li><a title=\"Log4J 2 Configuration: Using XML\" href=\"http:\/\/springframework.guru\/log4j-2-configuration-using-xml\/\" target=\"_blank\" rel=\"noopener noreferrer\">Log4J 2 Configuration: Using XML<\/a><\/li>\n<li><a title=\"Log4J 2 Configuration: Using JSON\" href=\"http:\/\/springframework.guru\/log4j-2-configuration-using-json\/\" target=\"_blank\" rel=\"noopener noreferrer\">Log4J 2 Configuration: Using JSON<\/a><\/li>\n<li><a title=\"Log4J 2 Configuration: Using YAML\" href=\"http:\/\/springframework.guru\/log4j-2-configuration-using-yaml\/\" target=\"_blank\" rel=\"noopener noreferrer\">Log4J 2 Configuration: Using YAML<\/a><\/li>\n<\/ul>\n<p>Which configuration to use is largely a matter of personal preferences and your logging needs. If your needs are fairly simple, often using the properties file is all you&#8217;ll need. But if you need to access more advanced logging features, you&#8217;ll need to use XML, JSON, or YAML. XML is structured and IDE friendly. But JSON\/YAML are more fun to write in.<\/p>\n<p>When it comes to adding\u00a0logging statements to your code, there are always questions, such as \u201c<em>What to log and what not to?<\/em>\u201d, \u201c<em>How much to log?<\/em>\u201d, and so on. There are no concrete answers. You should resist the tendency to log everything because logging means more code and that comes with overhead. Also the more you log, the more difficult it becomes to locate specific log messages when you later need them. Instead, write logs as demonstrated by specific, verifiable needs.<\/p>\n<p>You can find the code of this post on <a href=\"https:\/\/github.com\/springframeworkguru\/blogposts\/tree\/log4j2-springboot\" target=\"_blank\" rel=\"noopener\">Github<\/a><\/p>\n<figure id=\"attachment_4223\" aria-describedby=\"caption-attachment-4223\" style=\"width: 560px\" class=\"wp-caption aligncenter\"><a href=\"http:\/\/bit.ly\/2yhpu6x\" target=\"_blank\" rel=\"noopener noreferrer\"><img loading=\"lazy\" decoding=\"async\" class=\"wp-image-4223 size-full\" src=\"http:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02.png\" alt=\"Spring Framework 5\" width=\"560\" height=\"292\" srcset=\"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02.png 560w, https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/ReactiveIsComing2NewSmall02-300x156.png 300w\" sizes=\"(max-width: 560px) 100vw, 560px\" \/><\/a><figcaption id=\"caption-attachment-4223\" class=\"wp-caption-text\">Learn Spring Framework 5!<\/figcaption><\/figure>\n","protected":false},"excerpt":{"rendered":"<p>Similar to accompanying application development activities, such as unit testing and documentation, logging is an integral part of any enterprise application. And Spring Boot applications are no different. Through logging, you get to see what the Spring Boot application code is really doing during monitoring, troubleshooting, and debugging. Spring Boot makes using a logging framework [&hellip;]<a href=\"https:\/\/springframework.guru\/using-log4j-2-spring-boot\/\" class=\"df-link-excerpt\">Continue reading<\/a><\/p>\n","protected":false},"author":1,"featured_media":4575,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_lmt_disableupdate":"","_lmt_disable":"","_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"Using Log4J 2 with Spring Boot #springboot #java","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":true,"jetpack_social_options":{"image_generator_settings":{"template":"highway","enabled":false},"version":2}},"categories":[20,161,104],"tags":[160,29],"class_list":["post-2413","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-java","category-log4j2","category-spring-boot","tag-log4j-2","tag-spring-boot"],"jetpack_publicize_connections":[],"aioseo_notices":[],"modified_by":"jt","jetpack_sharing_enabled":true,"jetpack_featured_media_url":"https:\/\/springframework.guru\/wp-content\/uploads\/2018\/06\/NewBannerBOOTSWeb.jpg","jetpack_shortlink":"https:\/\/wp.me\/p5BZrZ-CV","_links":{"self":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2413"}],"collection":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/comments?post=2413"}],"version-history":[{"count":11,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2413\/revisions"}],"predecessor-version":[{"id":8262,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/posts\/2413\/revisions\/8262"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media\/4575"}],"wp:attachment":[{"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/media?parent=2413"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/categories?post=2413"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/springframework.guru\/wp-json\/wp\/v2\/tags?post=2413"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}