英文:
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.log
2023-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 `" : "`:
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 {{{time
levelmsg!("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"
Only return `ERROR` level logs:
q){select from x where level=ERROR}raze {{{
timelevel
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>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论