WildFly10记录为JSON

时间:2019-05-14 10:25:47

标签: json wildfly logback slf4j wildfly-10

我创建了一个类来记录JEE7 REST API接收到的请求(和响应)。

该类使用SLF4J和logback构造JSON格式的日志事件,例如

{
   "timestamp" : "2019-05-14 11:46:40.419",
   "level" : "INFO",
   "thread" : "default task-5",
   "mdc" : {
     "request.headers.Connection" : "keep-alive",
     "request.headers.Accept" : "*/*",
     "request.requestUri" : "http://localhost:8080/play/v1.0/map",
     "request.headers.User-Agent" : "PostmanRuntime/7.11.0",
     "request.headers.Cache-Control" : "no-cache",
     "request.method" : "GET",
     "request.headers.accept-encoding" : "gzip, deflate",
     "request.headers.Host" : "localhost:8080",
     "request.headers.Postman-Token" : "e03c8d8b-ebea-475c-bfaf-0ff8d4646081",
     "response.status" : "200",
     "message" : "200 GET /play/v1.0/map"
   },
   "logger" : "javax.ws.rs.container.ContainerRequestFilter",
   "message" : "you",
   "context" : "default"
 }

问题在于WildFly在日志中添加了自己的信息,因此当前结果如下所示:

11:46:40,420 INFO  [stdout] (default task-5) {
11:46:40,420 INFO  [stdout] (default task-5)   "timestamp" : "2019-05-14 11:46:40.419",
11:46:40,420 INFO  [stdout] (default task-5)   "level" : "INFO",
11:46:40,420 INFO  [stdout] (default task-5)   "thread" : "default task-5",
11:46:40,420 INFO  [stdout] (default task-5)   "mdc" : {
11:46:40,420 INFO  [stdout] (default task-5)     "request.headers.Connection" : "keep-alive",
11:46:40,421 INFO  [stdout] (default task-5)     "request.headers.Accept" : "*/*",
11:46:40,421 INFO  [stdout] (default task-5)     "request.requestUri" : "http://localhost:8080/play/v1.0/map",
11:46:40,421 INFO  [stdout] (default task-5)     "request.headers.User-Agent" : "PostmanRuntime/7.11.0",
11:46:40,421 INFO  [stdout] (default task-5)     "request.headers.Cache-Control" : "no-cache",
11:46:40,421 INFO  [stdout] (default task-5)     "request.method" : "GET",
11:46:40,421 INFO  [stdout] (default task-5)     "request.headers.accept-encoding" : "gzip, deflate",
11:46:40,421 INFO  [stdout] (default task-5)     "request.headers.Host" : "localhost:8080",
11:46:40,421 INFO  [stdout] (default task-5)     "request.headers.Postman-Token" : "e03c8d8b-ebea-475c-bfaf-0ff8d4646081",
11:46:40,421 INFO  [stdout] (default task-5)     "response.status" : "200",
11:46:40,421 INFO  [stdout] (default task-5)     "message" : "200 GET /play/v1.0/map"
11:46:40,421 INFO  [stdout] (default task-5)   },
11:46:40,421 INFO  [stdout] (default task-5)   "logger" : "javax.ws.rs.container.ContainerRequestFilter",
11:46:40,421 INFO  [stdout] (default task-5)   "message" : "you",
11:46:40,421 INFO  [stdout] (default task-5)   "context" : "default"
11:46:40,422 INFO  [stdout] (default task-5) }

我希望我的日志文件仅包含JSON对象,每行一个(为了清晰起见,上面的示例已漂亮地打印出来了)

WildFly的日志记录配置文档尚不清楚如何实现此目的。

有人可以帮我吗?

编辑1

logback.xml

<?xml version="1.0" encoding="UTF-8"?>

<configuration scan="true">
    <contextName>/psp/v1.0</contextName>

    <!-- ADD jboss-deployment-structure.xml -->
    <!-- WildFly adds slf4j as a default logging dependency. To avoid this exclude the main implementation in jboss-deployment-structure.xml descriptor -->

    <!-- don't log logback startup info -->
    <statusListener class="ch.qos.logback.core.status.NopStatusListener"/>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.contrib.json.classic.JsonLayout">
            <jsonFormatter
                    class="ch.qos.logback.contrib.jackson.JacksonJsonFormatter">
                <prettyPrint>false</prettyPrint>
            </jsonFormatter>
            <timestampFormat>yyyy-MM-dd' 'HH:mm:ss.SSS</timestampFormat>
            <appendLineSeparator>true</appendLineSeparator>
        </layout>
    </appender>

    <root level="info">
        <appender-ref ref="STDOUT"/>
    </root>
</configuration>

1 个答案:

答案 0 :(得分:0)

如果您将logback用作日志记录后端,则可以使用json附加程序对其进行配置。

您需要将以下依赖项添加到pom.xml:

df['DateTime']=pd.to_datetime(df['DialogDateTime'],format='%d-%b-%y %H.%M.%S.%f %p').dt.strftime(date_format='%H.%M.%S.%f')

然后,您可以按如下所示将附加程序添加到logback(logback.xml):

<dependencies>
    <dependency>
        <groupId>ch.qos.logback</groupId>
        <artifactId>logback-classic</artifactId>
        <version>1.1.7</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>