Talk about Java logs

 

This article was first published on the personal WeChat public account "andyqian", looking forward to your attention

foreword

When we write code, we all know to add comments to key algorithms and logical places. As a result, the maintainability of the code is improved. The second is to make the code self-readable. If we understand annotations as static self-readability. So, when the program is running, how can we know the actual running path of the program? This is today's protagonist - the log!

Why log?

I have many friends who don't like to log. Hundreds, even thousands of lines of code. Clap, crack, crack it all! Full of self-confidence, I didn't type a single log. There is no problem in joint debugging, testing, and online. After the system has been running for a period of time, there is an inexplicable problem. So, where exactly is the problem? What are the parameters of the method? Where has the system come? Know nothing. Now only catch blind. From here, we should be able to see that it is very necessary to log. Let's sort it out. There are many benefits to logging.

  1. The most direct benefit is that it is convenient to solve BUG.
  2. To record the elapsed time of the request, especially when connecting to a third-party provider and calling a remote service, we can record the elapsed time of the request through logs.
  3. The key business, the input parameters of the key algorithm, and the results are recorded and recorded.

Finally: Logging is for analyzing problems and solving bugs for yourself. We try to record as concise, understandable and clear as possible. Ideally, we can even restore the whole process of a request and a call through the log.

How to log?

We said above why we need to log. Now let's talk about how to log. For our most commonly used log component slf4j, its corresponding log levels are: trace, debug, info, warn, error, these five log levels. Its range is: error>warn>info>debug>trace.

In our daily development:

  1. In the early stage of development, we can use the log level of the info mode when entering the method to record the input and output parameters of the method. Before the remote method call, after the call, use the info level. Time spent logging remote methods. At try{}catch, use the error level log level to log information. It is convenient to quickly find and analyze problems.
  2. After the service runs stably for a period of time, we can modify the parameters that must be recorded to warn level. The log level is set to warn. That is, only logs in warn and error levels are displayed. (There are many companies that use error-level logs as alarm information and send them to the corresponding application person in charge.)
  3. In a production environment, it is forbidden to enable debug level logging. Because the debug level log will display a lot of unnecessary logs. This can easily lead to a waste of server resources. In severe cases, it can even cause the server to be full. directly cause the service to be unavailable.
  4. Everything is the opposite. Similarly, the more logs the better, too many logs are printed, and the service performance will also be degraded. Even the server pressure is too high. Directly affects the availability of the service.
  5. We can log in the following form:

Take log4j+slf4j-api as an example:

pom.xml dependencies:

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-api</artifactId>
  <version>1.7.25</version>
</dependency>

<dependency>
  <groupId>org.slf4j</groupId>
  <artifactId>slf4j-log4j12</artifactId>
  <version>1.7.25</version>
  <scope>test</scope>
</dependency>

<dependency>
  <groupId>log4j</groupId>
  <artifactId>log4j</artifactId>
  <version>1.2.17</version>
</dependency>

The following log example:

import org.junit.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


public class LogTest {

    private static Logger logger = LoggerFactory.getLogger(LogTest.class);


    @Test
   public void testLog(){
        String name="andyqian";
        String blog = "www.andyqian.com";
        //就日志
        logger.debug("name: {} and blog : {} ", name, blog);
    }
}

We record logs in the form of placeholders. It is not recommended to use the form of string concatenation to log.

Common Java logging framework

In Java, common logging frameworks are

  1. JDK Logger that comes with JDK.
  2. Apche commons Logging。
  3. Apache log4j。
  4. Slf4j ( Simple Logging Facade for Java)
  5. Logback
  6. Apache log4j 2 (This is actually an upgraded version of Apache log4j. On the basis of log4j, the performance has been further improved.)

In daily development, we usually implement Slf4j+. In the above case, we talked about it (in the form of slf4j+log4j). In actual use, we can also use the slf4j+ logback log as a log combination.

ps: The performance of each logging framework is not discussed in this article. Interested children's shoes can test by themselves.

summary

I haven't written an article for several days, and my hands are a bit raw. Looking through the articles I have recorded in my spare time is like running barefoot on the beach for a long time, sometimes looking back and seeing the footprints washed by the sea. I also find it interesting!

Related Reading:

" Java Annotations You Should Know "

" Tell me about unit testing "

" A Java Detail "

" A Preliminary Study of the Character Set of the JDK Source Code "

 

write picture description here

 Scan the code to follow and make progress together

Personal blog:  http://www.andyqian.com

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324381416&siteId=291194637