1

Update: Question has been solved. The problem was that I tried to put my log files into a directory to which tomcat7 user did not have access. See answers and comments below.

Original post:

I have a spring mvc app that runs on tomcat

I build my app with gradle (3.0)

I have just installed slf4j but it only logs to catalina.out (same as in post unable to find logback.xml)

I have put the logback.xml file both under my 'project\src\main\java' and also under 'project\src\main\webapp\resources', but either way it doesn't create the log files, just puts all logs into catalina.out

From the other post mentioned above, it says to put the file under the classpath, which I understand is the 'project\src' directory

Any ideas on how to work this out would be much appreciated

Thanks 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>

project tree:

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 user that runs the server process:

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$

permissions of log folder:

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$
Community
  • 1
  • 1
AHL
  • 738
  • 3
  • 11
  • 35

3 Answers3

1

Put the logback.xml into src/main/resourcers. That is the rght place

Jens
  • 67,715
  • 15
  • 98
  • 113
  • alright ; so the places I have put the file in my project are not being searched by the logger at all ? – AHL Sep 02 '16 at 10:30
  • I put the file in src/main/resources, rebuilt the project and deployed. but still, no log file in my folder /home/vagrant/logs/ – AHL Sep 02 '16 at 10:40
1

if your running a maven setup by default anything you add src/main/resources goes to the root of classpath, so better place it place your

logback.xml

under

src/main/resources

Or alternatively you can add a folder to classpath

<build>
    <resources>
        <resource>
            <directory>src/main/webapp/resources</directory>
        </resource>
    </resources>
 </build>
kuhajeyan
  • 10,727
  • 10
  • 46
  • 71
  • I use gradle ; have put my build.gradle file in my original post – AHL Sep 02 '16 at 10:29
  • how can i do this in gradle ? – AHL Sep 02 '16 at 10:39
  • I put the file in src/main/resources, rebuilt the project and deployed. but still, no log file in my folder /home/vagrant/logs/ – AHL Sep 02 '16 at 10:40
  • @AHL this post explains how to add to gradle http://stackoverflow.com/questions/24724383/add-resources-config-files-to-your-jar-using-gradle – kuhajeyan Sep 02 '16 at 10:41
  • And, check if you log directory has permissions for applications to write. and remember your classes pkg "org.codingpedia" will only log 'DEBUG' or higher – kuhajeyan Sep 02 '16 at 10:52
  • I followed the post and updated my build.gradle file (see post above). Still doesn't work. The directory where I want to pyt my logs has these permissions: drwxrwxr-x 2 vagrant vagrant 4096 Sep 2 19:04 logs/ – AHL Sep 03 '16 at 01:48
  • I followed the post and updated my build.gradle file (see post above). Still doesn't work. The directory where I want to put my logs has these permissions: drwxrwxr-x 2 vagrant vagrant 4096 Sep 2 19:04 logs/. My test logging in the java file are DEBUG so I expect them to be logged given the logger is configured to show all degug and higher messages. – AHL Sep 03 '16 at 01:54
1

How are you starting your server? Are you sure the user running the server process has permissions to the /home/vagrant/logs directory?

Also, in logback.xml, trying setting the debug attribute:

<configuration debug="true"> 

That should output a lot of additional information to the catalina.out if your logback.xml is getting picked up.

kaliatech
  • 17,579
  • 5
  • 72
  • 84
  • I added debug="true" to my configuration but there doesn't seem to be additional logs created in catalina.out. I start my tomcat server using "sudo service tomcat7 stop/start" (from within a bash script). How can I check what user run the server process and then, how can I check that this user has write access to my log directory? My log dir has permissions: "drwxrwxr-x 2 vagrant vagrant 4096 Sep 2 19:04 logs/". Thanks! – AHL Sep 03 '16 at 04:20
  • Not sure if this helps but I have tried to identify which user runs the tomcat process and pasted it into the original post; and also added information about the folder permission of my log directory. – AHL Sep 03 '16 at 04:23
  • it looks like the problem was directory permission related. i have now created a sub-directory under "/var/lib/tomcat7/logs/" and used chown to set its user to tomcat7 user and permission to "drwxr-xr-x", i.e. tomcat7 user will be able to write into it. i have updated my logback.xml above; i ended up using the one provided by MK Yong's website: https://www.mkyong.com/logging/logback-xml-example/ – AHL Sep 03 '16 at 05:56