简体   繁体   中英

Spring logging configuration with custom properties

I have to configure logging for a Spring Boot application, but I don't know how I could use custom properties and JVM options in logback-spring.xml.

Let's say I have the following properties:

  • application.properties: logging.app.name=app
  • JVM options: -Dlogging.app.version=1.0
  • (additionally I need active profile and hostname)

Then the filename and log entry should be the following:

  • app-[HOSTNAME].log
  • 2017/08/25 01:02:03.456 INFO app/1.0/[PROFILE] [main] Some log...

As I know I cannot simply access to these properties in my logging configuration (spring-logback.xml). That's why I tried to create these beans (ConsoleAppender, RollingFileAppender) manually in a configuration class, but it didn't work for me. I also tried implementing the afterPropertiesSet method of InitializingBean and put these variables manually for Logback, but this didn't help either and I start running out of ideas. So any help is really appreciated!

Try to run app with this configurations where you can define the path to application.properties and also provide the logback configuration file.

$java -jar /{pathToJar}/myApp.jar 
--spring.config.location=/{pathToFolderWithCustomeProperties}/application.properties 
--logging.config=/{path}/logback.xml 
-Dlogback.configurationFile=/{path}/logback.xml

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