在Spring Boot Java中将日志输出作为JSON字符串输出日志— Log4j2

假设: (Assumptions:)

You already have spring boot project. If you don’t, take the tutorial — https://spring.io/guides/gs/spring-boot/

您已经有spring boot项目。 如果您不这样做,请参加本教程— https://spring.io/guides/gs/spring-boot/

If you have spring boot project, your pom.xml would be something like the following:

如果您有spring boot项目,那么pom.xml将类似于以下内容:

<?xml version="1.0" encoding="UTF-8"?><project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"
>
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/>
</parent>
<groupId>com.vb.math</groupId>
<artifactId>learnit</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>learnit</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>

Spring boot comes default packaged with logging. Refer here — https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/boot-features-logging.html

Spring Boot默认附带日志记录。 请参阅此处-https://docs.spring.io/spring-boot/docs/2.1.x/reference/html/boot-features-logging.html

The format of the default log is:

默认日志的格式为:

INFO 45469 --- [           main] org.apache.catalina.core.StandardEngine  : Starting Servlet Engine: Apache Tomcat/7.0.52
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-03-05 10:57:51.253 INFO 45469 --- [ost-startStop-1] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1358 ms
2019-03-05 10:57:51.698 INFO 45469 --- [ost-startStop-1] o.s.b.c.e.ServletRegistrationBean : Mapping servlet: 'dispatcherServlet' to [/]
2019-03-05 10:57:51.702 INFO 45469 --- [ost-startStop-1] o.s.b.c.embedded.FilterRegistrationBean

You want JSON log out put.

您想要JSON注销。

We will achieve that using log4j2.

我们将使用log4j2来实现。

第1步: (Step 1:)

Include the log4j2 spring boot dependency and exclude the any version conflicts of including common libs at multiple places.

包括log4j2 spring boot依赖关系,并排除在多个位置包括公共库的任何版本冲突。

<!-- New Dependencies and Exclusions --><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.3</version>
<exclusions>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
</exclusion>
<exclusion>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.3</version>
</dependency><!-- Exclusions in case you need --><dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>


<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>

第2步: (Step 2:)

If you have been using any other logs please check and remove those also using exclusions as above. Check dependencies using the following and remove them:

如果您一直在使用其他任何日志,请使用上面的排除项检查并删除那些日志。 使用以下命令检查依赖性并将其删除:

mvn dependency:tree -Dincludes=*log*

第三步: (Step 3:)

Create a file with name log4j2-spring.xml with the following content and add to your resources folder.

创建一个名称为log4j2-spring.xml的文件,内容如下,并将其添加到资源文件夹中。

<?xml version="1.0" encoding="UTF-8"?>
<Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<JsonLayout complete="false" compact="true" eventEol="true"></JsonLayout>
<PatternLayout
pattern="%style{%d{ISO8601}}{black} %highlight{%-5level }[%style{%t}{bright,blue}] %style{%C{1.}}{bright,yellow}: %msg%n%throwable" />
</Console>
</Appenders>
<Loggers>
<!-- LOG everything at INFO level --> <Root level="info">
<AppenderRef ref="Console" />
</Root>
<Logger name="com.vb" level="trace"></Logger>
</Loggers>
</Configuration>

You have to replace the italics. These are the pattern of the log and your packages where you want your logger to be available.

您必须替换斜体。 这些是您希望记录器可用的日志和软件包的模式。

第4步: (Step 4:)

Use your logger.

使用您的记录器。

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
// Instantiate your logger
private static Logger logger - LoggerFactory.getLogger(MyClassName.class);//Print the logslogger.debug(" Debug Message");
logger.error(" Error Message");

The output would be something like the following:

输出将类似于以下内容:

{
"thread": "main",
"level": "INFO",
"loggerName": "com.vb.math.learnit.LearnitApplication",
"message": "Started LearnitApplication in 2.508 seconds (JVM running for 4.586)",
"endOfBatch": false,
"loggerFqcn": "org.apache.commons.logging.LogAdapter$Log4jLog",
"instant": {
"epochSecond": 1597070439,
"nanoOfSecond": 853000000
},
"threadId": 1,
"threadPriority": 5
}

翻译自: https://medium.com/@vinod.bhatt/print-logs-output-as-json-string-in-spring-boot-java-log4j2-7577c8e9229b

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值