views:

245

answers:

6

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:

  1. 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").

  2. 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?

  3. 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?

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.

A: 
  1. The 'logger' will be another object, not a method. This logger class will have methods defined on it like public void severe(String message)

  2. '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 like Car.StartEngine() (see the law of demeter for more info)

  3. 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.

Kirschstein
A: 

Does your class inherit from a class that defines logger?

A: 

logger is not a method but a class variable which seems to be an object that exposes the methods "severe", "warning" and "info".

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

dbemerlin
A: 
  1. The logger usually refers to the usage of a class in log4j.
  2. The logger is a member object whose function e.g. severe is called.
  3. The logger usually logs into a file (this can be configured through log4j.xml or some other config file or during the program start).

Edit: Changed the log4j link.

Amit Kumar
+3  A: 

The logger doesn't make anything special. It's all just Java code.

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.

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.

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.

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.

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.

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.

Update 2: as per the another confusion:

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 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.

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.
    }
}
BalusC
So, it is like that? If I see something like "aaa.bbb.ccc()" I can say that ccc() is a method of "aaa.bbb" and, as a consequence, "aaa.bbb" is an object. So, in the class "aaa" I should not search for the method called "bbb". Instead, I need to search for a field "bbb" defined in the class "aaa".
Roman
It's indeed like that.
BalusC
A: 

Try researching the Java logging API. Try the tutorials here and here

Also check out the Java docs for the Logger class

froadie