Getting started with log4j version-2 using JSON and YAML based configuration.
This is a minimalist introduction to how you can setup the log4j logging framework using the JSON file or YAML file.
Contents
1. Maven Dependencies
In order for log4j2 to work you need to add the following two dependencies that adds the API to the application.
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.11.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.11.1</version>
</dependency>
Now since we want to configure log4j2 using JSON file we also need to add the jackson parser to support the JSON parsing.
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.4</version>
</dependency>
module MyModuleName {
requires org.apache.logging.log4j;
}
2. Logging Configuration Examples
We will look at how we can configure logger for console and rolling files. Rolling files mean that after reaching a certain size framework will append logs into another file
Console configuration example
{
"configuration": {
"status": "error",
"name": "Swing Logging",
"packages": "com.deep",
"ThresholdFilter": {
"level": "TRACE"
},
"appenders": {
"Console": {
"name": "STDOUT",
"target": "SYSTEM_OUT",
"PatternLayout": {
"pattern": "%d [%t] %highlight{ %-5level }{FATAL=bg_red bright black, ERROR=BG_RED bright black, WARN=bg_yellow bright black, INFO=bg_white bright black, TRACE=bg_white bright black, DEBUG=bg_blue black} %highlight{%msg%n%throwable}{FATAL=red, ERROR=red, WARN=yellow, INFO=normal, TRACE=normal, DEBUG=blue}",
"disableAnsi": "false"
}
}
},
"loggers": {
"root": {
"level": "TRACE",
"AppenderRef": {
"ref": "STDOUT"
}
}
}
}
}
JSON Configuration for logging into rolling files.
{
"configuration": {
"status": "error",
"name": "Swing Logging",
"packages": "com.deep",
"ThresholdFilter": {
"level": "TRACE"
},
"appenders": {
"RollingFile": {
"name": "ROLLINGFILES",
"fileName": "${env:TEMP}/Gagan.log",
"filePattern": "${env:TEMP}/Gagan-backup-%d{MM-dd-yy-HH-mm-ss}-%i.log.gz",
"PatternLayout": {
"pattern": "[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS (zzz)} [%t] %c{1} (%L) - %msg%xEx%n"
},
"Policies": {
"SizeBasedTriggeringPolicy": {
"size": "256KB"
}
},
"DefaultRolloverStrategy": {
"max": "90"
}
}
},
"loggers": {
"root": {
"level": "TRACE",
"AppenderRef": {
"ref": "ROLLINGFILES"
}
}
}
}
}
Output for the rolling file looks like this
3. Understanding Pattern layout
Let's look at some of the labels defined and what they mean in the pattern defined above.
| Pattern | Description |
|---|---|
| %level | Outputs the level of the logging event. |
| %d | Outputs the date of the logging event |
| %t | Outputs the ID of the thread that generated the logging event. |
| %c | Outputs the name of the logger that published the logging event. |
| %L | Outputs the line number from where the logging request was issued. |
| %msg | Outputs the application supplied message associated with the logging event. |
| %throwable | Outputs the Throwable trace bound to the logging event. |
| %xEx | The same as the %throwable conversion word but also includes class packaging information. |
| %n | Outputs the platform dependent line separator character or characters. |
4. Stylizing with colors
If you want to stylize your output with text having background color or you feel like changing the color of the text itself, you can make use of the %higlight pattern.
Example:
%highlight{ %-5level }{FATAL=bg_red bright black, ERROR=BG_RED bright black, WARN=bg_yellow bright black, INFO=bg_white bright black, TRACE=bg_white bright black, DEBUG=bg_blue black}
Here we are giving a background color to the %level pattern by appending the %higlight.
5. Using the Logger in class
Declaring the logger has changed a bit. You can use the following statement.
private static final Logger LOG = LogManager.getLogger(Log4j2.class);



No comments:
Post a Comment