简体   繁体   English

Java Logging Customs 的动机

[英]Motivation for Java Logging Customs

I've been reading up on logging in Java and it seems like SLF4J is the way to go.我一直在阅读有关登录 Java 的信息,似乎 SLF4J 是要走的路。

What I don't understand is why the logging object is obtained/initialized the way it is.我不明白的是为什么日志对象是按原样获取/初始化的。

Why does为什么

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

need to be in every class?需要在每个班级?

  1. Why not instead have just some static logger instance for all classes/class-instances?为什么不为所有类/类实例只使用一些静态记录器实例? (What happens if I don't create new instances for every class?) (如果我不为每个类创建新实例会发生什么?)

  2. Why do I need to pass the class object of the class the logger is being used in to the factory?为什么我需要将使用记录器的类的类对象传递给工厂? Won't the logger have all of these details during runtime anyway?无论如何,记录器在运行时不会拥有所有这些细节吗? (What happens if I pass some different class object?) (如果我传递一些不同的类对象会发生什么?)

  1. You can actually create a singleton logger that can be used anywhere.您实际上可以创建一个可以在任何地方使用的单例记录器。 But we usually create a logger for each class just to filter logs.但是我们通常为每个类创建一个记录器只是为了过滤日志。
  2. this is just for naming the logger instance you have created, actually you can pass any string you want and nothing will happen ex: Logger logger = LoggerFactory.getLogger("any name");这只是为了命名您创建的记录器实例,实际上您可以传递您想要的任何字符串,并且不会发生任何事情,例如: Logger logger = LoggerFactory.getLogger("any name");

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

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