“This article has participated in the good article call order activity, click to see: back end, big front end double track submission, 20,000 yuan prize pool for you to challenge!”

❌ I. Pain points

At present, our company’s log management system is relatively primitive, using logback log to log files, although there is a service management platform, but the log record is only the front machine call background system input and logout, when the problem is more troublesome to check the log.

Log in to VPN- Open server - Find log directory - Open log file - Search

And this process is only in a server operation, generally need to look at the front machine, background system and even service management platform.

When the number of users is small, other indicators such as the sequence can be detected. However, when the number of users is small, it is difficult to determine which log is generated by which operation.

In addition, our product front facing users, background and many companies have a large number of products call each other, when the user encountered a problem first complaint is our products, such as in the log can not find the problem, the blame is us.

✔️ Two, the solution

There are two missions

  1. Link tracing, a log of a call that can be strung across any number of platforms;
  2. Log repository, which is mainly to develop a log query function for operation and maintenance personnel.

2.1, Tlog

After some investigation, for link tracking, we chose Tlog, a log tracking tool. Home page: yomahub.com/tlog/

The main considerations are:

  • The most basic function: log tag, and support tag template customization, you can use tLogContext.getTraceid () and tlogContext.putTraceid (ID) to obtain and set the ID;
  • Business code is non-intrusive

However, the HttpClient is intrusive, need to add interceptor

The implementation of this interceptor is fairly simple

public class TLogHttpClientInterceptor implements HttpRequestInterceptor {
    
    private static final Logger log = LoggerFactory.getLogger(TLogHttpClientInterceptor.class);
    
    @Override
    public void process(final HttpRequest request, final HttpContext context) throws HttpException, IOException {
        Args.notNull(request, "HTTP request");
        String traceId = TLogContext.getTraceId();
        if(StringUtils.isNotBlank(traceId)) {
            String appName = TLogSpringAware.getProperty("spring.application.name");

            request.addHeader(TLogConstants.TLOG_TRACE_KEY, traceId);
            request.addHeader(TLogConstants.TLOG_SPANID_KEY, SpanIdGenerator.generateNextSpanId());
            request.addHeader(TLogConstants.PRE_IVK_APP_KEY, appName);
            request.addHeader(TLogConstants.PRE_IVK_APP_HOST, LocalhostUtil.getHostName());
            request.addHeader(TLogConstants.PRE_IP_KEY, LocalhostUtil.getHostIp());
        } else {
            log.debug("[TLOG] chain 湴threadLocal, and dial dial, dial dial, dial dial, dial dial"); }}}Copy the code

Note, however, that there are some methods that use the methods provided by higher versions of Hutool. Note the resolution of version conflicts in your project.

There is still a problem with Tlog integration. The service management platform used by the company is developed and managed by other departments, which need to work together to solve the problem. But fortunately, we can get the source code 😂😂

2.2 Log storage

Log storage naturally does not want to have any impact on the current business, considering that the amount of concurrent system is not very large, the thread pool is used to call the log system repository.

2.3 Collect logs

Use annotations mainly to collect input and input and abnormal confidence processing. In addition, logback is simply encapsulated to provide info(), error() and other methods, and the logs input by these methods are also collected and stored. Due to the particularity of the business, the systems that need to be used reuse a set of custom annotations, common aspect methods, and complete the processing of the special business in the aspect.

Quite a lot of details will not write, this article is mainly to introduce the overall idea, for the amount of the system can say more or less.