Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to make Log4j2 configurable by environment using Spring Boot 1.3.6.RELEASE

I would like to change some properties from the log4j2.xml file depending on the my application.properties, for example define some properties and then substitute in the log4j2 those properties that are parameters.

I ran different approaches but I still did not get the right thing. I would like to have different configs depending on the environment (DEV, QA or PROD). How to accomplish this?

I'm trying to have this in my properties

#Place holders for log4j2.xml file
log.file.path=/opt/tomcat/logs
log.file.name=dummydummy
log.file.size=100 MB
log.level=DEBUG

My log4j2 below.

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="WARN" monitorInterval="30">
    <Properties>
        <Property name="PID">????</Property>
        <property name="name">my-log</property>
    </Properties>
    <Appenders>
        <RollingFile name="file" fileName="${log.file.path}${log.file}.log"
            filePattern="${log.file.path}${log.file}-%d{yyyy-MM-dd}-%i.log.gz">
            <PatternLayout
                pattern="%d{yyyy-MM-dd HH:mm:ss.SSS} %5p ${sys:PID} --- [%t] %c{1}(%M:%L) : %m%n%wEx" />
            <Policies>
                <TimeBasedTriggeringPolicy /><!-- Rotated everyday -->
                <SizeBasedTriggeringPolicy size="${log.file.size}" /> <!-- Or every 100 MB -->
            </Policies>
        </RollingFile>
        <Console name="Console" target="SYSTEM_OUT" follow="true">
            <PatternLayout
                pattern="%clr{%d{yyyy-MM-dd HH:mm:ss.SSS}}{faint} %clr{%5p} %clr{${sys:PID}}{magenta} %clr{---}{faint} %clr{[%t]}{faint} %clr{%c{1}(%M:%L)}{cyan} %clr{:}{faint} %m%n%wEx" />
        </Console>
    </Appenders>
    <Loggers>
        <Logger name="org.hibernate.validator.internal.util.Version"
            level="warn" />
        <Logger name="org.apache.coyote.http11.Http11NioProtocol" level="warn" />
        <Logger name="org.apache.tomcat.util.net.NioSelectorPool" level="warn" />
        <Logger name="org.apache.catalina.startup.DigesterFactory" level="error" />
        <Logger name="org.springframework.web" level="error" />

        <Root level="${log.level}">
            <AppenderRef ref="Console" />
            <AppenderRef ref="file" />
        </Root>
    </Loggers>
</Configuration>
like image 407
Rogelio Blanco Avatar asked Aug 09 '16 20:08

Rogelio Blanco


2 Answers

Since log4j 2.14.0, you can now use Spring Boot environment variables without Spring Cloud and without doing direct reference to the properties file. You'll need at least Spring Boot 2.0.3

<property name="applicationName">${spring:spring.application.name}</property>

Documentation: https://logging.apache.org/log4j/2.x/log4j-spring-boot/index.html

Maven repository: https://mvnrepository.com/artifact/org.apache.logging.log4j/log4j-spring-boot

like image 120
StardustOviedo Avatar answered Sep 21 '22 18:09

StardustOviedo


As of Log4j 2.13.0 Log4j 2 now provides a Spring Lookup as part of its Spring Cloud Config support. It will allow you to reference properties defined in your application.properties or application.yml file of your Spring Boot application in the log4j2.xml.

like image 29
rgoers Avatar answered Sep 22 '22 18:09

rgoers