No appenders could be found for logger in local se

2019-08-27 14:36发布

问题:

We have two servers. One for QA and another is Production server (hosted on AWS). I have used "apache-tomcat-8.5.16" server in both environment. We have developed Spring boot app and Log4j configuration is same for applications deployed in both servers.

logging.level.net.companyname= DEBUG
logging.level.com.google.api.ads.dfp.lib.client.DfpServiceClient.soapXmlLogger=WARN
logging.level.com.google.api.ads.dfp.lib.client.DfpServiceClient.requestInfoLogger=INFO

Yet, in local server (used for QA), we are getting the error "log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment" .

SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an explanation.
SLF4J: Actual binding is of type [org.slf4j.impl.Log4jLoggerFactory]
log4j:WARN No appenders could be found for logger (org.springframework.web.context.support.StandardServletEnvironment).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.

But in Production server we are getting logs without any problem. If it does matter, below is the pom.xml configuration related to log4j.

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-log4j2</artifactId>
    <scope>runtime</scope>
</dependency>
<dependency>
    <groupId>log4j</groupId>
    <artifactId>log4j</artifactId>
    <version>1.2.16</version>
</dependency>
    <dependency>
    <groupId>org.slf4j</groupId>
    <artifactId>slf4j-log4j12</artifactId>
</dependency>

Even configurations inside "apache-tomcat-8.5.16/conf/logging.properties" file are also same in both servers.

How to get logs as we are getting in Production environment? What else would make difference when both tomcat server versions are same and same war is deployed on both of them with same configuration settings?

Thank you.

Update: I downloaded the war file to local from server for testing and even there, log4j is working fine. What else do I need to check?

Thank you.

回答1:

This is an alternative and recommended solution. After waiting for a week, I thought, why should I worry on framework that has reached it's end of life. I upgraded to log4j2 and provided log4j2.xml file in the classpath (in my case, /src/main/resources, working in STS-Spring Tool Suite, Spring application.).

However the actual reason for this weird behavior is still unknown.

Below is the pom.xml file tailored only log4j2 dependency for verbosity.

    <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.springframework.boot</groupId>
        <artifactId>spring-boot-starter</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
        <scope>runtime</scope>
    </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>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
    </dependency>

log4j2.xml

<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="DEBUG">
    <Appenders>
        <Console name="Console" target="SYSTEM_OUT">
            <PatternLayout
                pattern="%d{HH:mm:ss.SSS} [%t] %-5level %logger{36} - ***CustomLog*** %msg ***/CustomLog***%n" />
        </Console>

        <File name="traceLoggerFile" fileName="logs/trace.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
        <File name="debugLoggerFile" fileName="logs/debug.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
        <File name="infoLoggerFile" fileName="logs/info.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
        <File name="errorLoggerFile" fileName="logs/errors.log">
            <PatternLayout>
                <Pattern>%d %p %c{1.} [%t] %m%n</Pattern>
            </PatternLayout>
        </File>
    </Appenders>

    <Loggers>
        <Logger name="com.packageName" level="TRACE">
            <AppenderRef ref="traceLoggerFile" level="TRACE" />
            <AppenderRef ref="debugLoggerFile" level="DEBUG" />
            <AppenderRef ref="infoLoggerFile" level="INFO" />
            <AppenderRef ref="errorLoggerFile" level="ERROR" />
        </Logger>
        <Root level="DEBUG">
            <AppenderRef ref="Console" />
        </Root>
    </Loggers>
</Configuration>

You need to give proper package name or class name in this line

<Logger name="com.packageName" level="TRACE">

Will be happy if anyone can find the actual reason.

Thank you.