To print logs, pay attention to the following four points.

1. Determine log levels in advance

For DEBUG and INFO logs, conditional output or placeholders are required. This protocol considers the efficiency of the program and the requirement of log printing.

Here’s a counter example:

log.debug("Input parameter id=" + id + ",obj=" + obj);
Copy the code

If logs at the DEBUG level are printed in an application whose print level is WARN, the logs will not be printed, but string concatenation will be performed. If obj were an object, the toString() method would also be executed, wasting system resources.

Examples of correct code are as follows:

// Use the conditional judgment formif (log.isDebugEnabled()) {
    log.debug(Input parameter id={}, id); } // Use placeholder form log.debug(Input parameter id={},obj={}, id, obj);
Copy the code

Avoid printing invalid logs

In the production environment, DEBUG logs are disabled and INFO logs are selectively output. When recording service log information at INFO and WARN levels, control the output quantity to avoid insufficient disk space, set a proper life cycle for log files, and clear expired logs in time.

To avoid repeated printing, be sure to set additivity=false in the log configuration file as shown in the following example:

<logger name="com.test" additivity="false">
    <level value="INFO" />
    <appender-ref ref="logfile" />
</logger>
Copy the code

Introduction to Additivity properties:

This is a flag bit that indicates whether a child Logger inherits its parent Logger’s appender. By default, a child Logger inherits its parent Logger’s appender. Set additivity to false and the child Logger will output only in its own appender and not in the parent Logger’s appender.

3. Treat error logs differently

WARN and ERROR are log levels related to errors. Do not print ERROR logs every time an ERROR occurs. For example, some service exceptions can be recovered by boot retry. The purpose of logging is to restore the scene during user consultation. If the output is ERROR level, it means that human intervention is required once it occurs, which is obviously not reasonable. Therefore, the ERROR level only records system logic errors, exceptions, or violations of important business rules. Other errors can be classified as a WARN level.

Iv. Ensure complete records

Note the following when printing logs:

  1. Be sure to output the exception stack when logging exceptions, for example:
log.error("xxx" + e.getMessage(), e);
Copy the code
  1. If an object instance is printed in the log, make sure that the instance class overrides the toString method, otherwise only the object’s hashCode value will be printed.

Resources: The Manual for Coding Effective Java Development

Recommended reading

1.SpringBoot Series – Log Configuration Basics

2.SpringBoot integrates JWT to implement permission authentication

3. JWT Certification in a minute!

4. How to elegantly read yML configuration files in SpringBoot?

5. How to flexibly implement the encryption and decryption function of interface data in SpringBoot?


Limited time to receive free Java related materials, covering Java, Redis, MongoDB, MySQL, Zookeeper, Spring Cloud, Dubbo/Kafka, Hadoop, Hbase, Flink and other high concurrency distributed, big data, machine learning and other technologies. Follow the public account below to get free: