1. Introduction
1.介绍
Most Java logging libraries today offer different layout options for formatting logs – to accurately fit the needs of each project.
如今,大多数Java日志库提供了不同的日志格式化选项–以准确地适应每个项目的需要。
In this quick article, we want to format and output our log entries as JSON. We’ll see how to do this for the two most widely used logging libraries: Log4j2 and Logback.
在这篇快速文章中,我们想把我们的日志条目格式化并输出为JSON。我们将看到如何为两个最广泛使用的日志库做到这一点。Log4j2和Logback。
Both use Jackson internally for representing logs in the JSON format.
两者都在内部使用Jackson来代表JSON格式的日志。
For an introduction to these libraries take a look at our introduction to Java Logging article.
关于这些库的介绍,请看我们的Java Logging介绍文章。
2. Log4j2
2.Log4j2
Log4j2 is the direct successor of the most popular logging library for Java, Log4J.
Log4j2是Java中最受欢迎的日志库Log4J的直接继承者。
As it’s the new standard for Java projects, we’ll show how to configure it to output JSON.
由于它是Java项目的新标准,我们将展示如何配置它来输出JSON。
2.1. Dependencies
2.1.依赖性
First, we have to include the following dependencies in our pom.xml file:
首先,我们必须在我们的pom.xml文件中包括以下依赖项。
<dependencies>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.13.0</version>
</dependency>
</dependencies>
The latest versions of the previous dependencies can be found on Maven Central: log4j-api, log4j-core, jackson-databind.
之前依赖的最新版本可以在Maven中心找到。log4j-api, log4j-core, jackson-databind.
2.2. Configuration
2.2.配置
Then, in our log4j2.xml file, we can create a new Appender that uses JsonLayout and a new Logger that uses this Appender:
然后,在我们的log4j2.xml文件中,我们可以创建一个新的Appender,使用JsonLayout和一个新的Logger,使用这个Appender。
<Appenders>
<Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
<JsonLayout complete="false" compact="false">
<KeyValuePair key="myCustomField" value="myCustomValue" />
</JsonLayout>
</Console>
</Appenders>
<Logger name="CONSOLE_JSON_APPENDER" level="TRACE" additivity="false">
<AppenderRef ref="ConsoleJSONAppender" />
</Logger>
As we can see in the example config, it’s possible to add our own values to the log using KeyValuePair, that even supports lookout into the log context.
正如我们在配置示例中所看到的,可以使用KeyValuePair将我们自己的值添加到日志中,这甚至支持对日志上下文的查看。
Setting the compact parameter to false will increase the size of the output but will make it also more human readable.
将compact参数设置为false会增加输出的大小,但也会使其更容易被人阅读。
2.3. Using Log4j2
2.3.使用Log4j2
In our code, we can now instantiate our new JSON logger and make a new debug level trace:
在我们的代码中,我们现在可以实例化我们新的JSON记录器,并做一个新的调试级别跟踪。
Logger logger = LogManager.getLogger("CONSOLE_JSON_APPENDER");
logger.debug("Debug message");
The debug output message for the previous code would be:
前面代码的调试输出信息将是。
{
"timeMillis" : 1513290111664,
"thread" : "main",
"level" : "DEBUG",
"loggerName" : "CONSOLE_JSON_APPENDER",
"message" : "My debug message",
"endOfBatch" : false,
"loggerFqcn" : "org.apache.logging.log4j.spi.AbstractLogger",
"threadId" : 1,
"threadPriority" : 5,
"myCustomField" : "myCustomValue"
}
3. Logback
3.回程
Logback can be considered another successor of Log4J. It’s written by same developers and claims to be more efficient and faster than its predecessor.
Logback可以说是Log4J的另一个继承者。它由相同的开发人员编写,并声称比其前辈更有效、更快速。
So, let’s see how to configure it to get the output of the logs in JSON format.
因此,让我们看看如何配置它以获得JSON格式的日志输出。
3.1. Dependencies
3.1.依赖性
Let’s include following dependencies in our pom.xml:
让我们在我们的pom.xml中包括以下依赖项。
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.2.6</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-json-classic</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.3</version>
</dependency>
</dependencies>
We can check here for the latest versions of these dependencies: logback-classic, logback-json-classic, logback-jackson, jackson-databind
我们可以在这里检查这些依赖项的最新版本。logback-classic, logback-json-classic, logback-jackson, jackson-databind.
3.2. Configuration
3.2.配置
First, we create a new appender in our logback.xml that uses JsonLayout and JacksonJsonFormatter.
首先,我们在logback.xml中创建一个新的appender,它使用JsonLayout和JacksonJsonFormatter。
After that, we can create a new logger that uses this appender:
之后,我们可以创建一个新的logger,使用这个appender。
<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
<jsonFormatter
class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
<prettyPrint>true</prettyPrint>
</jsonFormatter>
<timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
</layout>
</appender>
<logger name="jsonLogger" level="TRACE">
<appender-ref ref="json" />
</logger>
As we see, the parameter prettyPrint is enabled to obtain a human-readable JSON.
正如我们所看到的,参数prettyPrint被启用,以获得一个人类可读的JSON。
3.3. Using Logback
3.3.使用Logback
Let’s instantiate the logger in our code and log a debug message:
让我们在代码中实例化日志器,并记录一条调试信息。
Logger logger = LoggerFactory.getLogger("jsonLogger");
logger.debug("Debug message");
With this – we’ll obtain the following output:
有了这个–我们将获得以下输出。
{
"timestamp" : "2017-12-14 23:36:22.305",
"level" : "DEBUG",
"thread" : "main",
"logger" : "jsonLogger",
"message" : "Debug log message",
"context" : "default"
}
4. Conclusion
4.结论
We’ve seen here how we can easily configure Log4j2 and Logback have a JSON output format. We’ve delegated all the complexity of the parsing to the logging library, so we don’t need to change any existing logger calls.
我们在这里看到,我们可以轻松地配置Log4j2和Logback有一个JSON输出格式。我们已经将所有复杂的解析工作委托给了日志库,所以我们不需要改变任何现有的日志器调用。
As always the code for this article is available on GitHub here and here.