Spring日志不是特定于机器的

时间:2014-12-22 11:42:21

标签: java tomcat java-8 slf4j logback

我正在使用spring maven tomcat8 java8堆栈。我面临的问题是某些机器上没有生成日志。我正在使用slf4j-logback组合进行日志记录。当我尝试将筹码从java7移到java8时,问题浮出水面。

这是我的logback.xml:

<?xml version="1.0" encoding="UTF-8"?>

<configuration>
    <appender name="dal"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>/var/log/dal/dal.log</file>
        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- daily rollover -->
            <fileNamePattern>/var/log/dal/dal-%d{yyyy-MM-dd}.log.gz</fileNamePattern>
            <!-- keep 30 days' worth of history -->
            <maxHistory>30</maxHistory>
        </rollingPolicy>
        <append>true</append>
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS} %thread %-5level %C:%L] %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <encoder>
            <pattern>[%d{HH:mm:ss.SSS} %thread %-5level %C:%L] %msg%n</pattern>
        </encoder>
    </appender>

    <appender name="email-appender" class="ch.qos.logback.classic.net.SMTPAppender">
        <to></to>
        <from></from>
        <subject>API Logs - Error while sending notification</subject>
        <layout class="ch.qos.logback.classic.PatternLayout">
            <pattern>[%d{HH:mm:ss.SSS} %thread %-5level %C:%L] %msg%n</pattern>
        </layout>
    </appender>

    <root level="DEBUG">
        <appender-ref ref="dal" />
        <appender-ref ref="STDOUT" />
    </root>
</configuration>

使用此功能,我希望在自定义位置/var/log/dal/dal.logcatalina.out

中的两个位置获取日志

以下是生成日志的服务器的一些相关详细信息:

OS:

apache-tomcat-8.0.15 $ uname -a
Linux azi 3.2.0-74-generic #109-Ubuntu SMP Tue Dec 9 16:45:49 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

MVN:

apache-tomcat-8.0.15 $ mvn -version
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option MaxPermSize=1024m; support was removed in 8.0
Apache Maven 3.0.4
Maven home: /usr/share/maven
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: /usr/lib/jvm/java-8-oracle/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.2.0-74-generic", arch: "amd64", family: "unix"

Tomcat的:

apache-tomcat-8.0.15 $ bin/version.sh 
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.15
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.15
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.15/temp
Using JRE_HOME:        /usr/lib/jvm/java-8-oracle/
Using CLASSPATH:       /opt/apache-tomcat-8.0.15/bin/bootstrap.jar:/opt/apache-tomcat-8.0.15/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.15
Server built:   Nov 2 2014 19:25:20 UTC
Server number:  8.0.15.0
OS Name:        Linux
OS Version:     3.2.0-74-generic
Architecture:   amd64
JVM Version:    1.8.0_25-b17
JVM Vendor:     Oracle Corporation

JAVA:

apache-tomcat-8.0.15 $ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

许可:

ls -al /var/log/dal/
total 72
drwxrwxrwx 2 root root  4096 Dec 22 19:50 .
drwxrwxrwx 4 root root  4096 Oct 21 09:47 ..
-rw-r--r-- 1 root root 60925 Dec 22 20:38 dal.log

以下是我没有得到任何日志的服务器的详细信息: OS:

sysadmin@ip-172-30-0-26:/opt/apache-tomcat-8.0.15$ uname -a
Linux ip-172-30-0-26 3.13.0-29-generic #53-Ubuntu SMP Wed Jun 4 21:00:20 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux

MVN:

sysadmin@ip-172-30-0-26:/opt/apache-tomcat-8.0.15$ mvn -version
Apache Maven 3.2.2 (45f7c06d68e745d05611f7fd14efb6594181933e; 2014-06-17T19:21:42+05:30)
Maven home: /opt/apache-maven-3.2.2
Java version: 1.8.0_25, vendor: Oracle Corporation
Java home: /opt/jdk1.8.0_25-linux-x64/java-linux-x64/jre
Default locale: en_US, platform encoding: UTF-8
OS name: "linux", version: "3.13.0-29-generic", arch: "amd64", family: "unix"

Tomcat的:

sysadmin@ip-172-30-0-26:/opt/apache-tomcat-8.0.15$ sudo bin/version.sh 
Using CATALINA_BASE:   /opt/apache-tomcat-8.0.15
Using CATALINA_HOME:   /opt/apache-tomcat-8.0.15
Using CATALINA_TMPDIR: /opt/apache-tomcat-8.0.15/temp
Using JRE_HOME:        /usr
Using CLASSPATH:       /opt/apache-tomcat-8.0.15/bin/bootstrap.jar:/opt/apache-tomcat-8.0.15/bin/tomcat-juli.jar
Server version: Apache Tomcat/8.0.15
Server built:   Nov 2 2014 19:25:20 UTC
Server number:  8.0.15.0
OS Name:        Linux
OS Version:     3.13.0-29-generic
Architecture:   amd64
JVM Version:    1.8.0_25-b17
JVM Vendor:     Oracle Corporation

JAVA:

sysadmin@ip-172-30-0-26:/opt/apache-tomcat-8.0.15$ java -version
java version "1.8.0_25"
Java(TM) SE Runtime Environment (build 1.8.0_25-b17)
Java HotSpot(TM) 64-Bit Server VM (build 25.25-b02, mixed mode)

许可:

sysadmin@ip-172-30-0-26:/opt/apache-tomcat-8.0.15$ ls -al /var/log/dal/
total 8
drwxrwxrwx 2 root root 4096 Dec 22 19:48 .
drwxrwxrwx 4 root root 4096 Dec 22 19:48 ..

另一点需要注意的事实是,当我System.out.println("msg")时,"msg"会在两台服务器上进入catalins.out。如果我在机器日志上生成/var/log/dal的更改权限,则会在catalina.out中生成日志。所以这不应该与权限相关。

我曾尝试将war文件从一台服务器复制到另一台服务器,并且没有发现任何行为更改。所以,这不应该与maven相关。 Tomcat和Java版本是一样的。这可能与操作系统有关吗?

更新:当我使用mvn clean tomcat7:run运行我的应用程序时,我会在所有服务器上获取日志。所以问题似乎与tomcat8有关。我甚至尝试将tomcat8从一台服务器复制到另一台服务器。它没有帮助。

2 个答案:

答案 0 :(得分:1)

请确保类路径中没有其他slf4j bindings(例如,在tomcat中)。

答案 1 :(得分:0)

这是slf4j在类路径中识别多个绑定的情况。在这种情况下,slf4j几乎选择绑定randomly。就我而言,这种情况正在发生,因为slf4j-log4j12绑定可通过solr-core获得。要删除它,我必须明确排除它:

    <dependency>
        <groupId>org.apache.solr</groupId>
        <artifactId>solr-core</artifactId>
        <version>${solr.version}</version>
        <exclusions>
            <exclusion>
                <groupId>org.slf4j</groupId>
                <artifactId>slf4j-log4j12</artifactId>
            </exclusion>
        </exclusions>
    </dependency>