使用 Json 布局模板但在日志中缺少参数 [log4j2]

Used Json Layout template but getting parameters missing in logs [log4j2]

我在我的项目中使用了 Json 布局模板,但我得到的日志缺少几个参数。我试图在模式本身中提及那些缺失的参数,但我想这不是在我的日志中获取这些参数的正确方法。有什么建议吗?

rootLogger.level = info
rootLogger.appenderRef.stdout.ref = STDOUT


logger.rolling.name = SD
logger.rolling.level = debug
logger.rolling.additivity = false
logger.rolling.appenderRef.rolling.ref = RALLog

status = error
dest = err

filter.threshold.type = ThresholdFilter
filter.threshold.level = trace

appender.console.type = Console
appender.console.name = STDOUT
appender.console.layout.type = PatternLayout
appender.console.layout.pattern =%C{0}.%M:(%L) - %m%n
appender.console.filter.threshold.type = ThresholdFilter
appender.console.filter.threshold.level = error

#File Appender for JSON log file.
appender.rolling.type = RollingFile
appender.rolling.name = RALLog
appender.rolling.fileName = SD/sac.log
appender.rolling.filePattern = SD/sac/sac-{yyyyMMdd}.log.gz
appender.rolling.layout.type = JsonTemplateLayout
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy

备注:改为JsonLayout is considered deprecated. You should use JsonTemplateLayout。后者相当于 JSON-based 布局的 PatternLayout

如果您指的是丢失的位置信息(相当于 %C%M%L 模式,请参见 documentation),它是一个昂贵的操作,默认情况下不生成,因为它会大大降低日志记录性能。

要启用它,请使用:

appender.rolling.layout.type = JsonLayout
appender.rolling.layout.locationInfo = true

编辑:如果切换到JsonTemplateLayout,需要将locationInfoEnabled设置为true:

appender.rolling.layout.type = JsonTemplateLayout
appender.rolling.layout.locationInfoEnabled = true

此外 default template 不包含任何位置信息,因此您需要使用 EventTemplateAdditionalFields 添加它:

appender.rolling.layout.0.type = EventTemplateAdditionalField
appender.rolling.layout.0.key = log.origin.file.name
appender.rolling.layout.0.value = {"$resolver": "source", "field": "fileName"}
appender.rolling.layout.0.format = JSON
appender.rolling.layout.1.type = EventTemplateAdditionalField
appender.rolling.layout.1.key = log.origin.function
appender.rolling.layout.1.value = {"$resolver": "source", "field": "methodName"}
appender.rolling.layout.1.format = JSON
appender.rolling.layout.2.type = EventTemplateAdditionalField
appender.rolling.layout.2.key = log.origin.file.line
appender.rolling.layout.2.value = {"$resolver": "source", "field": "lineNumber"}
appender.rolling.layout.2.format = JSON