简体   繁体   中英

How to make Log4j environment specific

I am trying to edit my logging configuration file so that the levels for the logs would be different based on either it is in a development environment or production environment for example

in dev would be

<logger name="1" level="debug"/>
<logger name="2" level="debug"/>
<logger name="3" level="debug"/>

in production the loggers should be

<logger name="1" level="warn"/>
<logger name="2" level="warn"/>
<logger name="3" level="warn"/>

is there a way to do this with an environment variable and condition statement? I want to try to avoid having a log file specific to each environment

You just need to use two configuration files for logback, one that would log at least debug level and the other that would log at least warning . Quote from here :

  1. Logback tries to find a file called logback.groovy in the classpath.

  2. If no such file is found, logback tries to find a file called logback-test.xml in the classpath.

  3. If no such file is found, it checks for the file logback.xml in the classpath..

There is actually 4 and 5, but this is enough for the explanation. So basically use logback-test.xml for dev, and logback.xml for prod. Of course, don't deploy logback-test.xml to prod.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM