简体   繁体   English

如何将java.util.logging.Logger覆盖到我的私有记录器中?

[英]How to override java.util.logging.Logger to my private logger?

I have code fetched from jar that uses java.util.logging.Logger . 我从使用java.util.logging.Logger jar中获取了代码。

Jar contains about 1000 logger usages and each class start from: Jar包含约1000种记录器用法,每个类从以下位置开始:

private static final Logger LOG = Logger.getLogger(SomeClass.class.getName());

I want to handle all logs there, means, to point them to my Logger usage and not to java.util.logging.Logger . 我想在那里处理所有日志,也就是说,将它们指向我的Logger用法,而不是java.util.logging.Logger

Therefore I wrote my own logger. 因此,我编写了自己的记录器。

So instead: 所以与其:

LOG.log(Level.SEVERE, "Error sleeping", e);

I can write: 我可以写:

 MyLogger.toLog(TLogLevel.WFS_ERROR, "Monkey", "Error sleeping", e );

The problem is I need run over all java files and replace with mine. 问题是我需要运行所有Java文件并用我的替换。

Messy way, hmm 凌乱的方式,嗯

Does anyone know how can by easy way to convert java.util.logging.Logger to com.boo.MyLogger ? 有谁知道如何通过简单的方法将java.util.logging.Logger转换为com.boo.MyLogger

Thanks, 谢谢,

The SLF4J project has a jul-to-slf4j bridge that can be used to redirect java.util.logging.Logger calls to SLF4J. SLF4J项目具有一个jul-slf4j桥 ,可用于将java.util.logging.Logger调用重定向到SLF4J。 You could use that (by making your MyLogger implement the interface defined by SLF4J). 您可以使用它(通过使MyLogger实现SLF4J定义的接口)。

Note that, however, unlike all other logging libraries, jul is hard-wired into the Java class libraries and cannot be bridged without a performance penalty. 但是请注意,与所有其他日志记录库不同,jul被硬连接到Java类库中,并且不能在不影响性能的情况下进行桥接。

Also, I don't know what you are doing with MyLogger, but usually there is no need to write your own. 另外,我不知道您在使用MyLogger做什么,但是通常不需要编写自己的代码。 There are plenty of logging implementations to choose from, and they can be configured in many different ways. 有许多日志记录实现可供选择,并且可以用许多不同的方式进行配置。 And even if you do have to write your own Logger implementation , you should use an existing interface (such as SLF4J which seems to most popular these days). 即使必须编写自己的Logger 实现 ,也应该使用现有接口 (例如SLF4J,这在当今看来很流行)。

Take a look at SLF4J : 看看SLF4J

The Simple Logging Facade for Java or (SLF4J) serves as a simple facade or abstraction for various logging frameworks, eg java.util.logging, log4j and logback, allowing the end user to plug in the desired logging framework at deployment time. Java或(SLF4J)的简单日志记录外观可作为各种日志记录框架(例如java.util.logging,log4j和logback)的简单外观或抽象,从而允许最终用户在部署时插入所需的日志记录框架。

Using that you could then also use logback (same author) to log to a common logging framework using the various bridges already available. 然后,您还可以使用logback (同一作者)使用已经可用的各种桥来登录到通用日志记录框架。 Or, write your own, but either way you would not have to worry about replacing all that code... 或者,编写自己的代码,但是无论哪种方式,您都不必担心替换所有代码...

Oracle's Java 7 Logger is configurable, its implementation is simply: Oracle的Java 7 Logger是可配置的,其实现很简单:

public static Logger getLogger(String name) {
    // This method is intentionally not a wrapper around a call
    // to getLogger(name, resourceBundleName). If it were then
    // this sequence:
    //
    //     getLogger("Foo", "resourceBundleForFoo");
    //     getLogger("Foo");
    //
    // would throw an IllegalArgumentException in the second call
    // because the wrapper would result in an attempt to replace
    // the existing "resourceBundleForFoo" with null.
    LogManager manager = LogManager.getLogManager();
    return manager.demandLogger(name);
}

So you can also via code set a logging level; 因此,您还可以通过代码设置日志记录级别; besides declarative. 除了声明性的。

LogManager.getLogManager().getLogger(Logger.GLOBAL_LOGGER_NAME).setLevel(Level.INFO);

Lars Vogel has a nice page, also with its own Logger class. Lars Vogel有一个不错的页面,也有自己的Logger类。

All put together is quite workable, but maybe sometimes somewhat hard to understand. 全部放在一起都是可行的,但有时可能有些难以理解。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM