Java - Logger

About

A Logger is the entry point to logs messages.

The main entity on which applications make logging calls. A Logger object is used to log messages.

Structure

  • It has a hierarchical scope that is generally the full qualified class name Example: com.example.app.MyClass.
  • Inheritance: A child Logger inherits any configured properties from its parent Logger.
  • Loggers are normally named entities, using dot-separated names such as “java.awt”.
  • The namespace is hierarchical and is managed by a LogManager.
  • The namespace is typically aligned with the Java packaging namespace
  • Loggers keep track of their parent loggers in the logging namespace. The root Logger (generally named the empty string “”) has no parent.

Properties

Level

Loggers may be assigned levels. For instance, TRACE, DEBUG, INFO, WARN and ERROR

The level filter the messages by their severity that this logger will append.

Example: A logger level of warning will not log any info message.

The effective level for a given logger is equal to the first non-null level in its hierarchy, starting at itself and proceeding upwards in the hierarchy towards the root logger.

Example:

  • if the logger with the name com.example.myapp.mypackage has been set to the level info
  • then the logger with the name com.example.myapp.mypackage.MyClass will have the level of its parent info. You may set a level to overwrite it.

The root logger has by default a level (generally DEBUG)

Management

Create/Get

A static logger factory is the entry point to get or create a logger

Example:

// get the fully qualified name of the class by default
protected static final Logger logger = LogManager.getLogger();
Logger x = LoggerFactory.getLogger("x.y.x"); 

Powered by ComboStrap