The introduction

Lu Xun once said that it is impossible for programmers to be without bugs. An excellent programmer not only needs coding efficiency and quality, but also needs to be calm and confident to quickly locate the wrong target after problems occur. Today we’re going to talk about how to elegantly filter out exactly what you want from logs.

Members of the introduction

There are many Linux commands that can query logs. Simple queries should be competent for everyone. Here are some of the most commonly used Linux query commands and their commandsCommon grammar

A single practice

The following practices are easy to understand and remember with specific examples

cat

One day when the project went online, the client suddenly reported an exception, and the connection to the database was incorrectly configured. In this case, we can use the cat command to open the database configuration file cat XXXX. XML of the project, and directly display the overall view of the current database configuration file. When errors are found, we can use the vi or vim command to modify them.

tail

The tail command should be the most commonly used query log command by programmers. Its function is to display the trailing log, which can be scrolled to view with -f. Tailf xxx.log (tailf -100f) is the last 100 lines of the output. In a typical test environment, this command is sufficient to view the log because there are not many visitors and the log scrolling is not very fast.

grep

In fact, the output of the entire file is consistent with that of the grep and cat commands. However, it supports wildcards and special functions that can enhance the output of CAT, and I’ll use examples of their combination later.

head

For example, if I want to know what logs are printed in the first 1000 lines of a log file, I can use the head-1000f xxx.log command to do so.

sed

It’s actually an edit command, but we can use its P print behavior to precisely print our error log. For example, knowing that the number of error lines is between 100 and 200, we can use sed-n ‘100,200p’ xxx.log to find exactly what we want to report.

More with less

Both more and less are pagination grammars, which can display long queried log information in pagination. The difference is that more can only display more information, which cannot be turned up. Therefore, Linux provides another command less to supplement the function of turning up the page.

Afterword.

From the above analysis, it can be seen that these seven query commands have different characteristics in the query, so can we combine them? How about making our log queries smarter and more flexible? The answer is yes, and the combination is elegant. Tomorrow I’ll rehearse a few scenarios to illustrate their combination.