Reading Log files and finding errors 读取日志文件并查找错误

huangapple go评论51阅读模式
英文:

Reading Log files and finding errors

问题

我想从文件夹中读取当前日期的所有日志文件,并查找包含“ERROR”字符串的行。

创建一个具有以下列的表格:

LogName, ErrorDescription

ErrorDescription 应该包含具有错误条目的整行。

例如:

2023.04.17D07:05:42 : ERROR : 连接失败
英文:

I want to read all log files from a folder for current date and find lines with "ERROR" string.

Create a table with columns as:

LogName, ErrorDescription

ErrorDescription should contain entire line which has Error Entry.

For Example:

2023.04.17D07:05:42 : ERROR : Failed to connect

Please help.

答案1

得分: 1

以下是您要翻译的内容:

问题有些模糊,但以下是如何逐步构建它的示例:

列出文件夹中的文件:
```q
q)key `:myLogs
`s#`2023-04-17-process.log`2023-04-18-process.log

仅返回今天的.log文件:

q){x where x like @[string[.z.d];4 7;:;"-"],"*.log"}key `:myLogs
,`2023-04-18-process.log

从文件中读取文本:

q)show each {{read0 .Q.dd[x;y]}[x] each {x where x like @[string[.z.d];4 7;:;"-"],"*"}key x}`:myLogs
"2023.04.17D07:05:42 : ERROR : Failed to connect"
"2023.04.17D07:05:42 : DEBUG : Failed to connect"

" : "上拆分文本:

q)show each {{vs[" : "] each read0 .Q.dd[x;y]}[x] each {x where x like @[string[.z.d];4 7;:;"-"],"*"}key x}`:myLogs
"2023.04.17D07:05:42" "ERROR" "Failed to connect"
"2023.04.17D07:05:42" "DEBUG" "Failed to connect"

从日志字段创建表格:

q)raze {{{`time`level`msg!("P"$x 0;"S"$x 1;x)} each vs[" : "] each read0 .Q.dd[x;y]}[x] each {x where x like @[string[.z.d];4 7;:;"-"],"*"}key x}`:myLogs
time                          level msg
-------------------------------------------------------------------------------------
2023.04.17D07:05:42.000000000 ERROR "2023.04.17D07:05:42" "ERROR" "Failed to connect"
2023.04.17D07:05:42.000000000 DEBUG "2023.04.17D07:05:42" "DEBUG" "Failed to connect"

仅返回ERROR级别的日志:

q){select from x where level=`ERROR}raze {{{`time`level`msg!("P"$x 0;"S"$x 1;x)} each vs[" : "] each read0 .Q.dd[x;y]}[x] each {x where x like @[string[.z.d];4 7;:;"-"],"*"}key x}`:myLogs
time                          level msg
-------------------------------------------------------------------------------------
2023.04.17D07:05:42.000000000 ERROR "2023.04.17D07:05:42" "ERROR" "Failed to connect"

<details>
<summary>英文:</summary>

Question is vague but as example of how you might build it up:

List files in foler:

q)key :myLogs
s#2023-04-17-process.log2023-04-18-process.log


Only return `.log` files for today:

q){x where x like @[string[.z.d];4 7;:;"-"],"*.log"}key :myLogs
,
2023-04-18-process.log


Read in the text from the files:

q)show each {{read0 .Q.dd[x;y]}[x] each {x where x like @[string[.z.d];4 7;:;"-"],"*"}key x}`:myLogs
"2023.04.17D07:05:42 : ERROR : Failed to connect"
"2023.04.17D07:05:42 : DEBUG : Failed to connect"


Split the text on `&quot; : &quot;`:

q)show each {{vs[" : "] each read0 .Q.dd[x;y]}[x] each {x where x like @[string[.z.d];4 7;:;"-"],"*"}key x}`:myLogs
"2023.04.17D07:05:42" "ERROR" "Failed to connect"
"2023.04.17D07:05:42" "DEBUG" "Failed to connect"


Create a table from the log fields:

q)raze {{{timelevelmsg!(&quot;P&quot;$x 0;&quot;S&quot;$x 1;x)} each vs[&quot; : &quot;] each read0 .Q.dd[x;y]}[x] each {x where x like @[string[.z.d];4 7;:;&quot;-&quot;],&quot;*&quot;}key x}:myLogs
time level msg

2023.04.17D07:05:42.000000000 ERROR "2023.04.17D07:05:42" "ERROR" "Failed to connect"
2023.04.17D07:05:42.000000000 DEBUG "2023.04.17D07:05:42" "DEBUG" "Failed to connect"


Only return `ERROR` level logs:

q){select from x where level=ERROR}raze {{{timelevelmsg!("P"$x 0;"S"$x 1;x)} each vs[" : "] each read0 .Q.dd[x;y]}[x] each {x where x like @[string[.z.d];4 7;:;"-"],"*"}key x}`:myLogs
time level msg

2023.04.17D07:05:42.000000000 ERROR "2023.04.17D07:05:42" "ERROR" "Failed to connect"


</details>



huangapple
  • 本文由 发表于 2023年4月17日 17:47:48
  • 转载请务必保留本文链接:https://go.coder-hub.com/76033794.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定