简体   繁体   中英

Spring boot logging partially to log file and console when used log4j2

I was trying to use log4j2 for logging in a spring boot application it works fine but the issue is not all logging goes to the log file as configured in the log4j2.xml few logs are directed to console attached the snippet of my pom followed by log4j2.xml and the output to console and output to logfile

pom.xml

<groupId>CNER</groupId>
<artifactId>WS</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>WS</name>
<url>http://maven.apache.org</url>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.3.2.RELEASE</version>
</parent>
<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>3.8.1</version>
        <scope>test</scope>
    </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-data-jpa</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-data-rest</artifactId>
        <exclusions>
            <exclusion>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-logging</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
    <dependency>
        <groupId>org.postgresql</groupId>
        <artifactId>postgresql</artifactId>
        <version>9.4-1200-jdbc4</version>
    </dependency>
    <dependency>
        <groupId>com.mangofactory</groupId>
        <artifactId>swagger-springmvc</artifactId>
        <version>0.8.5</version>
    </dependency>
    <dependency>
        <groupId>commons-httpclient</groupId>
        <artifactId>commons-httpclient</artifactId>
        <version>3.1</version>
    </dependency>
    <dependency>
        <groupId>CNER</groupId>
        <artifactId>DAL</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>com.collabnet.eventrouter</groupId>
        <artifactId>rabbitmq-connection</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>CNER</groupId>
        <artifactId>Utils</artifactId>
        <version>0.0.1-SNAPSHOT</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-log4j2</artifactId>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </repository>
    <repository>
        <id>org.jboss.repository.releases</id>
        <name>JBoss Maven Release Repository</name>
        <url>https://repository.jboss.org/nexus/content/repositories/releases</url>
    </repository>
</repositories>

<pluginRepositories>
    <pluginRepository>
        <id>spring-releases</id>
        <name>Spring Releases</name>
        <url>https://repo.spring.io/libs-release</url>
    </pluginRepository>
</pluginRepositories>

log4j2.xml

 <Properties>

        <Property name="LOG_DIR">${env:logging.file}</Property>
        <Property name="ARCHIVE">${LOG_DIR}/archive</Property>
        <Property name="PATTERN">%-5level %d [%t] %c:%M(%L): %m%n</Property>
    </Properties>
    <Appenders>
        <Console name="STDOUT" target="SYSTEM_OUT">
            <PatternLayout pattern="${PATTERN}"/>
        </Console>

        <RollingFile name="fileWriter"
                     fileName="${LOG_DIR}/erdaemon.log"
                     filePattern="${ARCHIVE}/erdaemon.log.%d{yyyy-MM-dd-hh-mm}.gz">
            <PatternLayout pattern="${PATTERN}"/>
            <OnStartupTriggeringPolicy  />
        </RollingFile>
    </Appenders>
    <Loggers>
        <Root level="INFO">
            <AppenderRef ref="fileWriter"/>
        </Root>
        <Logger name="com" level="TRACE" additivity="false">
            <AppenderRef ref="fileWriter"/>
        </Logger>

    </Loggers>

console output

 > SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found
    > binding in
    > [jar:file:/C:/Users/dinesh.reddy/.m2/repository/org/slf4j/slf4j-simple/1.7.13/slf4j-simple-1.7.13.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in
    > [jar:file:/C:/Users/dinesh.reddy/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.4.1/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]
    > SLF4J: See http://www.slf4j.org/codes.html#multiple_bindings for an
    > explanation. SLF4J: Actual binding is of type
    > [org.slf4j.impl.SimpleLoggerFactory]
    > 
    > [main] INFO CNER.WS.App - Starting App on HYD01-DINESH-LT with PID
    > 11444 (E:\masterws\event_router\server\WebServices\WS\target\classes
    > started by dinesh.reddy in
    > E:\masterws\event_router\server\WebServices\WS) [main] INFO
    > CNER.WS.App - The following profiles are active: default [main] INFO
    > org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext
    > - Refreshing org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext@40c80397:
    > startup date [Fri Mar 25 06:22:11 UTC 2016]; root of context hierarchy
    > [main] INFO
    > org.springframework.beans.factory.support.DefaultListableBeanFactory -
    > Overriding bean definition for bean 'beanNameViewResolver' with a
    > different definition: replacing [Root bean: class [null]; scope=;
    > abstract=false; lazyInit=false; autowireMode=3; dependencyCheck=0;
    > autowireCandidate=true; primary=false;
    > factoryBeanName=org.springframework.boot.autoconfigure.web.ErrorMvcAutoConfiguration$WhitelabelErrorViewConfiguration;
    > factoryMethodName=beanNameViewResolver; initMethodName=null;
    > destroyMethodName=(inferred); defined in class path resource

logfile output

INFO  2016-03-25 11:48:31,816 [main] CNER.WS.App:main(54): Starting Event Router Server ...
INFO  2016-03-25 06:18:32,797 [background-preinit] org.hibernate.validator.internal.util.Version:<clinit>(17): HV000001: Hibernate Validator 5.2.2.Final
INFO  2016-03-25 06:18:38,409 [localhost-startStop-1] org.hibernate.jpa.internal.util.LogHelper:logPersistenceUnitInformation(46): HHH000204: Processing PersistenceUnitInfo [
    name: default
    ...]

Try to get rid of the multiple bindings :

SLF4J: Class path contains multiple SLF4J bindings. SLF4J: Found
> binding in
> [jar:file:/C:/Users/dinesh.reddy/.m2/repository/org/slf4j/slf4j-simple/1.7.13/slf4j-simple-1.7.13.jar!/org/slf4j/impl/StaticLoggerBinder.class] SLF4J: Found binding in
> [jar:file:/C:/Users/dinesh.reddy/.m2/repository/org/apache/logging/log4j/log4j-slf4j-impl/2.4.1/log4j-slf4j-impl-2.4.1.jar!/org/slf4j/impl/StaticLoggerBinder.class]

Exclude one of the jars mentioned, preferably the one not added by spring.

Have a look at the dependencies ( mvn dependency:tree see also mvn dependency tree ) and <exclude> the unwanted dependency.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM