Java中的“记录器”是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2411022/
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
What is "logger" in Java?
提问by Roman
I have a class in which I see the following things:
我有一堂课,我看到以下内容:
this.logger.severe("");
this.logger.warning("");
this.logger.info("");
I do not understand several things:
我不明白几件事:
How can we use a method that was not defined earlier? I mean, there are no "logger" methods defined in the class. I thought that these methods could be defined because the considered class is an extension of another class in which the "logger" is defined. But in the definition of the class there no "extends" statement (only "implements").
I can understand things like that: "objectName.methodName". But what is that "objectName.something1.something2"? "something1.something2" is name of a method? Can method names contain dots?
What exactly these "logger.*" do? I think they save information about the execution of the code. They kind of write report about what happened during the execution. But where I can find this information?
我们如何使用之前未定义的方法?我的意思是,类中没有定义“记录器”方法。我认为可以定义这些方法,因为所考虑的类是定义了“记录器”的另一个类的扩展。但是在类的定义中没有“扩展”语句(只有“实现”)。
我可以理解这样的事情:“objectName.methodName”。但什么是“objectName.something1.something2”?“something1.something2”是方法的名称?方法名称可以包含点吗?
这些“logger.*”究竟是做什么的?我认为它们保存了有关代码执行的信息。他们有点写关于执行过程中发生的事情的报告。但是我在哪里可以找到这些信息?
ADDED:
添加:
In the beginning of the file I have: import java.util.logging.Logger;
And then in the class I have: private Logger logger = Logger.getLogger("a.b.c.d")
;
So, logger is an object of the class Logger (but I do not understand why they could not instantiate the class in a usual way using "new Logger()). I also do not understand what exactly logger.severe("") do.
在文件的开头我有:import java.util.logging.Logger;
然后在课堂上我有:private Logger logger = Logger.getLogger("a.b.c.d")
;
所以,logger 是 Logger 类的一个对象(但我不明白为什么他们不能使用“new Logger()”以通常的方式实例化这个类。我也不明白 logger.severe("") 到底做了什么.
回答by Kirschstein
The 'logger' will be another object, not a method. This logger class will have methods defined on it like
public void severe(String message)
'something1' will be an object contained by 'objectName'. For example,
Car.Engine.Cylinder.Fire()
, it's considered bad practise to use a method to fire a car's cylinders like this, and you should do something more likeCar.StartEngine()
(see the law of demeterfor more info)The logger will keep a record of what's happened in your program, so if there's a crash or a bug later on, you can see what happened. Whether this is recorded to a text file, or to a database somewhere, will be down to the implementation of your logger.
“记录器”将是另一个对象,而不是方法。这个记录器类将在其上定义方法,如
public void severe(String message)
'something1' 将是一个包含在 'objectName' 中的对象。例如,
Car.Engine.Cylinder.Fire()
使用这样的方法来点燃汽车的气缸被认为是不好的做法,您应该做更多类似的事情Car.StartEngine()
(有关更多信息,请参阅demeter 定律)记录器会记录你的程序发生了什么,所以如果以后出现崩溃或错误,你可以看到发生了什么。这是记录到文本文件,还是记录到某处的数据库,将取决于您的记录器的实现。
回答by Morfildur
logger is not a method but a class variable which seems to be an object that exposes the methods "severe", "warning" and "info".
logger 不是一个方法,而是一个类变量,它似乎是一个暴露方法“严重”、“警告”和“信息”的对象。
Check your class for something like "someClass logger = new someClass();"
检查您的课程是否有“someClass logger = new someClass();”之类的东西
回答by amit
- The logger usually refers to the usage of a class in log4j.
- The logger is a member object whose function e.g. severe is called.
- The logger usually logs into a file (this can be configured through log4j.xml or some other config file or during the program start).
- logger 通常指的是log4j中某个类的使用。
- 记录器是一个成员对象,它的函数例如severe 被调用。
- 记录器通常会登录到一个文件中(这可以通过 log4j.xml 或其他一些配置文件或在程序启动期间进行配置)。
Edit:Changed the log4j link.
编辑:更改了 log4j 链接。
回答by BalusC
The logger doesn't make anything special. It's all just Java code.
记录器没有做任何特别的事情。这只是Java代码。
public class SomeClass {
private Logger logger = LogFactory.getLogger(SomeClass.class);
public void doSomething() {
this.logger.debug("foo");
}
}
The this.logger
just points to the instance variable named logger
of the current instance (this
). The this.
prefix is by the way superflous in this example. One could also just do logger.debug("foo")
here.
在this.logger
刚刚指向名为实例变量logger
的当前实例的(this
)。this.
顺便说一下,前缀在这个例子中是多余的。一个人也可以在logger.debug("foo")
这里做。
If it is not declared in the SomeClass
itself, then it's likely been declared in the extending class. Check the class which is declared in extends
.
如果它SomeClass
本身没有声明,那么它很可能是在扩展类中声明的。检查中声明的类extends
。
As to your objectName.something1.something2
doubt, have you already looked how System.out.println()
works? The System.out
returns a PrintStream
object which in turn has a println()
method. Thus, if objectName.something
returns a fullworthy Object
with methods, then you can just continue chaining method calls.
至于您的objectName.something1.something2
疑问,您是否已经看过如何System.out.println()
工作?所述System.out
返回一个PrintStream
对象,该对象反过来又一个println()
方法。因此,如果objectName.something
返回一个完整Object
的方法,那么您可以继续链接方法调用。
Basically,
基本上,
objectName.something1.something2;
can be translated as
可以翻译为
SomeObject someObject = objectName.something1;
someObject.something2;
But if you don't need someObject
anywhere else in the code, then it can just be shortened as in your example.
但是,如果您不需要someObject
代码中的任何其他地方,那么可以像您的示例一样缩短它。
Update: as per your update:
更新:根据您的更新:
So, logger is an object of the class Logger (but I do not understand why they could not instantiate the class in a usual way using "new Logger()). I also do not understand what exactly logger.severe("") do.
所以,logger 是 Logger 类的一个对象(但我不明白为什么他们不能使用“new Logger()”以通常的方式实例化这个类。我也不明白 logger.severe("") 到底做了什么.
Just read the javadoc of the class in question what it all does. As to why it can't be instantiated, it's because of the factory pattern.
只需阅读相关类的 javadoc 它都做了什么。至于为什么不能实例化,是工厂模式的原因。
Update 2: as per the another confusion:
更新 2:根据另一个困惑:
I do not understand why they use "this". I mean, if I use just field name, will it not be, by default, the field of this object? I there any difference between "this.x" and "x"?
我不明白他们为什么使用“这个”。我的意思是,如果我只使用字段名称,默认情况下它不会是这个对象的字段吗?“this.x”和“x”之间有什么区别吗?
This way you can be more explicit about which one you'd like to access. If the method contains for example an argument or a local variable with the name logger
, then this.logger
would still refer to the instance variable.
通过这种方式,您可以更明确地了解要访问的内容。如果该方法包含例如参数或名称为 的局部变量logger
,则this.logger
仍将引用实例变量。
public class SomeClass {
private Logger logger = LogFactory.getLogger(SomeClass.class);
public void doSomething(Logger logger) {
this.logger.debug("foo"); // Refers to the instance variable.
logger.debug("foo"); // Refers to the method argument.
}
public void doSomethingElse() {
Logger logger = LogFactory.getLogger(SomeClass.class);
this.logger.debug("foo"); // Refers to the instance variable.
logger.debug("foo"); // Refers to the method local variable.
}
}
回答by BalusC
The java.util.Logger class is the main access point to the Java logging API. Here is how you create a logger:
java.util.Logger 类是 Java 日志 API 的主要访问点。以下是创建记录器的方法:
Logger logger = Logger.getLogger("myLogger");
The string passed as parameter to the getLogger() factory method is the name of the Logger to create. You can choose the name freely, but the name implies where the Logger is located in the Logger hierarchy. Every . (dot) in the name is interpreted as a branch in the hierarchy.
作为参数传递给 getLogger() 工厂方法的字符串是要创建的 Logger 的名称。您可以自由选择名称,但名称暗示了 Logger 在 Logger 层次结构中的位置。每一个 。名称中的 (dot) 被解释为层次结构中的一个分支。