What is a "logger" in Java?

I have a class in which I see the following things:

this.logger.severe(""); this.logger.warning(""); this.logger.info(""); 

I do not understand a few things:

  • How can we use a method that has not been defined previously? I mean, the logger methods are not defined in the class. I thought that these methods can be defined because the class in question is an extension of another class in which the “registrar” is defined. But in the class definition there is no expression "extends" (only "implements").

  • I can understand things like this: "objectName.methodName". But what is "objectName.something1.something2"? "something1.something2" is the name of the method? Can method names contain periods?

  • What exactly do these “registrars" do? I think they save code execution information. They see a report of what happened during execution. But where can I find this information?

ADDED:

At the beginning of the file I have: import java.util.logging.Logger;
And then in the class I have: private Logger logger = Logger.getLogger("abcd") ;
Thus, logger is an object of the Logger class (but I don’t understand why they could not instantiate the class in the usual way using the “new Logger ()). I also don’t understand what logger.severe (" ") is.

+4
source share
5 answers

The registrar does nothing special. This is just Java code.

 public class SomeClass { private Logger logger = LogFactory.getLogger(SomeClass.class); public void doSomething() { this.logger.debug("foo"); } } 

this.logger simply points to an instance variable called logger current instance ( this ). Prefix this. Incidentally, is supernatural in this example. You can also just do logger.debug("foo") here.

If it is not declared in SomeClass itself, it most likely was declared in an expanding class. Check out the class declared in extends .

As for your doubt about objectName.something1.something2 , have you already seen how System.out.println() works? System.out returns a PrintStream object, which in turn has println() . That way, if objectName.something returns a fully functional Object method using methods, you can simply continue calling chain methods.

Basically,

 objectName.something1.something2; 

can translate as

 SomeObject someObject = objectName.something1; someObject.something2; 

But if you do not need someObject anywhere else in the code, then you can simply shorten it, as in your example.

Update : according to your update:

So, logger is an object of the Logger class (but I don’t understand why they could not instantiate the class in the usual way using the “new Logger ()). I also don’t understand what exactly logger.severe (" ") is.

Just read the javadoc of this class that all this does . As to why this is not possible to create, this is because of the factory pattern.

Update 2 : according to another confusion:

I do not understand why they use "this". I mean, if I use only the field name, will it not be the default field of this object? Am I there any difference between "this.x" and "x"?

In this way, you can more clearly indicate which one you want to access. If the method contains, for example, an argument or a local variable called logger , then this.logger will still refer to the instance variable.

 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. } } 
+9
source
  • The logger will be a different object, not a method. This logger class will have methods defined on it as public void severe(String message)

  • "something1" will be the object contained in "objectName". For example, Car.Engine.Cylinder.Fire() , it is considered improper practice to use a method to start such car cylinders, and you should do something more like Car.StartEngine() (see demeter law for more information)

  • The registrar will keep a record of what happened in your program, so if after that a crash or error occurs, you will see what happened. Regardless of whether it is written in a text file or somewhere in the database, work will be performed with your registrar.

+2
source

logger is not a method, but a class variable, which is represented by an object that provides the methods serious, warning, and info.

Check your class for something like "someClass logger = new someClass ();"

0
source
  • The logger usually refers to the use of the class in log4j .
  • A logger is a member object whose function, for example, is called cruel.
  • The logger is usually registered in the file (this can be configured using log4j.xml or another configuration file or during program startup).

Edit: Changed the log4j link.

0
source

The java.util.Logger class is the primary access point to the Java Registration API. This is how you create a registrar:

 Logger logger = Logger.getLogger("myLogger"); 

The string passed as a parameter to the getLogger () factory method is the name of the generated Logger. You can freely choose a name, but the name implies where Logger is located in the Logger hierarchy. Each. (dot) in the name is interpreted as a branch in the hierarchy.

0
source

Source: https://habr.com/ru/post/1303577/


All Articles