简体   繁体   English

将JsonLayout添加到log4j2 json配置

[英]Add JsonLayout to log4j2 json config

I'm trying to redirect an output of log in a JSON format, so, I have a Json configuration for log4j2. 我正在尝试以JSON格式重定向日志的输出,因此,我有一个log4j2的Json配置。 I know that I should use the JsonLayout, but I didn't find any way to put this on my configuration. 我知道我应该使用JsonLayout,但是我没有找到任何将其放在配置中的方法。 That's my log4j2.json: 那是我的log4j2.json:

{
  "Configuration": {
    "status": "info",
    "Appenders": {
      "Console": {
        "name": "Console",
        "target": "SYSTEM_OUT",
        "PatternLayout": {
          "Pattern": "%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} [%t] %-5level [%logger{1.}] - %msg%n"
        }
      }
    },
    "Loggers": {
      "Root": {
        "level": "info",
        "AppenderRef": [
          {
            "ref": "Console"
          }
        ]
      }
    }
  }
}

Could you help me to add this to my configuration file? 您能帮我将其添加到我的配置文件吗?

Here's something working example, You simply need JSONLayout which is an impl of Layout for json event logging. 这是一个JSONLayout示例,您只需要JSONLayout ,这是JSON事件日志记录的Layout JSONLayout

{
  "configuration": {
    "name": "log-enrichment",
    "appenders": {
      "RollingFile": {
        "name": "rollingFile",
        "fileName": "enrichment.log",
        "filePattern": "%d{MM-dd-yy-HH-mm-ss}-%i.log",
        "JSONLayout": {
          "complete": false,
          "compact": false,
          "eventEol": true
        },
        "SizeBasedTriggeringPolicy": {
          "size": "100 MB"
        },
        "DefaultRolloverStrategy": {
          "max": "5"
        }
      }
    },
    "loggers": {
      "root": {
        "level": "DEBUG",
        "appender-ref": {
          "ref": "rollingFile"
        }
      }
    }
  }
}

Here is an example. 这是一个例子。 You should look at http://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout for the options you can use. 您应该查看http://logging.apache.org/log4j/2.x/manual/layouts.html#JSONLayout中可以使用的选项。 Also, the configuration file does not have to be in JSON to use the JsonLayout. 另外,配置文件不必使用JSON即可使用JsonLayout。 It alos could be XML, YAML, or a properties file. 它也可以是XML,YAML或属性文件。

{
  "Configuration": {
    "status": "info",
    "Appenders": {
      "Console": {
        "name": "Console",
        "target": "SYSTEM_OUT",
        "PatternLayout": {
          "Pattern": "%d{yyyy-MM-dd'T'HH:mm:ss.SSSXXX} [%t] %-5level [%logger{1.}] - %msg%n"
        } 
      },
      "File" : {
        "name" : "file",
        "fileName" : "${env:logdir}/app.log",
        "JsonLayout" : {
          "complete" : "true"
        }
      }
    },
    "Loggers": {
      "Root": {
        "level": "info",
        "AppenderRef": [
          {
            "ref": "Console"
          },
          {
            "ref" : "File"
          }
        ]
      }
    }
  }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM