在单个appender上使用多个记录器 [英] Using multiple loggers on a single appender

查看:112
本文介绍了在单个appender上使用多个记录器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是我的日志记录配置。它正在记录我想要的内容,但它只记录到文件中。我希望在控制台上看到相同的信息。我已经尝试了几种变体,但是对于每种变体,我只能获取记录到控制台或文件的信息,而不是两者。

Below is my logging configuration. It is logging exactly what I want, but it is only logging it to a file. I would like to see the same information on the console. I have tried several variations, but with every variation I can only get the information logged to either the console or a file, not both.

    def appName = grails.util.Metadata.current.'app.name'
    def catalinaBase = System.properties.getProperty('catalina.base')
    catalinaBase = catalinaBase ?: "."
    def logDirectory = "${catalinaBase}${File.separator}logs${File.separator}${appName}"

    // log4j configuration
    log4j = {
        appenders {
            rollingFile name: 'stdout',     file: "${logDirectory}${File.separator}${appName}.log".toString(), maxFileSize: '100MB' // Use the same file for stdout and stacktrace
            rollingFile name: 'stacktrace', file: "${logDirectory}${File.separator}${appName}.log".toString(), maxFileSize: '100MB' // Use the same file for stdout and stacktrace
        }

        warn   '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',
               'net.sf.ehcache.hibernate'

        all    'grails.app.controllers.test',
               'grails.app.domain.test',
               'grails.app.services.test',
               'grails.app.taglib.test',
               'grails.app.conf.test',
               'grails.app.filters.test'
    }


推荐答案

首先,有两个不同的appender登录到同一个文件是一个坏主意,如果你想要满堆栈跟踪到正常日志的相同位置,那么您应该重新指向th在 logger 级别而不是 appender 级别。

Firstly, having two different appenders that log to the same file is a bad idea, if you want the full stacktraces to go to the same place as the normal logs then you should re-point things at the logger level rather than the appender level.

对于您的实际问题,我会创建一个控制台appender和一个文件appender,并将它们都附加到 root 记录器中。

For your actual problem, I would create a console appender and a single file appender, and attach them both to the root logger.

log4j = {
  appenders {
    console name:'stdout'
    rollingFile name: 'applog',     file: "${logDirectory}${File.separator}${appName}.log".toString(), maxFileSize: '100MB'
    'null' name:'stacktrace' // prevent Grails trying to create stacktrace.log
  }

  root {
    warn 'stdout', 'applog'
  }

  // Send full stack traces to the main appName.log file
  warn applog:'StackTrace'

  // individual warn/all logger configurations as before
}

这将发送相同的日志到标准输出和日志文件,但只会发送完整的堆栈跟踪文件(而不是控制台)。如果你真的想要在控制台上的完整堆栈跟踪,那么将其更改为

This will send the same logs to stdout and to the log file, but will only send full stack traces to the file (not to the console). If you really do want the full stack traces on the console too then change it to

  warn applog:'StackTrace', stdout:'StackTrace'

或者,您可以考虑完全禁用StackTrace记录器(删除整个警告applog:'StackTrace'行),然后通过将系统属性 grails.full.stacktrace 设置为<$来禁用堆栈跟踪过滤c $ c> true ,这样你就可以得到正常记录器记录的完整未过滤的堆栈跟踪。

Alternatively, you might consider disabling the StackTrace logger altogether (remove the whole warn applog:'StackTrace' line) and then also disabling stack trace filtering by setting the system property grails.full.stacktrace to true so you get full unfiltered stack traces recorded by the normal loggers.

这篇关于在单个appender上使用多个记录器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆