How to query logs gracefully under Linux

Source: Internet
Author: User

As a qualified Java background development
Often need to query the log on the line, to locate the problem on the line
So master the command of log query
Allows you to locate the error log location more quickly and solve the problem in a timely manner
Here, I will introduce several log query commands that I use frequently in my own work.
We all learn to discuss together, so that we can more elegant operation of the log

Suppose you have a log file demo.log that contains the contents as follows,
We will use this file to demonstrate how to gracefully query the log file and locate the contents of the file.

line1 123456 aaline2 123456 bbline3 123456 ccline4 123456 ddline5 654321 aaline6 654321 bbline7 12line8 34line9 56line0 78
    • Tail command

Used for trailing content in the output file, the actual application is as follows:

// 显示文件倒数2行数据,并实时刷新新日志tail -2f demo.log   // 执行效果如下:line9 56line0 78// 如果你需要停止,按Ctrl+C退出// 假如查看的日志,实时刷新的日志量非常多的话,慎用!
    • Cat command

Commands are used to connect files and print to standard output devices

// 显示文件全部内容cat demo.log // 执行结果:line1 123456 aaline2 123456 bbline3 123456 ccline4 123456 ddline5 654321 aaline6 654321 bbline7 12line8 34line9 56line0 78// 由于会显示整个文件的内容,所以如果文件大的话,慎用!
    • More commands

Similar cat , but will be displayed as a page by page, press the blank key to space the next page, press the b key will be back one page display

more demo.log// 执行结果(文件内容少的话,会直接显示全部,效果跟cat一样):line1 123456 aaline2 123456 bbline3 123456 cc--More--(15%)
    • Less command

lessmoresimilar, but use less can freely browse the file (using the up and down arrows on the keyboard), and less will not load the entire file before viewing

less demo.log// 执行结果(文件内容少的话,会直接显示全部,效果如下):line1 123456 aaline2 123456 bbline3 123456 ccline4 123456 ddline5 654321 aaline6 654321 bbline7 12line8 34line9 56line0 78demo.log (END)
    • grep command

grepThe directive is used to find the file containing the specified template style, and if the contents of a file are found to conform to the specified template style, the grep instruction will display the column containing the template style.

// 查询包含关键字`123456`的日志内容grep "123456" demo.log// 执行结果line1 123456 aaline2 123456 bbline3 123456 ccline4 123456 dd
// 查询包含关键字`123456`且包含`aa`的日志内容grep "123456" demo.log | grep "aa"// 执行结果line1 123456 aa
// 查询不包含`aa`的日志内容grep -v "aa" demo.log// 执行结果line2 123456 bbline3 123456 ccline4 123456 ddline6 654321 bbline7 12line8 34line9 56line0 78
// 查询包含关键字`123456`但不包含`aa`的日志内容grep "123456" demo.log | grep -v "aa"// 执行结果line2 123456 bbline3 123456 ccline4 123456 dd
// 查询包含关键字`123456`或`aa`的日志内容grep "123456\|aa" demo.log或者grep -E "123456|aa" demo.log// 执行结果line1 123456 aaline2 123456 bbline3 123456 ccline4 123456 ddline5 654321 aa
    • Summarize

When you are grep looking for keywords, but still have a lot of matching results, you can combine the preceding less or more implementing pagination display, as shown in the following example:

grep "123456" demo.log | lessgrep "123456" demo.log | moregrep "123456\|aa" demo.log | lessgrep "123456" demo.log | grep -v "aa" | less

With a few of the commands described above, I believe it should be enough to cope with most of the search log scenarios.

Well, the rest is a practice.

Of course, if you have a better way to query the log more elegant, but also hope you can share with me ~

How to query logs gracefully under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.