Grails: log stacktrace to stdout

泄露秘密 提交于 2019-12-03 19:39:08
Lari Hotari

Grails Bug report: http://jira.grails.org/browse/GRAILS-2730 (contains some workarounds)

If you want stacktraces to stdout:

log4j = {
  appenders {
    console name:'stacktrace'
    ...
  }
...
}

Disable stacktrace.log:

log4j = {
  appenders {
    'null' name:'stacktrace'
    ...
  }
...
}

stacktraces to application specific log file in Tomcat logs directory

log4j = {
  appenders {
    rollingFile name:'stacktrace', maxFileSize:"5MB", maxBackupIndex: 10, file:"${System.getProperty('catalina.home')}/logs/${appName}_stacktrace.log", 'append':true, threshold:org.apache.log4j.Level.ALL
    ...
  }
...
}

kudos to this blog post: http://haxx.sinequanon.net/2008/09/grails-stacktracelog/

cdeszaq

Here is what I have to deal with this:

log4j = {
    appenders {
        // Be smart if we are running in tomcat or not 
        String logDir = Environment.warDeployed ? 
                System.getProperty('catalina.home') + '/logs' : 
                'target'

        // Handle the stacktrace log correctly
        rollingFile name:'stacktrace',
                maxFileSize: "5MB",
                maxBackupIndex: 10,
                file: "${logDir}/${appName}_stacktrace.log",
                layout: pattern(conversionPattern: "'%d [%t] %-5p %c{2} %x - %m%n'"),
                'append': true,
                threshold: org.apache.log4j.Level.ALL
    }
}

This lets us gracefully handle being deployed as a WAR in Tomcat, as well as in development. It also has the advantage of allowing multiple grails applications to run within the same container without mushing all of their stacktrace logs together.

Inspiration for this comes from @FlareCoder's answer above, as well as from @BurtBeckwith's mailing list post.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!