其他教程

其他教程

Products

当前位置:首页 > 其他教程 >

golang正则表达式在日志文件中获取SQL语句

GG网络技术分享 2025-03-18 16:15 2


问题描述:

I tried to get the SQL statement in the log file through the regular expression of golang, but there were some problems with the matching results. If the SQL did not break lines in the code, the correct result could be obtained, but if it was long and divided into multiple lines in the code, my SQL could only be part of the first line。my expectation is to get two full SQL

Below are my code and some sample logs

package main

import (

\"fmt\"

\"io/ioutil\"

\"os\"

\"regexp\"

)

func main() {

file, err := os.OpenFile(\"/home/gopath/src/log.txt\", os.O_RDWR, 0766)

if err != nil {

fmt.Println(err)

}

res, err := ioutil.ReadAll(file)

reg := regexp.MustCompile(`\\[ORM\\].*`)

str := reg.FindAllStringSubmatch(string(res), -1)

fmt.Println(str)

}

[ORM]2018/08/03 10:23:50 -[Queries/read] - [ OK / db.Query / 432.4ms] - [SELECT acc.*,gp.group_name,gp.group_id,org.org_name,group_concat(r.role_name) role_name

FROM sys_account acc

LEFT JOIN sys_org org on org.org_id=acc.org_id

LEFT JOIN sys_group gp on gp.group_id=org.group_id

LEFT JOIN sys_account_role ar on ar.acct_id=acc.acct_id and ar.is_del=0

LEFT JOIN sys_role r on r.role_id=ar.role_id where 1=1 and acc.acct_type=1 group by acc.acct_id order by acc.create_time desc LIMIT 0, 15]

nsq consumer2: INF 13 [RYOLST_Ch_admin/crm] (192.168.1.233:4150) received CLOSE_WAIT from nsqd

nsq consumer2: INF 13 [RYOLST_Ch_admin/crm] (192.168.1.233:4150) beginning close

nsq consumer2: INF 13 [RYOLST_Ch_admin/crm] (192.168.1.233:4150) readLoop exiting

nsq consumer2: INF 13 [RYOLST_Ch_admin/crm] (192.168.1.233:4150) breaking out of writeLoop

nsq consumer2: INF 13 [RYOLST_Ch_admin/crm] (192.168.1.233:4150) writeLoop exiting

[ORM]2018/08/03 10:23:50 -[Queries/default] - [ OK / db.Query / 0.6ms] - [select * from sys_group where group_id=? ] - 1

图片转代码服务由CSDN问答提供

感谢您的意见,我们尽快改进~

功能建议

我尝试通过golang的正则表达式在日志文件中获取SQL语句,但是存在一些问题 匹配结果。 如果SQL没有在代码中折行,则可以获得正确的结果,但是如果它很长,并且在代码中分成多行,则我的SQL只能是第一行的一部分。我的期望是得到两个全数 SQL

下面是我的代码和一些示例日志</ p>

 包main 

import(

“ fmt”

“ io / ioutil”

“ os”

“ regexp”

func main(){

file,err:= os.OpenFile(“ / home / gopath / src / log.txt”,os.O_RDWR,0766)

如果err!= nil {

fmt.Println(err)

}

res,err:= ioutil.ReadAll(file)

reg:= regexp.MustCompile(`\\ [ORM \\]。*`)

str:= reg.FindAllStringSubmatch(string(res),-1)

fmt.Println(str)

}

</ code> </ pre>

[ORM] 2018 / 08/03 10:23:50-[查询/读取]-[OK / db.Query / 432.4ms]-[SELECT acc。*,gp.group_name,gp.group_id,org.org_name,group_concat(r.role_name) role_name

FROM sys_account acc

LEFT JOIN sys_org org on org.org_id = acc.org_id

LEFT J 在gp.group_id = org.group_id上的OIN sys_group gp

在ar.acct_id = acc.acct_id和ar.is_del = 0上的LEFT JOIN sys_account_role ar在r.role_id = ar.role_id上​​的LEFT JOIN sys_role r其中1 = 1和 acc.acct_type = 1按acc.acct_id的顺序按acc.create_time desc的限制LIMIT 0,15]

nsq使用者2:INF 13 [RYOLST_Ch_admin / crm](192.168.1.233:4150)从nsqd

nsq使用者2:INF收到CLOSE_WAIT 13 [RYOLST_Ch_admin / crm](192.168.1.233:4150)开始关闭

nsq使用者2:INF 13 [RYOLST_Ch_admin / crm](192.168.1.233:4150)readLoop退出

nsq使用者2:INF 13 [RYOLST_Ch_admin / crm](192.168 .1.233:4150)突破writeLoop

nsq使用者2:INF 13 [RYOLST_Ch_admin / crm](192.168.1.233:4150)writeLoop退出

[ORM] 2018/08/03 10:23:50-[查询/默认 ]-[确定/ db.Query / 0.6ms]-[从sys_group中选择*,其中group_id =? ]- 1 </ code> </ p>

</ blockquote>

</ div>

网友观点:

Try this pattern: \\[ORM\\][\\w\\W]+?\\wms\\][ -]+\\[([\\w\\W]+?)\\].

Explanation:

\\[ORM\\] - match [ORM],

[\\w\\W]+?\\wms - match any character (inlucding ) until \\wms is found (\\w is a word character, can be digit as well) - non greedy, so it stops at first occurence of \\wms, so it won\'t capture until the second SQL log.

([\\w\\W]+?)\\] - capturing group, that will contain full SQL statement, again non-greedy search for any character, similairly as above.

Demo

Golang 常用日志库介绍

前言

为什么需要日志

  • 调试开发
  • 程序运行日志
  • 用户行为日志

不同的目的决定了日志输出的格式、频率。作为开发人员,调试开发阶段打印日志目的是输出尽可能全的信息(如上下文,变量值...),辅助开发测试,因此日志格式要易读,打印频率要高。而在程序运行时,日志格式倾向于结构化(便于分析与搜索),而且为了性能和聚焦于关键信息(如error ),打印频率更偏低。

Go 标准库 Log

使用

我们常使用 Go log 以下三组函数:

  • Print/Printf/Println : 打印日志信息
  • Panic/Panicf/Panicln : 打印日志信息后,以拼装好的字符串为参数调用 Panic
  • Fatal/Fatalf/Fatalln : 打印日志信息后,os.Exit(1) 退出程序

带 f 后缀的是格式化输出,ln 后缀增加换行符,不过在打印日志场景中会自动增加一个换行符,这里 ln 后缀差别不大。Panic 与 Fatal 的区别在于 Panic 可以被捕获。

示例如下:

标签:

提交需求或反馈

Demand feedback