是每个类的记录器还是由整个应用程序访问的一组记录器? [英] Is a logger per class or is a set of loggers that are accessed by the entire application perferred?

查看:86
本文介绍了是每个类的记录器还是由整个应用程序访问的一组记录器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Java项目,我创建了七个记录器,可以从程序的每个点通过外观访问。但是在互联网上,我看到很多例子都是每个班级都有一个记录器。

I have a project in Java, and I create seven loggers that are accessed by a facade from every point of the program. But in the internet, I see a lot of examples witha a logger in each class.

最值得推荐的记录方式是什么?

What is the most recommended way to do logging?

推荐答案

每个类中的记录器更好,更容易扩展。
原因是在一个类中定义一个记录器可以轻松地将实际记录api与记录器的配置(格式,持久性)分开。
我使用过多个大而复杂的java软件(> 100万行代码),每个类都使用一个记录器。

a logger in each class is better and more easy to extend. the reason is that define one logger in one class separate the real logging api from the logger's configuration (format, persistence) easily. I have worked with more than one big and complex java software (> 1 million lines of code), they all use one logger per class.

另外,定义每个类一个记录器并不意味着每个类使用不同的记录器实例。

also, define "one logger per class" doesn't mean each class use a different logger instance.

class Foo {
    private static final Logger log = Logger.getLogger( Foo.class );
}


class Bar {
    private static final Logger log = Logger.getLogger( Bar.class );
}

上面使用的两个记录器是否使用相同的记录器实例,如果不确定上面的代码。通常,有一个配置位置(在属性文件或程序中)指定Foo和Bar中的logger是否共享记录器。

Whether the two logger used above using the same logger instance if not sure from the above code. Normally, there is a configuration place (in a property file or in program) that specify whether logger in Foo and Bar shares a logger.

所以每个类中的记录器只是在每个类中定义一个记录器,但是这样的记录器可能从不同的类引用相同的记录器实例

So "a logger in each class" is just define one logger in every class, But such logger may referred to the same logger instance from different classes

这篇关于是每个类的记录器还是由整个应用程序访问的一组记录器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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