这是我将日志写入文件的附件(logback.xml的一部分):
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/ps_gate_server.log</file>
<encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
<Pattern>%d{dd-MM-yyyy} %d{HH:mm:ss.SSS} [%-19.19t] [%-5p] [%-1.40X{OperationName};%-1.10X{ClientID};%-1.20X{PayID}] %m%n </Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/ps_gate_server.%d{yyyy-MM-dd}.%i.log.zip</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
如图所示,我的编码器是ch.qos.logback.classic.encoder.PatternLayoutEncoder
,一切正常。我需要将我的日志记录转换为JSON格式。为此,我将编码器更改为net.logstash.logback.encoder.LogstashEncoder
:
<appender name="FILE" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/ps_gate_server.log</file>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<Pattern>%d{dd-MM-yyyy} %d{HH:mm:ss.SSS} [%-19.19t] [%-5p] [%-1.40X{OperationName};%-1.10X{ClientID};%-1.20X{PayID}] %m%n </Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/ps_gate_server.%d{yyyy-MM-dd}.%i.log.zip</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
然后,当程序启动时,程序不会创建文件ps_gate_server.log。如果有人知道,请提示。所有必要的罐子都在那里。
答案 0 :(得分:0)
确保您拥有所有必要的logback依赖项,最小设置为:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.mycompamy</groupId>
<artifactId>logback-test</artifactId>
<version>1.0-SNAPSHOT</version>
<dependencies>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-core</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
<version>1.1.7</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>net.logstash.logback</groupId>
<artifactId>logstash-logback-encoder</artifactId>
<version>4.7</version>
</dependency>
</dependencies>
</project>
还要确保您已正确配置logback.xml,下一个示例效果很好:
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<appender name="sout" class="ch.qos.logback.core.ConsoleAppender">
<layout class="ch.qos.logback.classic.PatternLayout">
<Pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</Pattern>
</layout>
</appender>
<appender name="file" class="ch.qos.logback.core.rolling.RollingFileAppender">
<file>logs/ps_gate_server.log</file>
<encoder class="net.logstash.logback.encoder.LogstashEncoder">
<Pattern>%d{dd-MM-yyyy} %d{HH:mm:ss.SSS} [%-19.19t] [%-5p] [%-1.40X{OperationName};%-1.10X{ClientID};%-1.20X{PayID}] %m%n </Pattern>
</encoder>
<rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
<FileNamePattern>logs/ps_gate_server.%d{yyyy-MM-dd}.%i.log.zip</FileNamePattern>
<timeBasedFileNamingAndTriggeringPolicy class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
<!-- or whenever the file size reaches 100MB -->
<maxFileSize>100MB</maxFileSize>
</timeBasedFileNamingAndTriggeringPolicy>
</rollingPolicy>
</appender>
<root level="info">
<appender-ref ref="file" />
<appender-ref ref="sout" />
</root>
</configuration>