Not Able to Read Array Property From Spring Boot Yaml
Log4J 2 is a logging framework designed to address the logging requirements of enterprise applications. Its predecessor Log4J 1.10 has been around for more than one and a half decade and is still one of the most widely used Java logging framework. Log4J has even been ported to the .Net world. Log4net is one of the most popular logging frameworks for Microsoft's .Internet environment.
Log4J two goes steps ahead by removing the inherent architectural flaws of Log4J i.10. Since the initial release of Log4J 2 on August 2015, it's quickly being adopted by the programmer community. I wrote an introductory mail on Log4J 2 here. If you have not read information technology, I recommend starting with the introductory post first. In this post, I will hash out how to configure Log4J 2 using a backdrop configuration file. This is but one of several ways you can configure Log4J 2.
What are Log4J 2 Configuration Files?
Log4J 2 provides various components, such equally loggers, appenders, and layouts that piece of work together to perform logging in an awarding. Equally different applications accept different logging requirements, you're able configure LogJ 2 accordingly. Likewise, you will frequently need to go along changing Log4J two configurations of an application across its deployment lifecycle. For case, it is common to gear up the logging level to DEBUG during development, and afterward switch information technology to Error to avoid filling your logs with excessive debug information. Similarly, during local evolution, y'all can work with the panel appender to avert file I/O overheads and in other deployment environments, ready a file appender or some other persistent destination to preserve log messages.
You can configure Log4J 2 either programmatically in your application or through configuration files, such as backdrop, XML, JSON, and YAML residing on your projection classpath. Through the utilize of configuration files, y'all have the flexibility of irresolute the various configuration options without modifying your awarding lawmaking. In this post we're going to look at using backdrop file.
Setting upward Log4J 2 to Use Properties File
Dissimilar its predecessor Log4J 1.10, Log4J 2 did not support configuration through backdrop file when information technology was initially released. It was from Log4J ii.four that support for backdrop file was again added, but with a completely different syntax.
Log4J4 Maven Dependencies
To apply Log4J 2 in your application, you lot need to ensure that the Log4J ii jars are on your project classpath. If you intend to use properties file, requite actress attention to ensure that yous have the Log4J 2.iv or greater jars on the classpath. Otherwise, your properties file will not become picked.
When using Maven, specify the following Log4J two dependencies.
. . . <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>2.five</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>ii.5</version> </dependency> . . .
Log4J ii Spring Boot Dependencies
If you want to employ Log4J 2 in a Spring Kicking projection, things can be a fleck tricky. Simply adding the dependencies above won't piece of work equally Spring Kicking will first detect the default Logback classic on the classpath, and volition employ information technology. Therefore, you need to exclude the default dependency of the Spring Boot starter on Logback classic, and instead include the Spring Kick starter dependency on Log4J two, similar this.
. . . <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-kick-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-kick-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.springframework.kicking</groupId> <artifactId>spring-boot-starter-log4j2</artifactId> </dependency> . . .
This will configure Bound Kick to apply Log4J two, but with a catch – You still won't be able to utilise properties file for configuration. As of Leap Boot ane.iii.3 Release, Jump Boot starter dependency on Log4J ii is for Log4J 2.1, and every bit I have already mentioned it is from Log4J 2.4 onward that properties file is supported. Therefore, you demand to explicitly specify dependencies of Log4J ii.4 or above after excluding Spring Boot starter logging, similar this.
. . . <dependency> <groupId>org.springframework.kicking</groupId> <artifactId>spring-kick-starter</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-kicking-starter-logging</artifactId> </exclusion> </exclusions> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-api</artifactId> <version>ii.5</version> </dependency> <dependency> <groupId>org.apache.logging.log4j</groupId> <artifactId>log4j-core</artifactId> <version>2.5</version> </dependency> . . .
The above dependencies volition ready Log4J ii to use properties file in a Spring Boot application.
Configuring Log4J two using Properties File
Past default, Log4J 2 looks for a properties file with the name log4j2.properties in the classpath. In a Spring Boot awarding, the log4j2.properties file will typically exist in the resources folder.
Before nosotros start configuring Log4J 2, we will write a Coffee class to generate log messages via Log4J 2.
Log4J2PropertiesConf.coffee
//parcel guru.springframework.blog.log4j2properties; import org.apache.logging.log4j.LogManager; import org.apache.logging.log4j.Logger; public class Log4J2PropertiesConf { private static Logger logger = LogManager.getLogger(); public void performSomeTask(){ logger.debug("This is a debug message"); logger.info("This is an info bulletin"); logger.warn("This is a warn message"); logger.fault("This is an mistake message"); logger.fatal("This is a fatal message"); } }
To test the Log4J2PropertiesConf
class above, we will write a JUnit test form.
Log4J2PropertiesConfTest.java
//404: Not Found
We volition now configure Log4J 2 using a backdrop file. Similar any other Java backdrop file, a log4j2.properties file are a set of key value pairs with options to configure the diverse components of Log4J 2, such as loggers, appenders, and layouts. A bones log4j2.properties file starts with a proper name, optional backdrop to be used in other parts of the file, and appender declarations.
name=PropertiesConfig belongings.filename = logs appenders = console, file . . .
The preceding code declares two appenders, named panel
and file
. Side by side, let's configure both the appenders to write log messages to the console and a file. The configuration code for the appenders is this.
. . . appender.console.blazon = Panel appender.console.name = STDOUT appender.console.layout.blazon = PatternLayout appender.console.layout.pattern = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{ane} - %msg%due north appender.file.type = File appender.file.proper name = LOGFILE appender.file.fileName=${filename}/propertieslogs.log appender.file.layout.type=PatternLayout appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n . . .
In the lawmaking to a higher place nosotros configured ii appenders: One to write log messages to the console and the other to a log file. Both the appenders use pattern layouts that are configurable with conversion blueprint strings to format log letters. The appender.panel.layout.pattern
property specifies the pattern string. You can larn more about the design layout and conversion pattern strings hither. For the file appender, nosotros used the appender.file.fileName
property to specify the name and location of the log file that Log4J 2 will generate. Here, notice the ${filename}
declaration that nosotros used as a substitution for the property.filename
property we declared earlier.
Adjacent we volition configure the loggers, starting from the root logger.
. . . rootLogger.level = debug rootLogger.appenderRefs = stdout rootLogger.appenderRef.stdout.ref = STDOUT . . .
In the code above, nosotros configured the root logger to log debug and its lower level messages to the console (stdout). When nosotros run the Log4J2PropertiesConfTest exam class, the output in the IntelliJ console will exist similar to this.
The complete log4j2.properties file is this.
log4j2.properties
//name=PropertiesConfig property.filename = logs appenders = console, file appender.console.type = Console appender.console.proper name = STDOUT appender.console.layout.type = PatternLayout appender.console.layout.blueprint = [%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{ane} - %msg%n appender.file.type = File appender.file.name = LOGFILE appender.file.fileName=${filename}/propertieslogs.log appender.file.layout.type=PatternLayout appender.file.layout.pattern=[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n loggers=file logger.file.name=guru.springframework.blog.log4j2properties logger.file.level = debug logger.file.appenderRefs = file logger.file.appenderRef.file.ref = LOGFILE rootLogger.level = debug rootLogger.appenderRefs = stdout rootLogger.appenderRef.stdout.ref = STDOUT
When we run the Log4J2PropertiesConfTest test course now, log letters will be sent to the logs/propertieslogs.log past the file logger and additively to the panel by the root logger. The following effigy shows the log messages sent to the file and console in IntelliJ.
In the instance above, it is due to logger additivity that caused log messages to be sent to the file past the logger and additively to the console by the root logger. You tin can override this default behavior past setting the additivity flag of a logger to faux.
. . . logger.file.additivity = false . . .
The property above configures our file appender and so that it is no longer additive. Thus, log messages will only be sent to the file.
Appender additivity can exist somewhat disruptive. I propose reviewing the Log4J 2 documentation on the subject, where they accept some proficient examples how this works.
Summary
Using properties file is i of the several options you have to configure Log4J two. Log4J 2 is gradually moving to XML configuration and the new JSON and YAML configurations. Properties configuration cannot handle some avant-garde features, such every bit custom error handlers, time-based rolling policies, nested appenders, and special types of appenders, such as async appenders. However, backdrop configuration is still existence extensively used. Ofttimes y'all don't demand many of the more advanced logging features of Log4J 2. And then you lot're fine using the simplicity of the properties file configuration.
In time to come posts, I will cover using other configuration options for Log4J 2. This is to address logging configurations with more than complex requirements.
Source: https://springframework.guru/log4j-2-configuration-using-properties-file/
Post a Comment for "Not Able to Read Array Property From Spring Boot Yaml"