Log4j Properties in a Custom Place

后端 未结 3 980
梦如初夏
梦如初夏 2020-12-15 08:50

I\'m using Apache Commons Logging and SLF4J with log4j, but I also want to use the log4j.properties in a custom place like conf/log4.properties. Here is the problem:

相关标签:
3条回答
  • 2020-12-15 09:22

    You can specify config file location with VM argument

    -Dlog4j.configuration="file:/C:/workspace3/local/log4j.properties"

    0 讨论(0)
  • 2020-12-15 09:28

    As you state, by invoking PropertiesConfigurator, you are tying your application to log4j. However, the extend of this tie is quite limited. You could very easily remove the line invoking PropertiesConfigurator and recompile your code. After that is done, assuming you are using the SLF4J API for logging, you could repalce log4j with another logging framework, say logback-classic or j.u.l. just by replacing jar files. Thus, SLF4J still serves its purpose to a large extend. I would not throw the baby out with the bath water.

    0 讨论(0)
  • 2020-12-15 09:33

    I think the easiest thing to do is specify the location of the file using the log4j.configuration system property. Adopting the example in the Log4J manual:

    java -Dlog4j.configuration=conf/log4j.properties -classpath ...
    

    I believe that Log4J will find a file named "log4j.properties" anywhere on the classpath, but may be hallucinating. Worth trying, however.

    0 讨论(0)
提交回复
热议问题