我尝试使用 Logback 和以下配置在 SpringBoot 应用程序中打印日志,但日志未打印。 正在使用 Spring-boot 版本 2.7.2 和 Java 版本 17。
在pom.xml中
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>6.6</version>
</dependency>
在 logback-spring.xml 中,我给出了从 application.yml 文件引用的属性。
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<springProperty scope="context" name="APP_NAME" source="spring.application.name"/>
<springProperty scope="context" name="LOG_ROOT_PATH" source="log.root.path"/>
<property name="HOME_LOG" value="${LOG_ROOT_PATH}/logs/${APP_NAME}/${APP_NAME}.json"/>
<appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>
%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n
</Pattern>
</layout>
</appender>
<appender name="FILE-ROLLING" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>${HOME_LOG}</file>
<rollingPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedRollingPolicy">
<fileNamePattern>${LOG_ROOT_PATH}/logs/${APP_NAME}/archived/${APP_NAME}.%d{yyyy-MM-dd}.%i.json</fileNamePattern>
<!-- each archived file, size max 10MB -->
<maxFileSize>10MB</maxFileSize>
<totalSizeCap>2GB</totalSizeCap>
<maxHistory>60</maxHistory>
</rollingPolicy>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<providers>
<pattern>
<pattern>
{
"timestamp": "%d{ISO8601}",
"trace": "%X{traceId:-}",
"span": "%X{spanId:-}",
"level": "%level",
"logger": "%logger",
"thread": "%thread",
"application": "${APP_NAME}",
"message": "%msg"
}
</pattern>
</pattern>
</providers>
</encoder>
</appender>
<logger name="com.til" level="debug" additivity="false">
<appender-ref ref="FILE-ROLLING"/>
<appender-ref ref="CONSOLE" />
</logger>
<root level="info">
<appender-ref ref="FILE-ROLLING"/>
<appender-ref ref="CONSOLE" />
</root>
</configuration>
请帮忙。 谢谢
我对 LogstashEncoder 也有同样的问题。经过大量挖掘后,我配置了它对我有用的 LoggingEventCompositeJsonEncoder 自述文件
logstash-logback-encoder
将帮助您配置编码器。
我的最终结果是:
<appender name="json" class="ch.qos.logback.core.ConsoleAppender">
<encoder class="net.logstash.logback.encoder.LoggingEventCompositeJsonEncoder">
<providers>
<timestamp>
<fieldName>@timestamp</fieldName>
<timeZone>UTC</timeZone>
</timestamp>
<version>
<fieldName>@version</fieldName>
</version>
<logLevel>
<fieldName>level</fieldName>
</logLevel>
<loggerName>
<fieldName>logger_name</fieldName>
</loggerName>
<callerData>
<classFieldName>class</classFieldName>
<methodFieldName>method</methodFieldName>
<lineFieldName>line</lineFieldName>
<fileFieldName>file</fileFieldName>
</callerData>
<threadName>
<fieldName>thread_name</fieldName>
</threadName>
<message>
<fieldName>message</fieldName>
</message>
<pattern> <!-- Custom Fields -->
<pattern>{
"hostname": "${HOSTNAME}",
"application": "${APP_NAME}",
"root_path": "${LOG_ROOT_PATH}"
}</pattern>
</pattern>
<mdc />
<arguments>
<includeNonStructuredArguments>false</includeNonStructuredArguments>
</arguments>
<stackTrace>
<fieldName>stack</fieldName>
</stackTrace>
</providers>
</encoder>
</appender>
另外,如果您想创建嵌套路径,请检查此 stackOverflow 答案:How do Iwrapper of some of the messages of Logstash Logback Encoder into an inside field?
希望这有帮助!