为什么stacktrace.log不会在grails 3中填充logback? [英] Why does stacktrace.log not fill with logback in grails 3?
问题描述
当您创建一个新的grails应用程序时,默认的logback.groovy文件(以及几乎每个logback.groovy的例子,甚至 Haki先生的例子)包含下面的代码,我简化了这些代码,专注于相关部分:
root(错误,['STDOUT'])
appender(FULL_STACKTRACE,FileAppender){
file =build /stacktrace.log
append = true
encoder(PatternLayoutEncoder){
pattern =%level%logger - %msg%n
}
}
logger(StackTrace,ERROR,['FULL_STACKTRACE'],false)
此方法不会导致错误输出到stacktrace.log文件。
@ JeffScottBrown的答案包含以下Bootstrap.groovy文件以测试堆栈跟踪是否按预期进行日志记录:
class BootStrap {
def init = {servletContext - >
log.error'这是一个新错误'
}
def destroy = {
}
}
使用该引导文件,运行grails应用程序不会产生任何输出到 build / stacktrace.log
中。
如果您删除 StackTrace
记录器,并添加 FULL_STACKTRACE
到根记录器:
root(错误,['STDOUT','FULL_STACKTRACE']
您将输出到stacktrace.log中。 StackTrace
记录到 grails.app.init.Bootstrap
(感谢@JeffScottBrown这行):
logger'grails.app.init.BootStrap',ERROR,['FULL_STACKTRACE'],false
,你会得到输出到stacktrace.log
这个观察使我相信 StackTrace
记录器不会执行我进一步认为,任何未命名为包的记录器都不起作用。
作为所有这些的结果,我的问题是:
- 对于非包/类命名记录器,logback是否有效?
- 如果是这样,为什么
StackTrace
logger在默认logback.groovy
不会导致输出到stacktrace.log?
- 如果是这样,为什么
编辑:
- 我的主要问题是
StackTrace
记录器看起来完全没有必要,为什么它包含在默认文件中?
Second Edit :
确认这一点的另一种方法是仅将 StackTrace
记录器写入
appender),并且监视stacktraces从控制台中消失。 StackTrace,ERROR,['STDOUT','FULL_STACKTRACE'],false)
logger((stdout
root(ERROR,[])
log。错误'这是一个新的错误'
将常规错误级别消息记录到名为<$ c的记录器$ C> grails.app.init.yourapp.BootStrap 。它不会登录到名为 StackTrace
的记录器。
FULL_STACKTRACE
appender用于未经过滤的堆栈跟踪。这些由框架写入记录器 StackTrace
。
如果您替换
log.error'这是一个新错误'
with
throw new RuntimeException(foo)
您将看到完整的堆栈记录被记录到stacktrace.log中。
When you create a new grails application, the default logback.groovy file (and almost every example of a logback.groovy, even Mr Haki's example) contains the following code, which I have simplified to focus on the relevant piece:
root(ERROR, ['STDOUT'])
appender("FULL_STACKTRACE", FileAppender) {
file = "build/stacktrace.log"
append = true
encoder(PatternLayoutEncoder) {
pattern = "%level %logger - %msg%n"
}
}
logger("StackTrace", ERROR, ['FULL_STACKTRACE'], false )
However, following this approach does not result in errors being output to the stacktrace.log file.
@JeffScottBrown's answer contained the following Bootstrap.groovy file to test if the stacktrace was logging as expected:
class BootStrap {
def init = { servletContext ->
log.error 'this is a new error'
}
def destroy = {
}
}
With that bootstrap file, running the grails application will not produce any output into build/stacktrace.log
.
If you remove the StackTrace
logger, and add FULL_STACKTRACE
to the root logger:
root(ERROR, ['STDOUT', 'FULL_STACKTRACE']
you will get output into the stacktrace.log.
Alternatively, rename the StackTrace
logger to grails.app.init.Bootstrap
(thanks to @JeffScottBrown for this line):
logger 'grails.app.init.BootStrap', ERROR, ['FULL_STACKTRACE'], false
and you will get output into the stacktrace.log
This observation leads me to believe that the StackTrace
logger doesn't do anything. I further am led to believe that any logger not named for a package doesn't work.
As a result of all this, my question is:
- Does logback work for non-package/class named loggers?
- If so, why does the
StackTrace
logger in the defaultlogback.groovy
not result in output to stacktrace.log?
EDIT:
- The main issue for me is that the
StackTrace
logger seems completely unnecessary, so why is it included in the default file?
Second Edit:
Another way to confirm this is to make only the StackTrace
logger write to the STDOUT
appender, and watch stacktraces disappear from the console when you throw an exception:
logger("StackTrace", ERROR, ['STDOUT','FULL_STACKTRACE'], false)
root(ERROR, [])
Your log statement in Bootstrap.groovy:
log.error 'this is a new error'
logs a regular ERROR level message to the logger named grails.app.init.yourapp.BootStrap
. It does not log to the logger named StackTrace
.
The FULL_STACKTRACE
appender is for unfiltered stacktraces. These are written by the framework to the logger StackTrace
.
If you replace
log.error 'this is a new error'
with
throw new RuntimeException("foo")
you will see the full stacktrace being logged to stacktrace.log
这篇关于为什么stacktrace.log不会在grails 3中填充logback?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!