是否存在使用消息参数作为属性创建JSON对象的Logback布局? [英] Is there a Logback Layout that Creates JSON Objects with Message Parameters as Attributes?

查看:256
本文介绍了是否存在使用消息参数作为属性创建JSON对象的Logback布局?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将日志事件作为带有参数化字符串消息的JSON对象发送到Loggly。我们的项目目前有很多代码如下:

I want to send log events to Loggly as JSON objects with parameterized string messages. Our project currently has a lot of code that looks like this:

String someParameter = "1234";
logger.log("This is a log message with a parameter {}", someParameter);

我们目前正在使用Logback作为我们的SLF4J后端,而Logback的 JsonLayout 将我们的ILogEvent对象序列化为JSON。因此,当我们将日志事件发送到Loggly时,它们看起来像这样:

We're currently using Logback as our SLF4J backend, and Logback's JsonLayout to serialize our ILogEvent objects into JSON. Consequentially, by they time our log events are shipped to Loggly, they look like this:

{
    "message": "This is a log message with a parameter 1234",
    "level": INFO,
    ....
}

虽然这确实有效,但它会为 someParameter的每个值发送不同的消息字符串,这使得Loggly的自动过滤器无法使用。

While this does work, it sends a different message string for every value of someParameter, which renders Loggly's automatic filters next to useless.

相反,我想要一个创建如下所示的JSON的布局:

Instead, I'd like to have a Layout that creates JSON that looks like this:

{
    "message": "This is a log message with a parameter {}",
    "level": INFO,
    "parameters": [
        "1234"
    ]
}

此格式允许Loggly将所有日志事件与消息组合在一起这是一条带参数的日志消息,无论值 someParameter

This format would allow Loggly to group all log events with the message This is a log message with a parameter together, regardless of the value of someParameter.

它看起来像 Logstash的KV过滤器做了这样的事情 - 有没有办法用Logback完成这个任务,没有编写我自己的执行自定义的布局ILogEvent对象的序列化?

It looks like Logstash's KV filter does something like this - is there any way to accomplish this task with Logback, short of writing my own layout that performs custom serialization of the ILogEvent object?

推荐答案

你可以使用映射的诊断上下文为每种类型的日志消息设置一个标记,然后您可以在loggly中过滤一次。

You could use a Mapped Diagnostic Context to set a stamp for each of those type of log messages that you could then filter on once in loggly.

根据 JsonLayout 标记作为单独的值存储在JSON中。

According to the source of JsonLayout the stamp is stored as a separate value in the JSON.

这篇关于是否存在使用消息参数作为属性创建JSON对象的Logback布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆