Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic way of getLogger from log4j.Logger

Tags:

java

log4j

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?

like image 611
JavaSheriff Avatar asked May 23 '12 18:05

JavaSheriff


People also ask

What is logger getLogger ()?

getLogger(java. lang. String): This method is used to find or create a logger with the name passed as parameter. It will create a new logger if logger does not exist with the passed name.

What does getLogger return?

getLogger() (without passing a name), then it simply will return the root logger which is created at import time (and therefore, it is never new).

What is Loggers in Log4j2?

Logger Hierarchy is made up of set of LoggerConfig objects with a parent-child relationship. The topmost element in every Logger Hierarchy is the Root Logger. If Log4j2 doesn't find the configuration file, only Root Logger will be used for logging with logging level as ERROR.

What is Log4j2 used for?

It allows for easy configuration of advanced logging best practices such as rolling files, different types of logging output destinations, support for structured logging formats such as JSON or XML, using multiple loggers and filters, and custom log levels.


3 Answers

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() {
        log.info("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();
obj.someAction();

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.

like image 103
matts Avatar answered Nov 12 '22 11:11

matts


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.

like image 21
davesbrain Avatar answered Nov 12 '22 12:11

davesbrain


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.java.Log;
import lombok.extern.slf4j.Slf4j;

@Log
public class LogExample {    
  public static void main(String... args) {
    log.error("Something's wrong here");
  }
}
like image 23
Sanghyun Lee Avatar answered Nov 12 '22 12:11

Sanghyun Lee