Home > Mobile >  STS not picking log4j.xml nor log4j2.properties to write in file
STS not picking log4j.xml nor log4j2.properties to write in file

Time:01-06

We are migrating to log4j2.xml as a part of log4j2 vulnerability. But the newly created log4j2 is not being picked up during application startup. I have carried out all the below options but in vain.

Options tried:

  1. Tried adding and removing log4j-jcl dependency.
  2. Removed spring-boot-starter-logging dependency.
  3. Tried using log4j2.properties instead of log4j2.xml.
  4. Added logging.config prop in application.properties.
  5. Tried renaming the RollingFile name and providing an incorrect AppenderRef ref purposefully to check for any bean related exception. But the application has started successfully. Which means that this log4j2.xml and log4j2.properties file are not being picked up.

Here are the details of my project. Please let know if any other info is needed.

log4j2.xml:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration monitorinterval="30" status="info" strict="true">
    <Properties>
        <Property name="logPath">${sys:app.config.home}/logs</Property>
    </Properties>
    <Appenders>
         <Console name="ConsoleAppender" target="SYSTEM_OUT" follow="true">
            <PatternLayout>
                <pattern>TYPE=%level, DATE_TIME=%d, CLASS_NAME=%C, METHOD_NAME=%M, %msg%n</pattern>
            </PatternLayout>
        </Console>
        <RollingFile name="RollingFile" fileName="${logPath}/batch.log" filePattern="${logPath}/batch-%d{MM-dd-yyyy}-part%i.log.gz">
            <PatternLayout>
                <pattern>TYPE=%level, DATE_TIME=%d, CLASS_NAME=%C, METHOD_NAME=%M, %msg%n</pattern>
            </PatternLayout>
            <Policies>
                 <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
                <SizeBasedTriggeringPolicy size="200 MB"/>
            </Policies>
            <DefaultRolloverStrategy max="60"/>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="info" additivity="true" includeLocation="true">
            <AppenderRef ref="RollingFile" />
            <AppenderRef ref="ConsoleAppender" />
        </Root>
    </Loggers>
</Configuration>

pom.xml:

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.proj.ds</groupId>
    <artifactId>ProcessorBatch</artifactId>
    <version>1.0</version>
    <packaging>jar</packaging>
    <name>ProcessorBatch</name>
    <description>ProcessorBatch project</description>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
        <relativePath/>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <log4j2.version>2.17.1</log4j2.version>
    </properties>
    <repositories>
        <repository>
            <id>MDM</id>
            <url>https://devnexus.tent.trt.proj.pri:8443/nexus/content/groups/public</url>
            <name>Proj Repo</name>
        </repository>
    </repositories> 
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>           
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-batch</artifactId>
        </dependency>               
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis</artifactId>
            <version>3.5.8</version>
        </dependency>   
        <dependency>
            <groupId>org.mybatis</groupId>
            <artifactId>mybatis-spring</artifactId>
            <version>1.3.3</version>
        </dependency>   
        <dependency>
            <groupId>org.springframework.batch</groupId>
            <artifactId>spring-batch-infrastructure</artifactId>
            </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-oxm</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
        </dependency>
        <dependency>
            <groupId>aopalliance</groupId>
            <artifactId>aopalliance</artifactId>
            <version>1.0</version>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjrt</artifactId>
        </dependency>
        <dependency>
            <groupId>org.aspectj</groupId>
            <artifactId>aspectjweaver</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-api</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.logging.log4j</groupId>
            <artifactId>log4j-core</artifactId>
        </dependency>   
        <dependency>
            <groupId>commons-lang</groupId>
            <artifactId>commons-lang</artifactId>
            <version>2.6</version>
        </dependency>
        <dependency>
            <groupId>commons-collections</groupId>
            <artifactId>commons-collections</artifactId>
            <version>3.2.1</version>
        </dependency>
        <dependency>
            <groupId>joda-time</groupId>
            <artifactId>joda-time</artifactId>
        </dependency>       
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>               
        <dependency>
            <groupId>org.mapstruct</groupId>
            <artifactId>mapstruct-jdk8</artifactId>
            <version>1.1.0.Final</version> 
        </dependency>       
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc7</artifactId>
            <version>12.1.0.1</version>
        </dependency>   
    </dependencies>
    
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <executable>true</executable>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

application-context.xml:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jdbc="http://www.springframework.org/schema/jdbc"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
                            http://www.springframework.org/schema/jdbc
                            http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
                            http://www.springframework.org/schema/util 
                            http://www.springframework.org/schema/util/spring-util-3.2.xsd
                            http://www.springframework.org/schema/context
                            http://www.springframework.org/schema/context/spring-context-3.2.xsd">
    

    <import resource="classpath:context/persistance-context.xml" />
    <import resource="classpath:context/memory-jobrepository.xml" />

    <context:component-scan base-package="com.proj.ds" />
    
    <util:properties id="configProperties" location="file:${app.config.home}/config/batchConfig.properties" />
    <context:property-placeholder location="file:${app.config.home}/config/batchConfig.properties" />
    
    <bean id="processorTasklet" >
        <property name="batchDao" ref="batchDao" />     
        <property name="processorService" ref="processorService" />
        <property name="threadCount" value="${batch.threadcount}" />
    </bean>

    <bean id="batchDao" >
        <property name="metaBatchMapper" ref="metaBatchMapper" />
        <property name="hubBatchMapper" ref="hubBatchMapper" />
    </bean>
    
    <bean id="processorService" >
        <property name="projCompositeService" ref="projCompositeService" />
        <property name="endPointUrl" value="${mdm.endpoint}" />
    </bean>
        
    <bean id="projCompositeService" >
        <property name="handlerResolver" ref="handlerResolver" />
    </bean>
    
    <bean id="handlerResolver" />      

    <bean id="batchUtil" >
        <property name="configProperties" ref="configProperties" />     
    </bean>
    
</beans>

CodePudding user response:

The XML config file should be placed under the src/main/resources folder.

CodePudding user response:

Your Spring Boot project inherits from a Spring Boot Starter parent project, which dependes on SLF4J and logback. You may either migrate your project to logback or you should remove logback dependencies.

To remove logback dependencies try to update your POM file:

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <artifactId>logback-classic</artifactId>
                <groupId>ch.qos.logback</groupId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
</dependencies>
  •  Tags:  
  • Related