0
votes

I want to configure log4j with spring boot 1.5.10.RELEASE. I exclude spring-boot-starter-logging and I add log4j dependency. Before spring boot start I have all logs but after spring boot init logs stopped.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
        <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-core</artifactId>
    <version>2.11.0</version>
</dependency>
<dependency>
    <groupId>org.apache.logging.log4j</groupId>
    <artifactId>log4j-api</artifactId>
    <version>2.12.1</version>
</dependency>

Console logs:

...
2019-10-25 11:57:16,130 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=LogToConsole
2019-10-25 11:57:16,130 main DEBUG Registering MBean org.apache.logging.log4j2:type=18b4aac2,component=Appenders,name=LogToFile

  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::       (v1.5.10.RELEASE)

paź 25, 2019 11:57:18 AM org.apache.catalina.core.StandardService startInternal
INFO: Starting service [Tomcat]
paź 25, 2019 11:57:18 AM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.5.27

And log4j2.xml file:

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <Console name="LogToConsole" target="SYSTEM_OUT">
            <PatternLayout pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - %msg%n"/>
        </Console>
        <File name="LogToFile" fileName="logs/app.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>
    <Loggers>
        <Logger name="src.main.java" level="debug" additivity="false">
            <AppenderRef ref="LogToFile"/>
            <AppenderRef ref="LogToConsole"/>
        </Logger>
        <Logger name="org.springframework.boot" level="error" additivity="false">
            <AppenderRef ref="LogToConsole"/>
        </Logger>
        <Root level="error">
            <AppenderRef ref="LogToFile"/>
            <AppenderRef ref="LogToConsole"/>
        </Root>
    </Loggers>
</Configuration>

And idea what am I doing wrong?

1

1 Answers

0
votes

You are using

<Logger name="src.main.java" level="debug" additivity="false"> .  == I simply replaced it with my package name present inside src/main/java and its now working.

In my case changed it as below :-

<Loggers>
    <Logger name="com.example.demo.test" level="debug" additivity="false">
        <AppenderRef ref="fileLogger"/>
        <AppenderRef ref="console"/>
    </Logger>

    <Root level="info">
        <AppenderRef ref="fileLogger"/>
        <AppenderRef ref="console"/>
    </Root>
</Loggers>