【发布时间】:2021-07-26 15:44:15
【问题描述】:
我是使用 xmls 配置 log4j 的新手。我想禁用控制台日志记录,即只有错误级别的日志应该出现在控制台上。其余日志应转到各自的文件。
这里是log4j2.xml
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
<Properties>
<Property name="basePath">/var/log/svc</Property>
<Property name="LOG_PATTERN">%d{yyyy-MM-dd HH:mm:ss} %-5p [%C{1}] %M{1}:%L - %m%n</Property>
<Property name="consoleLogPattern">[%-5level] %d{yyyy-MM-dd HH:mm:ss.SSS} [%t] %c{1} - %msg%n</Property>
<Property name="JSON_LOG_PATTERN">{"time": "%d{ISO8601}", "level": "%-5level", "service": "${env:SERVICE}",
"class": "%-40.40logger{39}:%L", "environment": "${sys:spring.profiles.active:-defaultEnv}", "instanceId" :
"%X{instanceId}", "requestId" : "%X{logMDCFilter.UUID}", "message": "%enc{%maxLen{%m}{204800}}{JSON}",
"traceId": "%X{X-B3-TraceId}", "spanId": "%X{X-B3-SpanId}", "exception":
"%enc{%maxLen{%ex}{204800}}{JSON}"}%n
</Property>
<Property name="EXT_API_LOG_PATTERN">%n%date{yyyy-MM-dd HH:mm:ss ZZZZ} %message%n</Property>
<Property name="DEFAULT_API_LOG_PATTERN">%n%date{yyyy-MM-dd HH:mm:ss ZZZZ}, "traceId": "%X{X-B3-TraceId}",
"spanId": "%X{X-B3-SpanId}", %message%n
</Property>
</Properties>
<CustomLevels>
<CustomLevel name="SERVICE_LOG" intLevel="530"/>
</CustomLevels>
<Appenders>
<Console name="Console" target="SYSTEM_OUT" follow="true">
<PatternLayout pattern="${consoleLogPattern}"/>
</Console>
<RollingFile name="errorLog" fileName="${basePath}/svc-application-error.log"
filePattern="${basePath}/svc-application-error-%d{yyyy-MM-dd}-%i.log.zip">
<LevelRangeFilter minLevel="ERROR" maxLevel="ERROR" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="2000KB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="infoLog" fileName="${basePath}/svc-application-info.log"
filePattern="${basePath}/svc-application-info-%d{yyyy-MM-dd}-%i.log.zip">
<LevelRangeFilter minLevel="INFO" maxLevel="INFO" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${JSON_LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="2000KB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
<RollingFile name="serviceLog" fileName="${basePath}/svc-application-service.log"
filePattern="${basePath}/svc-application-service-%d{yyyy-MM-dd}-%i.log.zip">
<LevelRangeFilter minLevel="SERVICE_LOG" maxLevel="SERVICE_LOG" onMatch="ACCEPT" onMismatch="DENY"/>
<PatternLayout pattern="${DEFAULT_API_LOG_PATTERN}"/>
<Policies>
<TimeBasedTriggeringPolicy interval="1" modulate="true"/>
<SizeBasedTriggeringPolicy size="20000KB"/>
</Policies>
<DefaultRolloverStrategy max="10"/>
</RollingFile>
</Appenders>
<Loggers>
<Logger name="com.application.service" additivity="true">
<AppenderRef ref="errorLog"/>
<AppenderRef ref="infoLog"/>
<AppenderRef ref="Console"/>
<AppenderRef ref="serviceLog"/>
</Logger>
<Root level="error">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
如果我在最后保持日志级别,那么它会将日志与控制台一起打印到所有文件中。但是,如果我将其更改为错误,则只会将错误日志打印到文件和控制台。您能帮我解决以下问题吗-
- 应将日志写入相应的文件,但只有错误日志才会出现在控制台中。
- 实现第一点后,如何将具有自定义日志级别的日志写入特定文件。
【问题讨论】:
标签: spring-boot logging microservices log4j2