2

I would like to read environment variable or property in every log line from logback.xml. Example if I hit echo "${FOO_INFO}" the result is "FOO_RESULT", this FOO_RESULT I need to print it in the every log line.

After adding JAVA_OPTS="-Dfoo.info=${FOO_INFO}"

I tried below option:-

Option : added and calling %d{ISO8601} [%thread] %-5level %logger{36} ${FOO_INFO}- %msg%n

SSSR
  • 21
  • 1
  • 4

2 Answers2

0

The documentation indicates you can include a system property by using a PatternLayout with the property conversion pattern %property{key} where key is the system property name.

dnault
  • 8,340
  • 1
  • 34
  • 53
0

You can use system properties like ${SOME_VAR} and pass that value into your application with -D while running.

So in my case:

    <logger name="com.example.app" level="${SOME_LOGGING_LEVEL}"/>

And:

java -DSOME_LOGGING_LEVEL=WARN MyApp

Verified and it works. And ofc it works in docker, docker-compose and Kubernetes as this is Logback feature. See here

WesternGun
  • 11,303
  • 6
  • 88
  • 157