tomcat上的spring mvc app:slf4j找不到logback.xml,只记录到catalina.out

时间:2016-09-02 09:38:00

标签: java spring tomcat slf4j

更新:问题已经解决。问题是我试图将我的日志文件放入tomcat7用户无权访问的目录中。请参阅下面的答案和评论。

原帖:

我有一个在tomcat上运行的spring mvc应用程序

我使用gradle(3.0)

构建我的应用程序

我刚安装了slf4j,但它只记录到catalina.out(与帖子unable to find logback.xml相同)

我已将logback.xml文件放在我的'project \ src \ main \ java'下,也放在'project \ src \ main \ webapp \ resources'下,但无论如何它都不会创建日志文件,只需将所有日志放入catalina.out

从上面提到的另一篇文章中,它说把文件放在classpath下,我理解的是'project \ src'目录

非常感谢任何关于如何解决这个问题的想法

由于 AHL

logback.xml:

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

    <!-- *** CONFIGURE APPLICATION LOG DIRECTORY *** -->
    <property name="DEV_HOME" value="/var/lib/tomcat7/logs/spring4-1" />

    <appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
        <layout class="ch.qos.logback.classic.PatternLayout">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} [%thread] %-5level %logger{36} - %msg%n
            </Pattern>
        </layout>
    </appender>

    <appender name="FILE-AUDIT"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEV_HOME}/debug.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} - %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${DEV_HOME}/archived/debug.%d{yyyy-MM-dd}.%i.log
                        </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

    </appender>

    <appender name="FILE-ERROR"
        class="ch.qos.logback.core.rolling.RollingFileAppender">
        <file>${DEV_HOME}/error.log</file>
        <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
            <Pattern>
                %d{yyyy-MM-dd HH:mm:ss} - %msg%n
            </Pattern>
        </encoder>

        <rollingPolicy class="ch.qos.logback.core.rolling.TimeBasedRollingPolicy">
            <!-- rollover daily -->
            <fileNamePattern>${DEV_HOME}/archived/error.%d{yyyy-MM-dd}.%i.log
                        </fileNamePattern>
            <timeBasedFileNamingAndTriggeringPolicy
                class="ch.qos.logback.core.rolling.SizeAndTimeBasedFNATP">
                <maxFileSize>10MB</maxFileSize>
            </timeBasedFileNamingAndTriggeringPolicy>
        </rollingPolicy>

    </appender>

    <!-- Send logs to both console and file audit -->
    <logger name="com.concretepage" level="debug"
        additivity="false">
        <appender-ref ref="FILE-AUDIT" />
        <appender-ref ref="STDOUT" />
    </logger>

    <root level="error">
        <appender-ref ref="FILE-ERROR" />
    </root>

</configuration>

项目树:

vagrant@precise32:/vagrant/Spring4RESTAngularJS$ tree .
.
|-- build
|   |-- classes
|   |   `-- main
|   |       `-- com
|   |           `-- concretepage
|   |               |-- config
|   |               |   |-- AppConfig.class
|   |               |   |-- DBConfig.class
|   |               |   `-- WebAppInitializer.class
|   |               |-- controller
|   |               |   `-- PersonController.class
|   |               |-- dao
|   |               |   |-- IPersonDAO.class
|   |               |   `-- PersonDAO.class
|   |               |-- entity
|   |               |   `-- Person.class
|   |               `-- service
|   |                   |-- IPersonService.class
|   |                   `-- PersonService.class
|   |-- dependency-cache
|   |-- libs
|   |   `-- spring4-1.war
|   |-- resources
|   |   `-- main
|   |       `-- logback.xml
|   `-- tmp
|       |-- compileJava
|       |   `-- emptySourcePathRef
|       `-- war
|           `-- MANIFEST.MF
|-- build.gradle
|-- deploy.sh
`-- src
    `-- main
        |-- java
        |   `-- com
        |       `-- concretepage
        |           |-- config
        |           |   |-- AppConfig.java
        |           |   |-- DBConfig.java
        |           |   `-- WebAppInitializer.java
        |           |-- controller
        |           |   `-- PersonController.java
        |           |-- dao
        |           |   |-- IPersonDAO.java
        |           |   `-- PersonDAO.java
        |           |-- entity
        |           |   `-- Person.java
        |           `-- service
        |               |-- IPersonService.java
        |               `-- PersonService.java
        |-- resources
        |   `-- logback.xml
        `-- webapp
            |-- resources
            |   |-- css
            |   |   `-- style.css
            |   |-- js
            |   |   |-- app.js
            |   |   `-- lib
            |   |       |-- angular.min.js
            |   |       `-- angular-resource.min.js
            |   `-- sql
            |       `-- db.sql
            `-- WEB-INF
                `-- view
                    `-- home.jsp

37 directories, 30 files
vagrant@precise32:/vagrant/Spring4RESTAngularJS$

的build.gradle:

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'war'
archivesBaseName = 'spring4'
version = '1' 
repositories {
    mavenCentral()
}
dependencies {
    compile 'org.springframework.boot:spring-boot-starter-web:1.3.3.RELEASE'
    compile 'org.springframework.boot:spring-boot-starter-data-jpa:1.3.3.RELEASE'
    compile 'org.hibernate:hibernate-core:4.3.6.Final'
    compile 'mysql:mysql-connector-java:5.1.31'
    compile 'commons-dbcp:commons-dbcp:1.4'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat:1.3.3.RELEASE'    
    compile "ch.qos.logback:logback-classic:1.1.3"
    compile "org.slf4j:log4j-over-slf4j:1.7.13"
} 
configurations.all {
    exclude group: "org.slf4j", module: "slf4j-log4j12"
    exclude group: "log4j", module: "log4j"
}
sourceSets {
    main {
        resources {
            srcDirs "src/main/resources"
        }
    }
}

运行服务器进程的tomcat用户:

vagrant@precise32:/vagrant/Spring4RESTAngularJS$ ps auxwww | grep -v grep | grep tomcat
tomcat7   2435  3.2 77.0 1238660 291328 ?      Sl   14:13   0:16 /usr/lib/jvm/java-7-oracle/bin/java -Djava.util.logging.config.file=/var/lib/tomcat7/conf/logging.properties -Djava.awt.headless=true -Xmx128m -XX:+UseConcMarkSweepGC -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Xms512m -Xmx1024m -Djava.endorsed.dirs=/usr/share/tomcat7/endorsed -classpath /usr/share/tomcat7/bin/bootstrap.jar:/usr/share/tomcat7/bin/tomcat-juli.jar -Dcatalina.base=/var/lib/tomcat7 -Dcatalina.home=/usr/share/tomcat7 -Djava.io.tmpdir=/tmp/tomcat7-tomcat7-tmp org.apache.catalina.startup.Bootstrap start
vagrant@precise32:/vagrant/Spring4RESTAngularJS$

日志文件夹的权限:

vagrant@precise32:/vagrant/Spring4RESTAngularJS$ ll /home/vagrant/
total 72
drwxr-xr-x 7 vagrant vagrant 4096 Sep  2 19:04 ./
drwxr-xr-x 3 root    root    4096 Sep 14  2012 ../
-rw------- 1 vagrant vagrant 8574 Sep  3 09:31 .bash_history
-rw-r--r-- 1 vagrant vagrant  220 Sep 14  2012 .bash_logout
-rw-r--r-- 1 vagrant vagrant 3486 Sep 14  2012 .bashrc
drwx------ 2 vagrant vagrant 4096 Sep 14  2012 .cache/
drwxrwxr-x 6 vagrant vagrant 4096 Aug 28 20:01 .gradle/
drwxrwxr-x 2 vagrant vagrant 4096 Sep  2 19:04 logs/
drwxrwxr-x 3 vagrant vagrant 4096 Aug 27 21:59 .m2/
-rw------- 1 vagrant vagrant 1155 Aug 30 22:28 .mysql_history
-rwxr-xr-x 1 vagrant vagrant 6487 Sep 14  2012 postinstall.sh*
-rw-r--r-- 1 vagrant vagrant  675 Sep 14  2012 .profile
drwx------ 2 vagrant vagrant 4096 Aug 27 08:37 .ssh/
-rw-r--r-- 1 vagrant vagrant    0 Sep 14  2012 .sudo_as_admin_successful
-rw------- 1 vagrant vagrant    6 Sep 14  2012 .vbox_version
-rw------- 1 vagrant vagrant   12 Sep 14  2012 .veewee_version
vagrant@precise32:/vagrant/Spring4RESTAngularJS$

3 个答案:

答案 0 :(得分:1)

logback.xml放入src/main/resourcers。那是最好的地方

答案 1 :(得分:1)

如果您默认运行maven设置,那么添加src / main / resources的任何内容都会转到classpath的根目录,所以最好放置它

  

logback.xml

下的

  

的src /主/资源

或者您可以将文件夹添加到类路径

<build>
    <resources>
        <resource>
            <directory>src/main/webapp/resources</directory>
        </resource>
    </resources>
 </build>

答案 2 :(得分:1)

你是如何启动服务器的?您确定运行服务器进程的用户是否具有/ home / vagrant / logs目录的权限?

另外,在logback.xml中,尝试设置debug属性:

<configuration debug="true"> 

这应该向catalina.out 输出大量额外信息,如果你的logback.xml被拿起了。