preface

Only a bald head can be strong.

Welcome to our GitHub repository Star: github.com/ZhongFuChen…

I remember that I wrote an article before: “Alibaba Java development manual” after reading, before self-study because there is no contact with the “log”, so “manual” in the “log statute” I put aside.

Before, I wrote an article: What is the difference between the project I did in the company and the project I did in school? There will not be e.printStackTrace() for company projects; The existence of such code. Because this printed error message has no date, grade, etc., it is not easy to analyze.

Checking logs on the server at work is a very, very common operation, so I wrote a common Linux command at work, which talks about checking logs often used Linux commands.

I thought that since I had been in touch with the log for a while, I might as well look back at the Manual and see what I should pay attention to, so I got this note.

First, Java to play the basis of logging

When I used to study by myself, I would only write down the following code:

try {
   // doSomething
} catch(Exception e) { e.printStackTrace(); } -- -- -- -- -- -- -- -- -- --// Check the value of the data:
System.out.println(xxxx);
Copy the code

When I went to the company, I found that all the above code was missing, and the rest was:

LOGGER.info("begin to run Java3y:{}", id);
----
LOGGER.error("excepiton occurs when run Java3y {}, exception{}", id, e.toString());
Copy the code

If e.prinintStackTrace () is used; If the printed information in the control is analyzed, it is not convenient:

We record the information in grade and time on the disk of the server. If there is a problem, we can search for the relevant log according to the corresponding information (it is very convenient to troubleshoot) :

Let’s take a look at what a typical log looks like:

For example: now someone comes to feedback that certain user seems to fail to receive SMS messages, and gives the sending time and user ID. Then we can find out the sending status of this user in our system on the log (for example: state:81 on the figure, we consider it as successful sending status).

So, the question is, where do we log? The answer is already in the Manual:

Log carefully. Debug logs cannot be generated in the production environment. Selectively output info logs; If you make

Warn is used to record the service behavior information when the service is launched. Be careful about the log output to avoid disconnecting the server

Brace up and delete these observations as soon as possible.

A large number of invalid logs are generated, which is not conducive to system performance improvement or fault location. Think about this as you log

Does anyone really read the logs? What can you do with this log? Does it benefit troubleshooting?

1.1 What is dotting?

The most common log is used to print information about the execution of the program, which is used to quickly locate and troubleshoot problems. That’s how I understood it at first, but it can be extended.

In my current system, we also use logging to dot the execution link of the system. For example, if I want to push a notification message, the notification message actually looks like this:

The process goes something like this:

  • First someone calls the interface provided by my RPC (or I call my own interface) and discovers that this is a notification message. So I assemble the corresponding Task and asynchronously place it in the message queue
  • Another system fetches a Task from the message queue, performs business processing on the Task (such as whether it is blocked at night, whether it is forced to send, etc.), and then invokes an HTTP interface to pass the Task downstream
  • In fact, there are many things to do downstream, the whole link is very long (for example, to call the SDK library, Android and IOS do different processing).

We want to have metrics (exposure, click-through rate, conversion rate) after we post. As a result, you need to make a log in some key positions.

After the whole link is open, these points (logs) are collected and cleaned/filtered on storm/ Flink. If it is needed in real time, put it in Redis. If it is needed offline, put it in Hive.

Ii. Manual specifications

2.1 Logging framework using facade mode

[Mandatory] The API of logging system (Log4j and Logback) cannot be directly used in applications, but the logging framework must be used

The API in SLF4J, which uses the facade logging framework, facilitates maintenance and uniformity of log handling for each class.

I wrote a note before: Learn facade in 3 minutes!

Basically, the idea is to abstract a layer of API that allows you to switch between specific logging frameworks without extensive changes.

We can use JDBC to understand:

Whether I connect to MySQL, Oracle, or SQL Server, my interface is always the same, and I don’t need to change my Java API when SWITCHING databases

Have a look at the company’s project, using SLF4J+Logback

2.2 Invoking an RPC Interface Use the Throwable class for interception

Throwable must be used to catch exceptions when calling RPC, binary packages, or methods related to dynamically generated classes

Class to intercept.

In the previous troubleshooting, there was a problem that could not be sorted out, and the catch module was not included in the DeBug. Then my senior said, “Why don’t you try Throwable instead?

try{}catch (Throwable e) {
		
}
Copy the code

I was like, “Why Throwable? We can catch all exceptions with Exception. Exception is a subclass of Throwable, but Exception already contains all Java exceptions.”

As you know, Throwable has two subclasses:

  • Error (normally we ignore this… In general, Error programs will not run.)
  • Exception
The  Throwable class is the superclass of all errors and exceptions in the Java language
Copy the code

The above rules are also explained in the Manual:

Call a method by reflection mechanism. If no method is found, throw NoSuchMethodException. What would it sell

NoSuchMethodError? In the case of a class conflict, the mediation mechanism may lead to the introduction of an unexpected version that causes the method signature of the class to be inconsistent

Or change the method signature when a bytecode modification framework (such as ASM) dynamically creates or modifs a class. In these cases, i.e

Makes code compile time correct, but NoSuchMethodError is raised at runtime.

Calls to RPC, binary packages, or methods related to dynamically generated classes may directly raise an Error, and catch exceptions cannot be caught.

Those of you who want to see examples can read this article:

  • zhuanlan.zhihu.com/p/57950399

The last

Reference materials (Alibaba Development Manual download address) :

  • github.com/alibaba/p3c

Good articles found in search:

  • www.cnblogs.com/chenhonglia…

  • www.cnblogs.com/crazyacking…

Happy to export dry Java technology public number: Java3y. The public account has more than 200 original technical articles, massive video resources, beautiful brain map, attention can be obtained!

Think my article is good, give it a thumbs up!