Search code examples
spring-bootlogbacksyslog

Spring Boot and Logback logging.config file with spring properties placeholders


I've a set of applications installed on a server that I want to start sending logs thru syslog to a remote Logstash server. For that, I've created a single external Logback configuration file containing a SyslogAppender pointing to the desired remote server.

As multiple applications will log to the same server, I've changed the log pattern to be the following:

<suffixPattern>%d{yyyy-MM-dd HH:mm:ss.SSS} [${server_instance}] [${application_name}] %p ${PID:- } --- [%15.15t] %logger : %m%n</suffixPattern>

Where server_instance and application_name are command-line option arguments provided at startup.

Now I just simply set the logging.config property of all my apps pointing to the same Logback config file, and all the applications start sending the logs using the specified pattern to the desired server. That part works like a charm.

But the only problem I've is that Logback isn't able to determine the server_instance and the application_name properties and they appear as [server_instance_IS_NOT_DEFINED] and [application_name_IS_NOT_DEFINED] respectively.

Can I achieve this somehow using a single external configuration file?


Solution

  • Logback isn't able to see command-line arguments; those are seen by Spring alone.

    Moved those command-line options arguments to system properties (i.e. "-Dserver_instance" and "-Dapplication_name" instead of "--server_instance" and "--application_name") and now everything's working as expected.