HelloWorld


Zhikang Li's personal blog


java Logging

Overview

Java Util Logging, Log4J, Log4J 2, and Logback …

Log4j2

Advantages

  • define different levels of importance, such as ERROR, WARN, INFO, and DEBUG for log messages
  • define one or more destinations, such as console, file, database, and SMTP server to send log messages
  • perform logging asynchronously.
  • control logging on a class-by-class basis
  • easy to enable or disable only some type of log message

Components

Loggers
  • Loggers are the key objects in Log4J 2 that are responsible for capturing logging information. Loggers are stored in a namespace hierarchy and a root logger, an implementation of the Logger interface, sits at the top of the hierarchy
  • retrieve the root logger LoggerManager.getRootLogger()
  • For all other loggers, you can instantiate and retrieve them as LoggerManager.getLogger(String loggerName)
  • LogManager.getLogger(). This method, by default, uses the fully qualified class name of the owning class.
  • log level
    • trace, debug, info, warn, error, fatal
    • local, debug
    • production, error
  • In your application, once you have retrieved a logger, you call one of the printing methods debug(), … and log() on the logger to log messages. These messages are contained in the Logger interface and part of the root logger that all Log4J 2 loggers inherit.
Appenders
  • The output destination is called an appender, and it is attached to the logger.
  • Appenders are inherited additively from the logger hierarchy
    • if the console appender is attached to the root logger, all child loggers will inherently use the console appender
Layouts
  • In addition to specifying your preferred output destination, you can also specify the format of log messages. You can do so by associating a layout with the appender
  • eg: PatternLayout, Htmlayout, JsonLayout, and XmlLayout.

Using log4j

not configure appender and layout
  • pom

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter</artifactId>
    <exclusions>
    <exclusion>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-logging</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
  • code.java

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    import org.apache.logging.log4j.LogManager;
    import org.apache.logging.log4j.Logger;
    public class MyApp {
    private static Logger logger = LogManager.getLogger("MyApp.class");
    public void performSomeTask(){
    logger.debug("This is a debug message");
    logger.info("This is an info message");
    logger.warn("This is a warn message");
    logger.error("This is an error message");
    logger.fatal("This is a fatal message");
    }
  • will relied upon defaults inherited from the Log4J 2 root logger

  • The Log4J 2 root logger is associated with the console appender (ConsoleAppender class) by default
  • default Log4j 2 assigns the root logger level to ERROR and without external configuration
  • the root logger by default uses PatternLayout

SLF4j

INTRO

  • SL4J is a façade for commonly used logging frameworks
    • eg : Java Util Logging, Log4J, Log4J 2, and Logback
  • As a developer, you write logging code against the SL4J API. At deployment time, you have the flexibility to plug-in your desired logging framework, made possible through an intermediate bridge/adapter
    Application code -> SLF4j api -> bridge -> Log4j 2
  • With SL4J, you cannot perform operations such as configuring appenders or setting logging levels. You perform such configurations through a configuration file of the logging framework in use

Example

Depedency
  • add slf4j, log4j depedency
  • Exclude the built-in Logback dependency. This is necessary as Spring Boot will pick and use Logback if present in the classpath.
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-security</artifactId>
    <exclusions>
    <exclusion>
    <groupId>ch.qos.logback</groupId>
    <artifactId>logback-classic</artifactId>
    </exclusion>
    </exclusions>
    </dependency>
Configuration

application.properties or XML, JSON, and YAML

Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class IndexController {
private final Logger logger = LoggerFactory.getLogger(this.getClass());
@RequestMapping("/")
String index(){
logger.debug("This is a debug message");
logger.info("This is an info message");
logger.warn("This is a warn message");
logger.error("This is an error message");
return "index";
}
}

ref: