dee*_*ejo 5 java logback spring-boot
我使用 logback 和 slf4j 来登录 Spring Boot 应用程序。我创建了一个自定义布局类,因为所有日志语句都将包装为 json。我已配置 logback-spring.xml 如下所示以采用自定义布局。有用!
问题是我无法应用该模式。只有布局(或)模式起作用。我想要的是始终记录在日志中,转到布局类,然后在记录之前应用模式。
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${user.home}/logs/sample.log</file>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<fileNamePattern>${user.home}/logs/sample_%d{yyyy-MM-dd}.%i.log</fileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<maxFileSize>10MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
<!-- how many days to keep the files -->
<maxHistory>30</maxHistory>
</rollingPolicy>
<encoder class="ch.qos.logback.core.encoder.LayoutWrappingEncoder">
<layout class="com.test.test.payment.core.logging.SampleLogLayout" >
</layout>
</encoder>
*<!-- <encoder>
<charset>UTF-8</charset>
<Pattern>{"@timestamp": "%d{yyyy-MM-dd HH:mm:ss.SSS}", "priority": "%p", "application": "payment",
"class": "%C", "file": "%F:%L", "payload": %m }%n
</Pattern>
</encoder>-->*
</appender>
Run Code Online (Sandbox Code Playgroud)
这是SampleLogLayout课程:
public class SampleLogLayout extends LayoutBase<LoggingEvent> {
@Override
public String doLayout(LoggingEvent event) {
String renderedMessage = event.getMessage();
if (!isJson(renderedMessage)) {
Throwable throwable = null;
if (event.getLevel().equals(Level.ERROR) || event.getLevel().equals(Level.WARN)) {
final IThrowableProxy throwableProxy = event.getThrowableProxy();
if ((throwableProxy != null) && (throwableProxy instanceof ThrowableProxy)) {
ThrowableProxy proxy = (ThrowableProxy) throwableProxy;
throwable = proxy.getThrowable();
}
String message = LogErrorMessage.create(CommonCoreErrors.GENERIC_ERROR)
.message(renderedMessage).exception(throwable).build();
return message;
} else {
return LogMessage.create(renderedMessage).build();
}
}
return renderedMessage;
}
private boolean isJson(String msg) {
if (null == msg) {
return false;
} else {
return msg.startsWith("{") && msg.endsWith("}");
}
}
}
Run Code Online (Sandbox Code Playgroud)
我开始重现这个问题是为了提供一个完整的解决方案,但由于缺乏,LogMessage这LogErrorMessage使得这有点棘手。
但是,在我看来,您只想以 JSON 格式登录,不仅是消息,还包括元数据timestamp,例如priority等。
这可以通过使用 来实现JsonLayout。这是布局配置的示例:
<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>
<includeContextName>false</includeContextName>
</layout>
Run Code Online (Sandbox Code Playgroud)
使用该配置,以下日志调用...
logger.info("{\"a\": 1, \"b\": 2}");
Run Code Online (Sandbox Code Playgroud)
...将发出:
{"timestamp":"2017-10-05 10:51:34.610","level":"INFO","thread":"main","logger":"com.stackoverflow.logback.LogbackTest","message":"{\"a\": 1, \"b\": 2}"}
Run Code Online (Sandbox Code Playgroud)
您也可以包含 MDC,例如...
MDC.put("application", "payment");
logger.info("{\"a\": 1, \"b\": 2}");
Run Code Online (Sandbox Code Playgroud)
...将发出:
{"timestamp":"2017-10-05 10:52:56.088","level":"INFO","thread":"main","mdc":{"application":"payment"},"logger":"com.stackoverflow.logback.LogbackTest","message":"{\"a\": 1, \"b\": 2}"}
Run Code Online (Sandbox Code Playgroud)
这非常接近您想要的输出并且JsonLayout是可扩展的,因此您可以......
toJsonMap()以更改键的名称,例如替换timestamp为@timestamp、替换message为payloadaddCustomDataToJsonMap()将其他键:值对添加到日志事件中因此,我认为您可以使用预先存在的JsonLayout而不是编写自己的输出来实现您想要的输出。
有关 Logback JSON 扩展的更多详细信息,请参见此处。
Maven 坐标为:
<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-json-core</artifactId>
<version>0.1.5</version>
</dependency>
<dependency>
<groupId>ch.qos.logback.contrib</groupId>
<artifactId>logback-jackson</artifactId>
<version>0.1.5</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11351 次 |
| 最近记录: |