Grails - No Logging In Tomcat

2019-03-25 11:17发布

问题:

I cannot get grails log4j logging to work in Tomcat6. I previously thought the problem had to do with Production mode, but now I believe the issue is tomcat.

I have a log4j configuration in my Config.groovy that correctly sets logging when I run the application in STS. I am using the wonderful app-info plugin, which confirms that the correct logging values are being set.

However, when I do a grails war or even a grails dev war with the same code, and plug it into the webapps folder in Tomcat, no logging happens. When I look in the app-info for logging now, I get a basic log4j.xml which is picking up nothing inside of my configuration. Also, when I look at catalina.out, I see:

log4j:WARN No appenders could be found for logger (org.springframework.web.context.ContextLoader).
log4j:WARN Please initialize the log4j system properly.

For completeness, here is the config that is working correctly in STS, but not when run in Tomcat directly:

log4j = {
    String logDirectory = "${System.getProperty('catalina.base') ?: '.'}/logs"

    appenders {
        console name: "stdout", layout: pattern(conversionPattern: "%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n")
        file name: "errors", file: "pps-errors.log", layout: pattern(conversionPattern: "%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n")
        appender new org.apache.log4j.DailyRollingFileAppender(name:"roll", datePattern: "'.'yyyy-MM-dd", file:"pps-rolling.log", layout: pattern(conversionPattern: "%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n"))
    }


    root { info "stdout", "roll", "errors" }
    // Log all at the error level
    info 'org.apache.',
    'org.tomcat.',
    'grails.app',
    'org.acegisecurity',
    'org.codehaus.groovy.grails.web.servlet', // controllers
    'org.codehaus.groovy.grails.web.pages', // GSP
    'org.codehaus.groovy.grails.web.sitemesh', // layouts
    'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
    'org.codehaus.groovy.grails.web.mapping', // URL mapping
    'org.codehaus.groovy.grails.commons', // core / classloading
    'org.codehaus.groovy.grails.plugins', // plugins
    'org.codehaus.groovy.grails.orm.hibernate', // hibernate integration
    'org.springframework',
    'org.hibernate'
    warn 'grails.app'
}

Here is the app-info logging output when this is run in STS:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM 'log4j.dtd'>

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
   <appender name="stacktraceLog" class="org.apache.log4j.FileAppender">
      <param name="file" value="/home/devon/Documents/SynaSync/Projects/ProjectPointSpread/Source/trunk/target/stacktrace.log"/>
      <param name="name" value="stacktraceLog"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="conversionPattern" value="%d [%t] %-5p %c{2} %x - %m%n"/>
      </layout>
   </appender>
   <appender name="roll" class="org.apache.log4j.DailyRollingFileAppender">
      <param name="file" value="pps-rolling.log"/>
      <param name="name" value="roll"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="conversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n"/>
      </layout>
   </appender>
   <appender name="stdout" class="org.apache.log4j.ConsoleAppender">
      <param name="name" value="stdout"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="conversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n"/>
      </layout>
   </appender>
   <appender name="errors" class="org.apache.log4j.FileAppender">
      <param name="file" value="pps-errors.log"/>
      <param name="name" value="errors"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="conversionPattern" value="%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n"/>
      </layout>
   </appender>
   <logger name="StackTrace" additivity="false">
      <appender-ref ref="stacktraceLog"/>
   </logger>
   <logger name="grails.app">
      <level value="WARN"/>
   </logger>
   <logger name="org.acegisecurity">
      <level value="INFO"/>
   </logger>
   <logger name="org.apache.">
      <level value="INFO"/>
   </logger>
   <logger name="org.codehaus.groovy.grails.commons">
      <level value="INFO"/>
   </logger>
   <logger name="org.codehaus.groovy.grails.orm.hibernate">
      <level value="INFO"/>
   </logger>
   <logger name="org.codehaus.groovy.grails.plugins">
      <level value="INFO"/>
   </logger>
   <logger name="org.codehaus.groovy.grails.web.mapping">
      <level value="INFO"/>
   </logger>
   <logger name="org.codehaus.groovy.grails.web.mapping.filter">
      <level value="INFO"/>
   </logger>
   <logger name="org.codehaus.groovy.grails.web.pages">
      <level value="INFO"/>
   </logger>
   <logger name="org.codehaus.groovy.grails.web.servlet">
      <level value="INFO"/>
   </logger>
   <logger name="org.codehaus.groovy.grails.web.sitemesh">
      <level value="INFO"/>
   </logger>
   <logger name="org.hibernate">
      <level value="INFO"/>
   </logger>
   <logger name="org.springframework">
      <level value="INFO"/>
   </logger>
   <logger name="org.tomcat.">
      <level value="INFO"/>
   </logger>
   <root>
      <level value="INFO"/>
      <appender-ref ref="stdout"/>
      <appender-ref ref="roll"/>
      <appender-ref ref="errors"/>
   </root>
</log4j:configuration>

Here is the app-info logging output when run directly in tomcat6:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE log4j:configuration SYSTEM 'log4j.dtd'>

<log4j:configuration xmlns:log4j="http://jakarta.apache.org/log4j/" debug="false">
   <logger name="Test">
      <level value="ALL"/>
   </logger>
   <root>
      <level value="DEBUG"/>
   </root>
</log4j:configuration>

I am using Grails 1.3.7 if that is helpful.

回答1:

It could be the way you define your environment in the DSL config I have similar setup: Grails 1.3.7 + Tomcat 6.0 this works great for me.

log4j = {
    environments {
        production {
        appenders {
            def catalinaBase = System.properties.getProperty('catalina.base')
            if (!catalinaBase) catalinaBase = '.'
            def logDirectory = "${catalinaBase}/logs"

            /* log file appender config & layout */
            appender new DailyRollingFileAppender(
                name:"logfile",
                file:"$logDirectory/logfilename.log",
                layout:pattern(conversionPattern: "%d{yy.MM.dd HH:mm:ss} [%t] %-5p %c %x - %m%n"),
                datePattern:"'.'yyyy-MM-dd",
                threshold: org.apache.log4j.Level.ERROR 
        }

        /* type of errors to log */
        error  'org.codehaus.groovy.grails.scaffolding.view.ScaffoldingViewResolver', 
               'org.codehaus.groovy.grails.web.servlet',  //  controllers
               'org.codehaus.groovy.grails.web.pages', //  GSP
               'org.codehaus.groovy.grails.web.sitemesh', //  layouts
               'org.codehaus.groovy.grails.web.mapping.filter', // URL mapping
               'org.codehaus.groovy.grails.web.mapping', // URL mapping
               'org.codehaus.groovy.grails.commons', // class
               'org.codehaus.groovy.grails.plugins', // plugins
               'org.codehaus.groovy.grails.orm.hibernate', // hibernate
               'org.springframework', // spring
               'org.hibernate', // hibernate



        /* jetty, not required on tomcat */
        warn   'org.mortbay.log'


        /* root logger config */
        root {
            debug 'logfile'
            additivity = true
        }
    }//production

    development{
         /* add config for development evn*/
    }

    test{
         /* add config for test env*/
    } 

}//log4j


回答2:

It had something to do with a bogus web-inf file that resided from a long time ago. This works correctly when that is not there.