本文介绍了如何使用SLF4J和Log4j2记录致命(或任何自定义日志级别)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有那些具体的要求:
- 需要能够登录 FATAL 级别
- 需要使用 SLF4J
- 需要使用 Log4j2
现在,这是我的实现:
final Logger logger = LoggerFactory.getLogger(HelloWorld.class);
final Marker marker = MarkerFactory.getMarker("FATAL");
logger.error(marker, "!!! Fatal World !!!");
这是我的 PatternLayout (以Yaml为单位):
PatternLayout:
Pattern: "%d{ISO8601_BASIC} %-5level %marker [%t] %logger{3.} - %msg%n"
这是我的日志输出:
20150506T155705,158 ERROR FATAL [main] - !!! Fatal World !!!
您对如何有效地从日志输出中删除错误"有任何想法吗?
Do you have any idea about how to efficiently to remove the "ERROR" from the log output?
非常感谢您
推荐答案
这是我和一些同事一起提出的最接近的解决方案:
Here's the closest working solution I came with some colleagues :
- 使用 SLF4J 标记创建 Fatal 标记类.
- 使用 RoutingAppender ,使用标记作为路由模式:"$$ {marker:}"
- 配置一个 Fatal特定附加程序,该附加程序具有自己的PatternLayout,该PatternLayout不包含LogLevel,而是一个硬编码的FATAL级别.
- Create a Fatal Marker class using SLF4J Markers.
- Using a RoutingAppender, use the Marker as the routing pattern: "$${marker:}"
- Configure a Fatal-specific appender that has its own PatternLayout that doesn't include the LogLevel but a hardcoded FATAL level.
这是 Java 示例:
Marker fatal = MarkerFactory.getMarker("FATAL");
// Usage example
final Logger logger = LoggerFactory.getLogger(FatalLogger.class);
logger.log(fatal, "this is a fatal message");
// Log sample :
20150514T115144,279 FATAL [main] FatalLogger - this is a fatal message
这是 YAML 示例:
Configuration:
status: debug
Appenders:
RandomAccessFile:
- name: APPLICATION_APPENDER
fileName: logs/application.log
PatternLayout:
Pattern: "%d{ISO8601_BASIC} %-5level %msg%n"
- name: FATAL_APPENDER
fileName: logs/application.log
PatternLayout:
Pattern: "%d{ISO8601_BASIC} FATAL %msg%n"
Routing:
name: ROUTING_APPENDER
Routes:
pattern: "$${marker:}"
Route:
- key: FATAL
ref: FATAL_APPENDER
- ref: APPLICATION_APPENDER #DefaultRoute
Loggers:
Root:
level: trace
AppenderRef:
- ref: ROUTING_APPENDER
这篇关于如何使用SLF4J和Log4j2记录致命(或任何自定义日志级别)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!