How do I use a Spring Boot application.yml property in log4j2 configuration?

Viewed 1282

I have a Spring Boot application that uses the YAML form (i.e. application.yml) for configuration. I am also using Log4J2 (i.e. log4j2-spring.xml) for logging.

I am trying to use a property defined in application.yml as a property substitution in the log4j configuration.

Here is what I mean:

application.yml:

service:                
  name: RouteAssessor   
  id: 1     

log4j2-spring.xml:

  <Properties>
    <Property name="serviceName">${sys:service.name}</Property>
    <Property name="serviceId">${sys:service.id}</Property>

    <Property name="logFile">logs/${serviceName}-${serviceId}</Property>
    <Property name="statsFile">metrics/${serviceName}-${serviceId}_stats.json</Property>
  </Properties>

  <Appenders>                                                                             
    <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">                    
      <PatternLayout pattern="${LOG_PATTERN}" />                                          
    </Console>                                                                            

    <RollingFile name="RollingFile" fileName="${logFile}.current-session.log"             
      filePattern="${logFile}.%i.log.gz" ignoreExceptions="false">                        
      <PatternLayout>                                                                     
        <Pattern>${LOG_PATTERN}</Pattern>                                                 
      </PatternLayout>                                                                    
      <Policies>                                                                          
        <OnStartupTriggeringPolicy />                                                     
        <SizeBasedTriggeringPolicy size="10 MB" />                                        
      </Policies>                                                                         
      <DefaultRolloverStrategy max="10" />                                                
    </RollingFile>                                                                        

    <File name="StatsFile" fileName="${statsFile}" append="false">                        
      <PatternLayout>                                                                     
        <Pattern>${STATS_PATTERN}</Pattern>                                               
      </PatternLayout>                                                                    
    </File>                                                                               
  </Appenders>       

Nothing I have tried will name the log files that way I need them to be. They always end being named something like: ${sys:service.name}-${sys:service.id}.current-session.log.

  1. Can we access the application.yml properties from within log4j configuration?
  2. If yes, how is it done?
  3. If no, best practice suggestions on an alternative.

Thank you

0 Answers
Related