Rasmus Sjørslev
Rasmus Sjørslev

Reputation: 137

Logback with Spring Boot - programatically change configuration at runtime to add Syslog Appender

I am building an application (Spring Boot 1.4.2) where i would like to offer an administrator the option to enable syslog but i want to avoid him/her having to manually edit any config files - in this case logstash-spring.xml. Therefore i am trying to understand how i can achieve using a logback-spring.xml file as a baseline (e.g. define file based log options, levels etc. - settings i dont want the administrator to change) and on top of that provide a functionality at runtime where an administrator can add or change a syslog appender.

I have listed what i see as requirements:

  1. The changes made to the Logger should be persisted after the Spring Boot application is restarted.
  2. Ideally the syslog server info (name, port) are kept in my persistence layer (H2, hibernate) but i am not sure if that is possible as i guess the logging framework is being injected prior to my persistence layer?
  3. The syslog appender that i want to add should be referenced by root logger so that all the packages i have configured logging for would go to syslog (not sure if this is just "how it works per default")

Also i dont know if i could simply treat logback-spring.xml as a regular XML object and use for example JAXB to manipulate that file and use the autoscan feature of Logback to simply read in the new changes?

I have played around with defining a Logger @Bean:

@Bean
public Logger logger() {
    LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
    // excluded implementation
}

This is being picked up by Spring Boot but that brings me to the 2 items i have listed above that i dont know how or where i would store the syslog server information the administrator would provide.

UPDATE:

I wrote the following which meets the 3 requirements above however i would appreciate any feedback on the actual implementation as i am very new to Spring and Java.

GitHub repository with implementation - spring-boot-logback-syslog

Upvotes: 2

Views: 3613

Answers (1)

Rasmus Sjørslev
Rasmus Sjørslev

Reputation: 137

I managed to use the example i posted in my Github repo in my designated application and with that i am answering my own questions based on the implementation in that repo. Please refer to the README for full details on how it was implemented.

UPDATE: As part of Spring Boot 1.5.1 there is an actuator that can set the logging level during runtime: Production Ready Loggers
Not necessarily 100% related to this topic but this was one of the requirements i had for the implementation as well as changing syslog related settings.

Upvotes: 1

Related Questions