I have seen log and logger as follows,
public static final Log logger = LogFactory.getLog(this.class)
logger.debug something
private final Log log = LogFactory.getLog(this.class)
log.debug(something)
Are there any difference between log and logger?
Note: They are both from apache.commons.
The fundamental difference is not in the names "log" and "logger" (they are different names for a variable of the same type and initialized in the same way, but in this:
public static final Log logger;
private final Log log;
Being "final", which both are, they cannot be modified once initialized. However, "private" means that the variable is only accessible within the class. "public" means that it is accessible to all classes that imported or are within the package. When an object is "static", like logger, there is one value for the entire program; when it is not, like log, it is instantiated with each instance of the object.
Related
Consider and example as below:
public class LoggerTestClass {
private static final Logger LOGGER = Logger.getLogger(LoggerTestClass.class);
}
In above example parameter passed to getLogger method is hard coded i.e LoggerTestClass.class
Now every time I change the class name I have to change the getLogger method's parameter.
Any other way which will fetch the class name automatically, for example for non static variable we can add this.getClass()
You can use Lombok to achive it in a clean fashion. All you need to do is to put #Log on top of your class and it will give you a log object to work with.
Read more here
Bro,For the question,first of all,we need make clear some concept.
In java,if a identifier is a member of a class,and decorated by 'static' keyword,it's value is Decided in the compile phase.
If the 'final' keyword is added,the identifier is constant,it's value must have a initial value on it is declared.and the value can't be change in the java runtime.
And the interesting thing is, the static constent is only declared inside the class,it can be called 'Classname' + '.' ,but has no connection to the class context.if there's no context ,the identifier con't become dynamic.
I think if let the identifier become dynamic ,there are only two ways:
let the identifier become a variable.remove the 'final' key word.the code look like this:
class Test {
private static Logger LOGGER;
public Test() {
LOGGER = Logger.getLogger(getClass().getSimpleName());
}
}
The 'private' keyword is sufficient if you don't want other classes to access it.
Try the java dynamic proxy. it can change the class in java runtime.
Code happy ~
Welcome to talk with me~
Can you use java 9+? Then solution is pretty simple:
private static final Logger LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass());
As MethodHandles.lookup() creates lookup using caller class. (via Reflection.getCallerClass() but Reflection is internal API, so you need that additional method handle lookup step)
is it possible to determine the class of an implementing class in a static context. Given i want to create a logger in the superclass which logs its messages under the class name of the implementing classes.
Currently i'm trying to use this approach:
public abstract class GenericDao<T, ID extends Serializable>
{
protected static Logger logger = Logger.getLogger(new Object() { }.getClass().getEnclosingClass());
...
}
But i still get the superclass. Is there any way to do this or should i give up and include the class in the actual logging method when i have access to "this".
Static variables and methods always belong to the declaring class, GenericDao in this case. If you create a new subclass SpecificDao then it is not accessing a new copy of logger if it accesses it, it is accessing the exact same logger as is in GenericDao.
You will need to create the Logger as a non-static member and have one per object (potentially very inefficient) , or declare the logger in each class.
What should work to give you what you want though is if you have an abstract method getLogger() in GenericDao and no Logger at all..
Whenever it wants to log GenericDao calls getLogger().log
The implementing classes then define their own private static Logger logger. They override the getLogger() abstract method and return the static one from that.
Log4j pattern layout docs warns that generating caller class information is costly and should be used only when performance is not a concern. I want to have the class name in logs for easy analysis and also doesn't want to have an impact in performance. Having stated that I have two options,
Store class name using getClass().getName() in every class and use that string in logging
String class = getClass().getName();
...
...
logger.debug(class + "log message");
Hardcode the class name and give it in log
String class = "com.foo.myclass";
...
...
logger.debug(class + "log message");
Which would give better performance or is there any other way to log fully qualified class name and method name without affecting performance. Thanks in Advance!
Note: I am using log4j 1.2.16
Generating the caller class name is costly but if you're following the typical Log4J design pattern of one Logger per class with the Logger name being the same as the class name
private static final Logger LOG = Logger.getLogger(ThisClassName.class);
then it may be sufficient to simply use %c instead of %C in your layout pattern. This uses the logger name (which is a property of the logger itself) rather than inspecting the call stack, and does not involve the overhead incurred with patterns like %C or %L.
If you really do need the runtime class of the caller rather than the static name of the class that textually encloses a particular logging call, then you could use a per-instance logger rather than (or as well as) the per-class one
private final Logger instLog = Logger.getLogger(this.getClass());
and now the logger name will be the runtime type of this, i.e. if B extends A and we call a method on an instance of B then the logger name will be B, even for methods that are actually defined by the superclass A.
A usual pattern for loging with log4j is the following
public class Foo {
private static final Logger LOG = Logger.getLogger(Foo.class);
someMethod(){
...
if(LOG.isDebugEnabled()){
LOG.debug("log message");
}
}
}
The classname is the logger name so it will (by default) appears in the log message.
Most importantly regarding performance: there is a guard preventing the overhead of the computation of the log message when the log level is not enabled.
I have this parent abstract class which defines an Apache logger static object. Something like this:
import org.apache.log4j.Logger;
public abstract class A {
private final static Logger logger;
(...)
}
I know this code is illegal because the logger object is not initialized. The problem is I don't want to initialize it with logger = Logger.getLogger(A.class); because I want each child class to initialize it with its own class object, that way I will know which class caused which errors.
But at the same time I want to include some of my logging methods on the base class A.
What would be the best practice for this? Should I initialize it with A.class, then reinstantiate it for each child class? Somehow that feels incorrect to me.
Initialize it with the actual class it's created in:
logger = Logger.getLogger(getClass()); //log4j way of creating loggers
To do this you'll need to remove static modifier from your logger declaration.
I prefer to keep it private and access it via a getter-method, but you can also make it protected and access directly from A subclasses.
You should not worry that many logger objects will be created, one logger per class instance: under the hood Logger contains a map of loggers, and every time you create a new logger - it's being cached. When you try to get logger for the same class the second time - it's just being retrieved from the inner map.
So, if you have 5 classes in your hierarchy - only 5 Logger objects will be created, no matter how many times you call getLogger(getClass()).
Should I initialize it with A.class, then reinstantiate it for each
child object?
You should reinstantiate it for each child class.
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?
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.
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.
}
}
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).
Edit: Changed the log4j link.
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 like Car.StartEngine() (see the law of demeter for 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.
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();"
The java.util.Logger class is the main access point to the Java logging API. Here is how you create a logger:
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.