Grails Log4J未登录生产

时间:2011-07-17 14:37:54

标签: grails log4j tomcat6

我有一个Grails 1.3.7应用程序,我正在尝试在配置中设置log4j进行生产。 log4j设置在开发中很好,但我无法获得任何显示生产。我试图制作一个滚动文件,但我无法得到任何东西。我甚至在“信息”级别进行配置,这样我就可以确保有一些东西可以通过。我没有看到服务器上的tomcat日志感兴趣。有什么想法吗?

log4j = {
    def catalinaBase = System.properties.getProperty('catalina.base')
    if (!catalinaBase) catalinaBase = '.'   // just in case
    def logDirectory = "${catalinaBase}/logs"

    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 'org.mortbay.log'
    warn 'grails.app'

    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"))

        environments {
            production {
                file name: "prod-errors", file: "${logDirectory}/pps-errors.log".toString(), 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:"prod-roll", datePattern: "'.'yyyy-MM-dd", file:"${logDirectory}/pps-errors-daily.log".toString(), layout: pattern(conversionPattern: "%d{yyyy-MMM-dd HH:mm:ss,SSS} [%t] %c %x%n %-5p %m%n"))
            }
        }
    }

    environments {
        development{

            debug stdout:   'org.hibernate.SQL',
            'org.hibernate.transaction'//,
            //'org.hibernate.cache'
            //trace stdout: 'org.hibernate.type'
        }
        test {
            root { error "stdout", "roll", "errors" }
        }
        production {

            root {
                info "prod-roll", "prod-errors", "stdout", "roll", "errors"
            }

            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'
        }
    }

    debug 'grails.app.task'
    //grails = "debug"
    //StackTrace = "error,errors"

    //additivity.StackTrace = false
}

编辑:

tomcat6中的catalina.out文件具有以下内容:

  

log4j:WARN找不到记录器的appender   (org.springframework.web.context.ContextLoader)。 log4j:警告请   正确初始化log4j系统。

第二次编辑:

我在下面的Burt之后做了一个最小配置:

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"
    }
}

我仍然在tomcat中收到相同的消息,但仍然没有进行日志记录。有什么我必须做的事情来设置tomcat以这种方式登录吗?

编辑3: App-Info的输出。这里没有我的自定义appender,让我相信这就是问题所在。但是,当我在开发模式下运行应用程序时,我可以验证是否实际创建了日志文件:

<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="stdout" class="org.apache.log4j.ConsoleAppender">
      <param name="name" value="stdout"/>
      <layout class="org.apache.log4j.PatternLayout">
         <param name="conversionPattern" value="%d [%t] %-5p %c{2} %x
- %m%n"/>
      </layout>    </appender>    <logger name="StackTrace" additivity="false">
      <appender-ref ref="stacktraceLog"/>    </logger>    <logger name="grails.app">
      <level value="WARN"/>    </logger>    <logger name="grails.app.task">
      <level value="DEBUG"/>    </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"/>    </root> </log4j:configuration>

编辑4:我在登录生产时仍然遇到问题。现在,当我使用下面的日志配置并在调试模式下运行它时,我可以正确地看到app-info工具中的所有appender。但是,当我构建生产代码时,app-info显示将生成的log4j.xml几乎是空白的:

新配置:

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

    // For some reason the 'appenders' section had to come first and it messed everything up! Maybe it just 
    // needs to go before root
    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'
}

在Prod中输出log4j.xml:

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

1 个答案:

答案 0 :(得分:6)

不幸的是,您无法在任意位置添加environments块,并且通常无法使log4j配置非常干,因为它必须在一个闭包中发生。以下是我认为您的目标:

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"
         }
      }
   }
}

我删除了'org.mortbay.log',因为我怀疑你是否正在使用Jetty - 如果你是的话,请把它放回去。此外,'grails.app'列在infowarn下方。我从prod中删除了“stdout”appender,因为它将转储到catalina.out并且你已经配置了文件记录器。

如果使用传统的log4j.xml方法,app-info插件有一个功能,它可以对可用于配置等效日志记录配置的XML进行反向工程。这不是确切的,但应该非常接近。我发现它对于调试这样的日志记录问题非常有用,因为即使您没有使用XML文件语法,您也可以将Config.groovy设置与XML中的内容进行比较,看看缺少什么,配置错误等等。 / p>