Generic way of getLogger from log4j.Logger

Instead of specifying the class name on each class:

log = Logger.getLogger(Foo.class);
log = Logger.getLogger(Bar.class);
log = Logger.getLogger(Test.class);

Will it be OK to use :

log = Logger.getLogger(this.getClass());

What will be the implications?

If you create a subclass, the log messages will get logged to the subclass's logger.

package pkgone;
public class SuperType {
    private Logger log = Logger.getLogger(this.getClass());
    public void someAction() {"Doing something");


package pkgtwo;
import pkgone.SuperType;
public class SubType extends SuperType {
    // for instances of SubType, the log object in SuperType
    // will get initialized with SubType's class object


// some code somewhere that uses SubType
SubType obj = new SubType();

In the above example, "Doing something" will get logged to the pkgtwo.SubType logger instead of pkgone.SuperType logger, which may or may not be what you want.

Is Logger.getLogger(MyClass.class) the best way to initialise log4j , Your comment says that "verbose" refers to the need to repeat this line of code in every class. My first response is that, in the big picture, adding two lines of code� and finally the usage will be static final Logger LOGGER = LogManager.getLogger(WebService.class.getName()); Note: don't forget to put config file into the root directory of the project, other wise you won't be able to get your logs.

Try this way to look up a generic class...

private static final Log LOGGER = Logger.getLogger(MethodHandles.lookup().lookupClass());

The best part is you can use this method statically.

Java Code Examples for org.apache.log4j.Logger#getLogger, This page shows Java code examples of org.apache.log4j.Logger#getLogger. getLogger(JsonSourceMapper.class); UnitTestAppender appender = new UnitTestAppender(); log. getString("comp", "local-filebroker-user-data-path")); this. static Logger log = Logger.getLogger(log4jExample.class.getName()); Logging Methods. Once we obtain an instance of a named logger, we can use several methods of the logger to log messages. The Logger class has the following methods for printing the logging information. #

If you don't want to repeat to make the logger and want to avoid write a wrong class name, there's @Log of Project Lombok.

If you don't mind using one more library in your project, you can simply add a logger with a simple annotation.

import lombok.extern.slf4j.Slf4j;

public class LogExample {    
  public static void main(String... args) {
    log.error("Something's wrong here");

Logging With Log4j in Java, We use Logger's methods to generate log statements. Log4j provides the factory method to get Logger objects. Syntax to get Logger objects:., ConsoleAppender. Programmatically, an individual logger can be created or retrieved (if already exists) by invoking the static methods Logger.getLogger(Class) or Logger.getLogger(String). For example:

Sure that seems fine.

Be aware that one implication is that if you may have trouble using the log from a static context - It may not be initialized or it may not be visible

Category (Apache Log4j 1.2.17 API), Log4j 1.2 will never produce Category objects but only Logger instances. In order to getLogger(Class) instead. This is the most generic printing method. This Mkyong tutorial suggests intialising loggers this way: @Controller public class WelcomeController { private static final Logger logger = Logger.getLogger(WelcomeController.class); // etc } Now presumably every other class you use, that has a logger will initialise their loggers the same way.

+1 for sublass instances that call a super method, the logger will be the sub class.

Also if you are applying this as a logging template for your classes, if your class is abstract - your this.getClass will fail as you do not have an instance of this class.

NOPLogger (Apache Log4j 1.2.17 API), This generic form is intended to be used by wrappers. Set the resource bundle to be used with localized logging methods Methods inherited from class org. apache.log4j.Logger � getLogger, getLogger, getLogger, getRootLogger� Like getLogger(String) except that the type of logger instantiated depends on the type returned by the LoggerFactory.makeNewLoggerInstance(java.lang.String) method of the factory parameter. This method is intended to be used by sub-classes.

Logger (Apache Log4j 1.2.17 API), In other words, calling this method is the only way to retrieve the root logger. getLogger. public static Logger getLogger(String name, LoggerFactory factory). Like� The following are top voted examples for showing how to use org.apache.logging.log4j.Logger.These examples are extracted from open source projects. You can vote up the examples you like and your votes will be used in our system to generate more good examples.

Extending Log4j 2 - Apache Logging Services, Log4j 2 provides numerous ways that it can be manipulated and extended. with the ClassLoader that created the caller of the getLogger call. shows how the root LoggerConfig simply extends a generic LoggerConfig. The method getLogger() is provided as the most convenient way to obtain a named Logger based on the calling class name. This class also provides method for obtaining named Loggers that use String.format(String, Object) style messages instead of the default type of parameterized messages.

Logger in Java | Java Logging Basics with Log4j and util, The getLogger() method identifies the name of the Logger and takes string as a parameter. So, if a Logger pre-exists then, that Logger is returned,� Implementation of Log that maps directly to a Logger for log4J version 1.2.. Initial configuration of the corresponding Logger instances should be done in the usual manner, as outlined in the Log4J documentation.

  • Here is something I didn't think about! thanks! what will be the difference if its: Logger.getLogger(SuperType.class);
  • @user648026 then logging requests to that log object will go to the pkgone.SuperType logger. I.e., no matter what subclass it is, the log message will in the above example will go to that logger. IMO this is less confusing as a rule, since you know exactly which logger it's going to. But there may be situations where you want the logging requests from SuperType to go to SubType's log, perhaps if you have multiple subclasses and want to categorize log messages based on the subclass.
  • does it do this.getClass() or ClassName.class ?
  • @Vikash It's the latter according to the docs
  • see matts comment for one possible implication