配置文件:

 

 加载对应配置文件log4j2-test.ymlConfiguration:

  status: info

Properties:
Property:
- name: log.pattern
value: '%d{yyyy-MM-dd HH:mm:ss.SSS}{GMT+8} %X{log_trace_id} %highlight{%-5level} %location %logger{35} %msg %n{FATAL=red, ERROR=red, WARN=yellow, INFO=cyan, DEBUG=cyan,TRACE=blue}'
- name: log.path
value: /jydata/log
- name: application.name
value: eureka注册对应服务名称
Appenders:
Console: #输出到控制台
name: console
target: SYSTEM_OUT
ThresholdFilter:
level: info
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
Pattern: ${log.pattern}
disableAnsi: false

Socket:
name: logstash
host: 10.0.17.40
port: 5044
protocol: TCP
# 配置说明见: https://www.docs4dev.com/docs/zh/log4j2/2.x/all/manual-layouts.html
JsonLayout:
compact: true
eventEol: true
stacktraceAsString: true
# 官网说明: 生成location information是一项昂贵的操作,可能会影响性能。请谨慎使用。
locationInfo: true
KeyValuePair:
- key: service
value: ${application.name}
- key: trace
value: $${ctx:log_trace_id}
- key: sleuthTraceId
value: $${ctx:X-B3-TraceId}
- key: span
value: $${ctx:X-B3-SpanId}
- key: parentSpanId
value: $${ctx:X-B3-ParentSpanId}
- key: spanExport
value: $${ctx:X-Span-Export}
- key: logdate
value: ${date:yyyy-MM-dd HH:mm:ss.SSS}
# 待添加: docker部署使用
# - key: containerId
# value: ${docker:containerId}
# - key: containerName
# value: ${docker:containerName}
# - key: imageName
# value: ${docker:imageName}

# 主日志
RollingFile:
- name: rollingFile
fileName: ${log.path}/${application.name}.log
filePattern: '${log.path}/history/$${date:yyyy-MM}/${application.name}-%d{yyyy-MM-dd}.log'
PatternLayout:
Pattern: ${log.pattern}
Filters:
ThresholdFilter:
- level: error
onMatch: DENY
onMismatch: NEUTRAL
- level: info
onMatch: ACCEPT
onMismatch: DENY
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100

# 系统间交互日志
- name: transRollingFile
ignoreExceptions: false
fileName: ${log.path}/trans/${application.name}-trans.log
filePattern: "${log.path}/trans/$${date:yyyy-MM}/${application.name}-%d{yyyy-MM-dd}.log"
JsonLayout:
compact: true
eventEol: true
stacktraceAsString: true
# 官网说明: 生成location information是一项昂贵的操作,可能会影响性能。请谨慎使用。
locationInfo: true
KeyValuePair:
- key: service
value: ${application.name}
- key: trace
value: $${ctx:log_trace_id}
- key: logdate
value: ${date:yyyy-MM-dd HH:mm:ss.SSS}
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100

# feign trans 服务间交互日志
- name: feignRollingFile
ignoreExceptions: false
fileName: ${log.path}/feign/${application.name}-feign.log
filePattern: "${log.path}/feign/$${date:yyyy-MM}/${application.name}-%d{yyyy-MM-dd}.log"
JsonLayout:
compact: true
eventEol: true
stacktraceAsString: true
# 官网说明: 生成location information是一项昂贵的操作,可能会影响性能。请谨慎使用。
locationInfo: true
KeyValuePair:
- key: service
value: ${application.name}
- key: trace
value: $${ctx:log_trace_id}
- key: logdate
value: ${date:yyyy-MM-dd HH:mm:ss.SSS}
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100

# 错误日志
- name: errorRollingFile
ignoreExceptions: false
fileName: ${log.path}/error/${application.name}-error.log
filePattern: "${log.path}/error/$${date:yyyy-MM}/${application.name}-%d{yyyy-MM-dd}.log"
ThresholdFilter:
level: error
onMatch: ACCEPT
onMismatch: DENY
PatternLayout:
Pattern: ${log.pattern}
Policies:
TimeBasedTriggeringPolicy: # 按天分类
modulate: true
interval: 1
DefaultRolloverStrategy: # 文件最多100个
max: 100

Async:
- name: asyncRollingFile
includeLocation: true
AppenderRef:
ref: rollingFile

- name: asyncFeignRollingFile
includeLocation: true
AppenderRef:
ref: feignRollingFile

- name: asyncTransRollingFile
includeLocation: true
AppenderRef:
ref: transRollingFile

- name: asyncErrorRollingFile
includeLocation: true
AppenderRef:
ref: errorRollingFile

Loggers:
Root:
level: info
AppenderRef:
- ref: console
- ref: logstash
- ref: asyncRollingFile
- ref: asyncErrorRollingFile

Logger:
- name: trans
level: info
additivity: false
AppenderRef:
- ref: logstash
- ref: console
- ref: asyncTransRollingFile

- name: feign
level: info
additivity: false
AppenderRef:
- ref: logstash
- ref: console
- ref: asyncFeignRollingFile

对应pom需添加依赖:
<!-- 加上这个才能辨认到log4j2.yml文件 -->
<dependency>
<groupId>com.fasterxml.jackson.dataformat</groupId>
<artifactId>jackson-dataformat-yaml</artifactId>
</dependency>
<!-- logstash.logback -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
<exclusions>
      <!-- 需排除springboot默认使用日志 -->
        <exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-logging</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-log4j2</artifactId>
<version>2.4.1</version>
</dependency>