Grails Log4J Not Logging In Production

后端 未结 1 730
遥遥无期
遥遥无期 2021-02-11 08:31

I have a Grails 1.3.7 application and am trying to setup log4j for production in the configuration. The log4j settings were fine in development, but I can\'t get anything to sho

相关标签:
1条回答
  • 2021-02-11 09:00

    Unfortunately you can't add environments blocks in arbitrary locations, and in general the log4j configuration can't be made very DRY since it has to occur in one closure. Here's what I think you were getting at:

    import org.apache.log4j.DailyRollingFileAppender
    
    String commonPattern = "%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n"
    environments {
       development {
          log4j = {
             info 'org.apache.',
                  'org.tomcat.',
                  'org.acegisecurity',
                  'org.codehaus.groovy.grails',
                  'org.springframework',
                  'org.hibernate'
             warn 'grails.app'
             debug 'org.hibernate.SQL',
                   'org.hibernate.transaction'
          }
       }
       test {
          log4j = {
             root {
                error "stdout"
             }
    
             info 'org.apache.',
                  'org.tomcat.',
                  'org.acegisecurity',
                  'org.codehaus.groovy.grails',
                  'org.springframework',
                  'org.hibernate'
             warn 'grails.app'
          }
       }
       production {
    
          log4j = {
             String logDirectory = "${System.getProperty('catalina.base') ?: '.'}/logs"
    
             info 'org.apache.',
                  'org.tomcat.',
                  'grails.app',
                  'org.acegisecurity',
                  'org.codehaus.groovy.grails',
                  'org.springframework',
                  'org.hibernate'
             warn 'grails.app'
    
             appenders {
                file name: "errors", file: "${logDirectory}/pps-errors.log",
                     layout: pattern(conversionPattern: commonPattern)
                appender new DailyRollingFileAppender(
                   name:"roll", datePattern: "'.'yyyy-MM-dd",
                   file:"${logDirectory}/pps-rolling.log",
                   layout: pattern(conversionPattern: commonPattern))
    
                file name: "prod-errors", file: "${logDirectory}/pps-errors.log",
                     layout: pattern(conversionPattern: commonPattern)
                appender new DailyRollingFileAppender(
                   name:"prod-roll", datePattern: "'.'yyyy-MM-dd",
                   file:"${logDirectory}/pps-errors-daily.log",
                   layout: pattern(conversionPattern: commonPattern))
             }
    
             root {
                info "prod-roll", "prod-errors", "roll", "errors"
             }
          }
       }
    }
    

    I removed 'org.mortbay.log' since I doubt you're using Jetty - put it back if you are. Also 'grails.app' is listed under info and warn. And I removed the "stdout" appender from prod since that will dump to catalina.out and you've already configured file loggers.

    The app-info plugin has a feature where it will reverse-engineer the XML that could have been used to configure the equivalent logging configuration, if you were using the traditional log4j.xml approach. It's not exact but should be very close. I find it's very useful for debugging logging issues like this since even if you haven't used the XML file syntax you can compare the Config.groovy settings with what ends up in the XML and see what's missing, misconfigured, etc.

    0 讨论(0)
提交回复
热议问题