【搬运】Get Log Output in JSON,通过Log4j2与Logback输出JSON格式日志


最近在做日志组件输出JSON到MQ这一块,找了很多官方文档,如Log4j2、Spring Projects、Spring AMQP Simples等等,勉强写出了输出的配置,但是Elasticsearch是存储JSON的,为了简化JSON在Logstash处的处理,决定使用日志框架Log4j2输出JSON数据,当我按其他文档把这一块写完后,帮人解决Logback似问题时,发现了如下这篇博客,鉴于其内容简单易懂,而且只能通过网*才能看得到,就直接搬运了过来,原文地址:https://www.baeldung.com/java-log-json-output

Baeldung是由Eugen运营维护的编程博客,目前它成为了关于Java编程语言和相关技术的最热门的博客之一。 Eugen是一个充满激情的教育家,他发布了许多有用的课程,包括了关于REST With Spring、Jackson JSON的教程和Spring Security的学习资料。


Get Log Output in JSON

1. Introduction

Most Java logging libraries today offer different layout options for formatting logs – to accurately fit the needs of each project.

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.

Both use Jackson internally for representing logs in the JSON format.

For an introduction to these libraries take a look at our introduction to Java Logging article.

2. Log4j2

Log4j2 is the direct successor of the most popular logging library for Java, Log4J.

As it's the new standard for Java projects, we'll show how to configure it to output JSON.

2.1. Dependencies

First, we have to include the following dependencies in our *pom.*xml file:


The latest versions of the previous dependencies can be found on Maven Central: [log4j-api](https://search.maven.org/classic/#search|gav|1|g%3A"org.apache.logging.log4j" AND a%3A"log4j-api"), [log4j-core](https://search.maven.org/classic/#search|gav|1|g%3A"org.apache.logging.log4j" AND a%3A"log4j-core"), [jackson-databind.](https://search.maven.org/classic/#search|gav|1|g%3A"com.fasterxml.jackson.core" AND a%3A"jackson-databind")

2.2. Configuration

Then, in our *log4j2.*xml file, we can create a new Appender that uses JsonLayout and a new Logger that uses this Appender:

    <Console name="ConsoleJSONAppender" target="SYSTEM_OUT">
        <JsonLayout complete="false" compact="false">
            <KeyValuePair key="myCustomField" value="myCustomValue" />
<Logger name="CONSOLE_JSON_APPENDER" level="TRACE" additivity="false">
    <AppenderRef ref="ConsoleJSONAppender" />

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.

Setting the compact parameter to false will increase the size of the output but will make it also more human readable.

2.3. Using Log4j2

In our code, we can now instantiate our new JSON logger and make a new debug level trace:

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

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.

So, let's see how to configure it to get the output of the logs in JSON format.

3.1. Dependencies

Let's include following dependencies in our pom.xml:


We can check here for the latest versions of these dependencies: [logback-classic](https://search.maven.org/classic/#search|ga|1|g%3A"ch.qos.logback" AND a%3A"logback-classic"), [logback-json-classic](https://search.maven.org/classic/#search|ga|1|g%3A"ch.qos.logback.contrib" AND a%3A"logback-json-classic"), [logback-jackson](https://search.maven.org/classic/#search|ga|1|g%3A"ch.qos.logback.contrib" AND a%3A"logback-jackson"), [jackson-databind](https://search.maven.org/classic/#search|ga|1|g%3A"com.fasterxml.jackson.core" AND a%3A"jackson-databind")

3.2. Configuration

First, we create a new appender in our logback.xml that uses JsonLayout and JacksonJsonFormatter.

After that, we can create a new logger that uses this appender:

<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
    <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
        <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
<logger name="jsonLogger" level="TRACE">
    <appender-ref ref="json" />

As we see, the parameter prettyPrint is enabled to obtain a human-readable JSON.

3.3. Using 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

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.

As always the code for this article is available on GitHub here and here.

posted @ 2020-03-03 09:12  东北小狐狸  阅读(...)  评论(...编辑  收藏