简体   繁体   English

在 XML 配置文件中为 Log4j2 日志级别设置自定义名称

[英]Set custom name for Log4j2 log level in XML configuration file

I'd like to force my log4j2 to display log level like "Warning" instead of "WARN" etc.我想强制我的 log4j2 显示日志级别,如“警告”而不是“警告”等。

Is it possible to do so?有可能这样做吗? It can't be done programmatically, it must be done in XML configuration file because I'm using it in an already built image so I don't have access to the source code.它不能以编程方式完成,它必须在 XML 配置文件中完成,因为我在已经构建的图像中使用它,所以我无法访问源代码。

I tried something like this but it did nothing.我试过这样的东西,但它什么也没做。

<CustomLevels>
    <CustomLevel name="Warning" intLevel="300" />
    <CustomLevel name="Information" intLevel="400" />
</CustomLevels>

I'd like to replace default log levels with my custom names.我想用我的自定义名称替换默认日志级别。

Edit :编辑

I need something like this, but for JsonTemplateLayout:我需要这样的东西,但对于 JsonTemplateLayout:

<PatternLayout>
   <Pattern>"%level{WARN=Warning, DEBUG=Debug, ERROR=Error, TRACE=Trace, INFO=Info}"</Pattern>
</PatternLayout>

It must be in JSON format using JsonTemplateLayout (no PatternLayout)它必须是 JSON 格式,使用 JsonTemplateLayout(无 PatternLayout)

I found the solution: :)我找到了解决方案::)

<JsonTemplateLayout eventTemplateUri="classpath:EcsLayout.json">
             <EventTemplateAdditionalField
                key="@l"
                format="JSON"
                value='{"$resolver": "pattern", "pattern": "%level{WARN=Warning, DEBUG=Debug, ERROR=Error, TRACE=Trace, INFO=Information}"}'/>
</JsonTemplateLayout>

It's not very elegant, but it works:)它不是很优雅,但它有效:)

Edit:编辑:

I'm not sure why, but for ERROR level it didn't work correctly.我不确定为什么,但对于 ERROR 级别,它无法正常工作。 It changed ERROR to Error, but my @l contained also exception message.它将错误更改为错误,但我的@l 还包含异常消息。 I needed to add %ex{none} to my pattern like this:我需要像这样将%ex{none}添加到我的模式中:

{"$resolver": "pattern", "pattern": "%level{FATAL=Fatal, ERROR=Error}%ex{none}"}

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

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