Why log4j's Logger.getLogger() need pass a Class type?

log4j tutorial
log4j properties
log4j configuration
log4j documentation
what is log4j in selenium
log4j maven
log4j rootcategory
what is log4j file

I read some articles about how to use log4j. Most of them give below code as a beginning:

 Logger logger = Logger.getLogger("com.foo.Bar");

or

 Logger logger = Logger.getLogger(XXX.class);

This will initialize the logger object.But my question is why need send the class type as a parameter? It seems when I use the logger, I don't care in which class I use it.So the Class type seems no effect to logger. If I declare a logger as static and public, I can call this logger at another class, So what's the intention of the author to design it like this? Will the Class type bind something when I use the logger? Or I can send any Class types to the getLogger function.

Why use Log4j?, Log4j is an open source , light weight framework that facilities a developer to log their message during the application development. These messages can be  Log4j is an open source, light weight framework that facilities a developer to log their message during the application development. These messages can be categorized and optionally displayed in development,QA and production environment. Logging messages can be categorized into five categories :

From the javadoc: Logger.getLogger(Class) is a shorthand for getLogger(clazz.getName()). A convention used with log4j and other logging frameworks is to define a static logger per class. For example,

public class SomeClass {
    private static final Logger LOG = Logger.getLogger(SomeClass.class);
    ...
}

I have found this convention to work well for organizing logging output. It's certainly not required but is a useful practice.

What is Log4J and how do I use it? - Web Tutorials, Log4j is a JavaSW library that specializes in logging. At the time of this writing, its home page is at http://logging.apache.org/log4j/docs/ . The Java library itself is  log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a popular logging package written in Java. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages.

1:you can use "class name" or "string name" when you define in log4j.properties before, such as

log4j.logger.anything=INFO,anything

so,you can record your log as

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

2:If you define some log name,you can check it easily,cus they are separate.

log4j Tutorial, log4j Tutorial - log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j is a​  Log4j has certainly various advantages over System.out.println firstly being checking the output the sout outputs to standard output which is mostly a console window whereas the output from Log4j can be to the console or email server,database table or a log file.

XXX.class is to Name your Logger i.e to flag subsequent log statements . To give you an idea which class certain log statements belongs to / originated from.

Log4j, Apache Log4j is a Java-based logging utility. It was originally written by Ceki Gülcü and is part of the Apache Logging Services project of the Apache Software​  Another great benefit of Log4j is that different levels of logging can be set. The levels are hierarchical and are as follows: TRACE, DEBUG, INFO, WARN, ERROR, and FATAL. If you set a particular log level, messages will get logged for that level and all levels above it, and not for any log levels below that.

Logger with class name is not mandatory, you can use your own message. It is convention to use:

Logger logger = Logger.getLogger(XXX.class) 

and is useful to debug. It will log which line of code is executed.

What is log4j framework?, log4j is a java framework/package used to do application logging of java applications. It has 3 basic components loggers, appenders, and  Apache log4j is an Apache Software Foundation Project and developed by a dedicated team of Committers of the Apache Software Foundation. For more info, please see The Apache Software Foundation. Apache log4j is also part of a project which is known as Apache Logging. Please see the License.

Why Log4j is better than System.Out.Println? (What is difference , Additionally, in Log4j different levels of logging can be set such as TRACE, DEBUG, INFO, WARN, ERROR and FATAL. If a log level is set  Almost every large application includes its own logging or tracing API. In conformance with this rule, the E.U. SEMPER project decided to write its own tracing API. This was in early 1996. After countless enhancements, several incarnations and much work that API has evolved to become log4j, a popular logging package for Java.

How to use Log4j with Selenium Web Driver, Log4j is an open source logging framework. With log4j – logging behavior can be controlled by editing a configuration file only without touching  log4j is a reliable, fast and flexible logging framework (APIs) written in Java, which is distributed under the Apache Software License. log4j has been ported to the C, C++, C#, Perl, Python, Ruby, and Eiffel languages. log4j is highly configurable through external configuration files at runtime.

Logging With Log4j in Java, log4j.* package. We have to initialize one Logger object for each Java class. We use Logger's methods to generate log statements. Log4j  What is log4j? log4j is a tool to help the programmer output log statements to a variety of output targets. In case of problems with an application, it is helpful to enable logging so that the problem can be located. With log4j it is possible to enable logging at runtime without modifying the application binary.

Comments
  • No, not ANY STRING. I write this in a jsp: Logger.getLogger("fp_check_power.jsp") and log.debug("..."). However, in the log file it only shows:"[2017/07/12 16:13:21, DEBUG] (jsp) : open url = ....".
  • I have the same problem, did you find a solution to it?
  • including no only --- i don't understand the sentence, could you explain?
  • Looks like Logger.getLogger(blah) creates one logger per class, rather than one logger per instance of a class. (I checked by getting logger two times for same class. logger1 == logger2 was true). Why doesn't it create a logger per instance ?
  • @testerjoe2 log4j manages the logger instances so that any two logger names (or classes in this case) result in the same instance. The practice of create a static logger instance per class is fairly well established. What are you trying to accomplish with multiple instances?
  • I don't need to use multiple instances of logger for the same class. I only want to understand why we prefer to use one instance.
  • @testerjoe2 understood. I think it's just the way the API the organized. Logger instances are managed by name.
  • That's useful, but I can format my log content to categorize my log.