java 如何在代码中使用 log4j?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4134306/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 04:56:15  来源:igfitidea点击:

How to use log4j in code?

javasingletonlog4j

提问by sushil bharwani

We have to introduce logging in our application. We have decided to use log4j is it a good choice. Also somebody told that we should use singleton pattern for using log4j can somebody emphasise how and why?

我们必须在我们的应用程序中引入日志记录。我们决定使用 log4j 是不是一个不错的选择。还有人告诉我们应该使用单例模式来使用 log4j 有人可以强调如何以及为什么吗?

回答by Adam Paynter

In the singleton pattern, the Loggerfield is static to the class:

在单例模式中,该Logger字段对于类来说是静态的:

public class SomeAwesomeClass {

    private static final Logger logger = Logger.getLogger(SomeAwesomeClass.class);

}

The alternative is to make the Loggerfield non-static (that is, each instance of SomeAwesomeClassholds a reference to the Logger):

另一种方法是使Logger字段非静态(即,每个 的实例都SomeAwesomeClass包含对 的引用Logger):

public class SomeAwesomeClass {

    private final Logger logger = Logger.getLogger(SomeAwesomeClass.class);

}

I don't think it makes a lot of difference which route you choose as I believe Log4j will continually return the same Loggerinstance for each instance of SomeAwesomeClass. Really, it's just unnecessary object references, so you might as well use the singleton pattern.

我认为您选择哪条路线没有太大区别,因为我相信 Log4j 会不断LoggerSomeAwesomeClass. 真的,这只是不必要的对象引用,所以你不妨使用单例模式。

However, the non-singleton pattern becomes necessary if you do something like this:

但是,如果您执行以下操作,则非单例模式变得必要:

public class SomeAwesomeClass {

    private final Logger logger = Logger.getLogger(getClass());

    public void doSomething() {
        log.info("Doing something");
    }

}

public class AwesomerClass extends SomeAwesomeClass {
}

Later...

之后...

SomeAwesomeClass o = new AwesomerClass();
o.doSomething();

In this example, the loggerfield will correspond to AwesomerClass, not SomeAwesomeClass.

在此示例中,该logger字段将对应于AwesomerClass,而不是SomeAwesomeClass

回答by Aravind Yarram

If you are introducing logging for the first time then use Logback. Use the configuration file as suggested hereand add it to the classpath. Logging will be auto configured.

如果您是第一次引入日志记录,请使用 Logback。使用此处建议的配置文件 并将其添加到类路径中。日志记录将自动配置。

回答by Buhake Sindi

The reason why Singleton is sometimes preferable is that we want only one instance of the Logger per application, otherwise, for every new instance of the logger, there will be a newly created file or an overwrite of an existing log file.

Singleton 有时更可取的原因是我们只希望每个应用程序有一个 Logger 实例,否则,对于记录器的每个新实例,都会有一个新创建的文件或对现有日志文件的覆盖。

As to whether Log4J is a good choice or not will depend on your research/experience with the different loggers available. It's all about your preference (I use Log4J all the time as I find it easier to configure, but I'm not biased to any loggers out there).

至于 Log4J 是否是一个不错的选择,将取决于您对可用的不同记录器的研究/经验。这完全取决于您的偏好(我一直使用 Log4J,因为我发现它更易于配置,但我不偏向于那里的任何记录器)。

Famous ones out there are:

著名的有:

  • Log4J
  • SLF4J
  • LogBack
  • Java's own Logging....
  • 日志4J
  • SLF4J
  • 登录
  • Java 自己的日志记录....

There are some examples on the web that shows you how to use loggers. Hereis a brief comparison for Log4J vs SLF4J.

网络上有一些示例向您展示了如何使用记录器。是 Log4J 与 SLF4J 的简要比较。