Follow + star our public account to not miss wonderful content
Source | CSDN-nan1996
Compiled & formatted | Embedded Application Research Institute
Quickly Locate Errors in Large Log Files
View logs dynamically
tail -f catalina.out
Open the log file from the beginning
cat catalina.out
You can use >nanjiangtest.txt
to output a new log for viewing
[root@yesky logs]# cat -n catalina.out | grep 717892466 > nanjiangtest.txt
Simple Commands for tail/head:
[root@yesky logs]# tail -n number catalina.out Get the last number lines of the log
[root@yesky logs]# tail -n +number catalina.out Get all logs after line number
[root@yesky logs]# head -n number catalina.out Get the first number lines of the log file
[root@yesky logs]# head -n -number catalina.out Get all logs except the last number lines
Method 1 (Find line numbers by keywords):
Using grep to get very few logs, we need to check nearby logs. I do this by first: cat -n test.log | grep "keyword"
to get the line number of the key log
[root@yesky logs]# cat -n catalina.out | grep 717892466
13230539 [11:07 17:47:11] INFO nanjiang:Edit Old Article: 717892466-2020-11-07 17:47:11
13230593 [11:07 17:47:15] INFO nanjiangSave Article ID IS: 717892466
13230595 717892466 article.getDisplayTime()1 = 2020-11-07 16:25:11
13230596 717892466 article.getDisplayTime()2 = 2020-11-07 16:25:11
13230601 [11:07 17:47:15] INFO nanjiang 10.10.10.39 edit article 717892466 Edit article
“cat -n catalina.out | tail -n +13230539 | head -n 10”
-
tail -n +13230539
means to query logs after line 13230539 -
head -n 10
means to query the first 10 records from the previous query result
[root@yesky logs]# cat -n catalina.out | tail -n +13230539 | head -n 10
13230539 [11:07 17:47:11] INFO nanjiang:Edit Old Article: 717892466-2020-11-07 17:47:11
13230540 [11:07 17:47:11] INFO Takes:2 ms class com.tmg.cms.manager.dao.article.impl.ArticleContentDaoImpl getListByArticleId [NzE3ODkyNDY2] [int]
13230541 [11:07 17:47:11] INFO Takes:1 ms class com.tmg.cms.manager.dao.resourceImage.impl.ResourceImageDaoImpl load
13230542 [11:07 17:47:11] INFO Takes:0 ms class com.tmg.cms.manager.dao.resourceImage.impl.ResourceImageDaoImpl load
13230543 [11:07 17:47:11] INFO Takes:1 ms class com.tmg.cms.manager.dao.resourceImage.impl.ResourceImageDaoImpl load
13230544 [11:07 17:47:11] INFO article.getImage3: /uploadImages/2020/312/02/3NXCRK4U3589_2.jpg
13230545 [11:07 17:47:11] INFO Takes:0 ms class com.tmg.cms.manager.dao.resourceImage.impl.ResourceImageDaoImpl load
13230546 [11:07 17:47:11] INFO Takes:2 ms class com.tmg.cms.manager.dao.privilege.impl.UserDaoImpl getUserByid
13230547 [11:07 17:47:11] INFO Takes:57 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl selectSitemapWithoutAudit [MQ==] [int]
13230548 [11:07 17:47:11] INFO Takes:5 ms class com.tmg.cms.manager.dao.forbidword.impl.ForbidwordDaoImpl getForbidwordBysiteid [MjI=] [int]
Method 2: View logs within a specified time range
First, check if there are logs within the current day by querying the time range
grep '11:07 18:29:20' catalina.out
grep '11:07 18:31:11' catalina.out
Query within the time range
sed -n '/11:07 18:29:20/,/11:07 18:31:11/p' catalina.out
sed -n '/11:07 18:29:/,/11:07 18:31:/p' catalina.out
Method 3: Count occurrences of specific characters in logs
[root@yesky logs]# grep '1175109632' catalina.out | wc -l
154
Method 4: Query the last number of lines and find the keyword “result”
[root@yesky logs]# tail -n 20 catalina.out | grep 'INFO Takes:1'
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.config.impl.ConfigInfoDaoImpl load
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [NTkwOTQ5] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [MzI0] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [MzI3] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [MzMw] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [MzA5NA==] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [NTQ2MzQw] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [NTg2NzYy] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [MzYyMjA=] [int]
Method 5: Query the last number of lines, find the keyword “result” and highlight the results
[root@yesky logs]# tail -n 20 catalina.out | grep 'INFO Takes:1' --color
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.config.impl.ConfigInfoDaoImpl load
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [NTkwOTQ5] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.sitemap.impl.SitemapDaoImpl getSitemapTop [MzI0] [int]
Method 6: Query the last number of lines, find the keyword “result”, highlight the results, and expand two lines above and below
[root@yesky logs]# tail -n 20 catalina.out | grep 'INFO Takes:1' --color -a2
[11:11 22:02:51] INFO Takes:0 ms class com.tmg.cms.manager.dao.article.impl.ArticleContentDaoImpl getArticlePageNum [NzE4MTM2ODky] [int]
[11:11 22:02:51] INFO Takes:1 ms class com.tmg.cms.manager.dao.config.impl.ConfigInfoDaoImpl load [com.tmg.cms.manager.model.config.ConfigInfo]
Method 7: Paginated view, use space to flip pages (using more/less)
[root@yesky logs]# tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -a2 | more
[root@yesky logs]# tail -n 2000 catalina.out | grep 'INFO Takes:1' --color -a2 | less
Additional
1. Full screen navigation
-
ctrl + F
– Move forward one screen -
ctrl + B
– Move back one screen -
ctrl + D
– Move forward half a screen -
ctrl + U
– Move back half a screen
2. Single line navigation
-
j
– Move forward one line -
k
– Move back one line
3. Other navigation
-
G
– Move to the last line -
g
– Move to the first line -
q / ZZ
– Exit less command
