将INFO和ERROR日志与java.util.logging分开 [英] Separate INFO and ERROR logs from java.util.logging

查看:76
本文介绍了将INFO和ERROR日志与java.util.logging分开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为Java应用程序配置日志记录.我的目标是创建两个日志:一个用于所有消息,一个仅用于特定级别以上的消息.

I'm configuring the logging for a Java application. What I'm aiming for is two logs: one for all messages and one for just messages above a certain level.

该应用程序使用java.util.logging.*类:我按原样使用它,所以我仅限于通过logging.properties文件进行配置.

The app uses the java.util.logging.* classes: I'm using it as is, so I'm limited to configuration through a logging.properties file.

我看不到一种不同地配置两个FileHandler的方法:我见过的文档和示例设置了如下属性:

I don't see a way to configure two FileHandlers differently: the docs and examples I've seen set properties like:

java.util.logging.FileHandler.level = INFO

我希望两个不同的处理程序以不同的级别记录到不同的文件中.

While I want two different Handlers logging at different levels to different files.

有什么建议吗?

推荐答案

http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html 很有帮助.您只能为任何单独的记录器设置一个级别(正如您可以从记录器上的setLevel()方法看出的那样).但是,您可以选择两个常见级别中的最低级别,然后以编程方式进行过滤.

http://java.sun.com/j2se/1.4.2/docs/guide/util/logging/overview.html is helpful. You can only set one Level for any individual logger (as you can tell from the setLevel() method on the logger). However, you can take the lowest of the two common levels, and then filter programmatically.

不幸的是,您不能仅使用配置文件来执行此操作.要仅使用配置文件进行切换,您将必须切换至 log4j 之类的文件,您说过这不是一个选择.

Unfortunately, you can't do this just with the configuration file. To switch with just the configuration file you would have to switch to something like log4j, which you've said isn't an option.

所以我建议使用以下过滤器来更改登录代码:

So I would suggest altering the logging in code, with Filters, with something like this:

class LevelFilter implements Filter {
    private Level Level;
    public LevelFilter(Level level) {
        this.level = level;
    }
    public boolean isLoggable(LogRecord record) {
        return level.intValue() < record.getLevel().intValue();
    }
}

然后在第二个处理程序上,执行setFilter(new LevelFilter(Level.INFO))或其他操作.如果您希望文件可配置,则可以使用自己编写的日志记录属性设置,并使用常规的Properties方法.

And then on the second handler, do setFilter(new LevelFilter(Level.INFO)) or whatever. If you want it file configurable you could use a logging properties setting you've made up yourself, and use the normal Properties methods.

我认为,一旦设计完成,用于设置两个文件处理程序和程序代码的配置代码就相当简单,但是如果您想获得更多详细信息,请添加注释,然后我会进行编辑.

I think the configuration code for setting up the two file handlers ad the programmatic code is fairly simple once you have the design, but if you want more detail add a comment and I'll edit.

这篇关于将INFO和ERROR日志与java.util.logging分开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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