独立配置两个logback追加器 [英] Configuring two logback appenders independently
问题描述
我可能会问一些小事,但我试过的似乎并不工作。使用我的MAINappender,除了第三方包(我们称之为无聊
),我想要记录所有的info他们(所以我只看警告)。此外,我想在我的有趣的
包中记录调试。这与以下 logback.groovy
的工作正常:
root ,[MAIN])
logger(interesting,DEBUG,[MAIN])
logger(boring,WARN,[MAIN])
现在我想配置一个不同的appender,一个级别更像
root(DEBUG,[DETAIL])
logger(interesting,TRACE,[DETAIL])
logger(boring,INFO, DETAIL])
这也可以,但是当我把它们放在一起时,我可以想象这是由每个
Logger
对于给定级别打开或关闭的事实造成的。我知道对于我想要的行为,无聊包中的日志记录器必须在INFO级别(因为DETAIL
appender),并且消息MAIN
appender将被过滤,但我看不到如何配置此。
UPDATE < h3>
我看到我做的几乎一切都错了。
logger(interesting,DEBUG,[MAIN])
没有像将级别设置为DEBUG用于主要追加器和包
有趣
下面),它做两个独立的事情:
- 将包的级别设置为DEBUG
code>和
- 添加MAIN appender到有趣
Logger
/ ul>
解决方案这样似乎是不可能的,没有写一个自己的过滤器,这是幸运地很容易。我最终得到了类似
root(DEBUG,[DETAIL,MAIN])
//设置最详细的appender
logger(interesting,TRACE)
logger(boring,INFO)
appender(MAIN,...){
...
filter = new MyFilter()
.deny(boring,INFO)
.accept(interesting,DEBUG)
。 ,DEBUG)
}
其中
deny
和accept
是我的方法添加条目到MyFilter
。这些条目是顺序评估的,即
- 在包中
无聊
在有趣的
及以下版本中,INFO
或以下会被拒绝 - 凡是级别
DEBUG
或以上的任何东西都会被接受 - 否则,在任何包中,级别
DEBUG
或以上被拒绝
受到此问题。
I might be asking something trivial, but what I've tried doesn't seem to work. With my "MAIN" appender, I want to log all "info"s everywhere, except in a third-party package (let's call it
boring
), which produces too many of them (so I look at warnings only). Additionally, I want to log "debug"s in myinteresting
package. This works fine with the followinglogback.groovy
:root(INFO, ["MAIN"]) logger("interesting", DEBUG, ["MAIN"]) logger("boring", WARN, ["MAIN"])
Now I want to configure a different appender logging one level more like
root(DEBUG, ["DETAIL"]) logger("interesting", TRACE, ["DETAIL"]) logger("boring", INFO, ["DETAIL"])
This also works, but when I put both together, it doesn't. I can imagine that this is caused by the fact that each
Logger
is either on or off for a given level. I'm aware that for the behavior I want, the loggers in the "boring" package must be on the INFO level (because of theDETAIL
appender) and that the messages for theMAIN
appender are to be filtered, but I can't see how to configure this.UPDATE
I see I was doing nearly everything wrong. The line
logger("interesting", DEBUG, ["MAIN"])
says nothing like "set the level to DEBUG for the MAIN appender and the package
interesting
and below"), it does two independent things instead:- set the level to DEBUG for the package
interesting
and below - add the MAIN appender to the "interesting"
Logger
解决方案This seems to be impossible without writing an own filter, which is fortunately pretty easy. I ended up with something like
root(DEBUG, ["DETAIL", "MAIN"]) // settings for the most detailed appender logger("interesting", TRACE) logger("boring", INFO) appender("MAIN", ...) { ... filter = new MyFilter() .deny("boring", INFO) .accept("interesting", DEBUG) .deny("", DEBUG) }
where
deny
andaccept
are my methods adding entries toMyFilter
. The entries are evaluated sequentially, i.e.,- in package
boring
and below, everything with levelINFO
or below gets denied - in package
interesting
and below, everything with levelDEBUG
or above gets accepted - otherwise, in any package, everything with level
DEBUG
or above gets denied
Inspired by this question.
这篇关于独立配置两个logback追加器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 在包中