【问题标题】:log4j2.properties cannot read variables from Docker Environment or Kubernetes Envlog4j2.properties 无法从 Docker Environment 或 Kubernetes Env 读取变量
【发布时间】:2022-01-08 22:16:35
【问题描述】:

在 Spring Boot 2.6.0 中使用 Log4J2。我想使用从外部到 log4j.propeties 的环境变量 但它总是使用本地 application.propeties 文件而不是真正的 docker 或 Kubernetes 环境变量

文件application.properties

spring.application.name=myapp

#Logger FilePath
log.file.path=logs/dev/my-app

Docker Composer 文件

   version: "3"

services:
 
  spring-app-log4j2:
    build: ./log4j2
    ports:
      - "8080:80"
    environment:
      - SERVER_PORT=80
      - LOG_FILE_PATH=logs/prod/my-app
      

文件log4j2.properties

name=config

#Read Properties values from application properties
property.filename = ${bundle:application:log.file.path}

property.layoutPattern = %d{MMM dd yyyy HH:mm:ss.SSS z} | ${hostName} | %-5p | %c{1}:%L  |  %M() - %m%n

appenders=console, rolling 

#log to console
appender.console.type=Console
appender.console.name=STDOUT
appender.console.layout.type=PatternLayout
appender.console.layout.pattern=${layoutPattern}


#log to file and daily rolling
appender.rolling.type = RollingFile
appender.rolling.name = roll
appender.rolling.fileName = ${filename}.log
appender.rolling.filePattern = ${filename}.%d{dd-MMM-yyyy}.log.gz
appender.rolling.layout.type = PatternLayout
appender.rolling.layout.pattern = ${layoutPattern}
appender.rolling.policies.type = Policies
appender.rolling.policies.time.type = TimeBasedTriggeringPolicy
appender.rolling.policies.time.interval = 2
appender.rolling.policies.time.modulate = true
appender.rolling.policies.size.type = SizeBasedTriggeringPolicy
appender.rolling.policies.size.size=100MB
appender.rolling.strategy.type = DefaultRolloverStrategy
appender.rolling.strategy.max = 5

loggers = rolling

logger.rolling.name = org.apache.logging.log4j.core.appender.rolling
logger.rolling.level = info
logger.rolling.additivity = true
logger.rolling.appenderRefs = rolling
logger.rolling.appenderRef.rolling.ref = roll


rootLogger.level=info
rootLogger.appenderRefs=stdout,rolling
rootLogger.appenderRef.stdout.ref=STDOUT
rootLogger.appenderRef.rolling.ref = roll

输出尝试

 property.filename = ${bundle:application:logpath}     #Output - logs/dev/app
    #(Taking values from application Properties only, not taking from Docker pod environment varibales)
    property.filename = ${sys:logpath}     #Output - ${sys:logpath}
    property.filename = ${env:logpath}   # Not Working   #Output - ${env:logpath}
    #use filename variable 
    appender.rolling.fileName = ${filename}.log

在 Java 代码中打印时

@Value("${logpath}") String logpath;  #Output -logs/prod/app
# Working with Docker env

问题:当 log4j 启​​动时,它没有从 environment 中找到文件路径,所以默认从 application.propeties 读取

如何从 Docker 环境或 Kubernetes 环境中读取数据

我需要的日志文件应该是“logs/prod/my-app”而不是“logs/dev/my-app”

源代码:https://github.com/jeebendu/log4j2

【问题讨论】:

  • 我想在本地复制此问题,但缺少一些信息。您能否分享您在 Kubernets 中应用的应用程序、Docker 文件和 yaml 文件的示例代码应用程序,以便您的issue will be reproducible?您使用的是哪个 Kubernetes 版本?
  • @MikolajS。我已经在这里更新了我的最小代码,你能试试一次吗提前谢谢

标签: spring-boot docker kubernetes log4j2


【解决方案1】:

在所有尝试中,您都为 filename 属性使用单一来源。您需要使用变量替换的 fallback 功能之一:

  • 您可以使用语法${variable:-default} 为变量替换添加默认值:

    property.filename = ${env:LOG_FILE_PATH:-${bundle:application:log.file.path}}
    appender.rolling.fileName = ${filename}.log
    
  • 或者您可以利用每个${prefix:variable} 回退到${variable} 的事实:

    property.LOG_FILE_PATH = ${bundle:application:log.file.path}
    appender.rolling.fileName = ${env:LOG_FILE_PATH}
    

【讨论】:

  • 现在可以使用了,非常感谢
  • 如果它回答了您的问题,您可能会考虑接受答案(答案旁边的大勾号)。
  • 我接受了
猜你喜欢
  • 1970-01-01
  • 2019-02-12
  • 1970-01-01
  • 2021-04-09
  • 2021-08-12
  • 2020-07-01
  • 2019-01-29
  • 1970-01-01
  • 2021-09-21
相关资源
最近更新 更多