I tried to get SQL information from log files using regular expressions
The information I need is the running time of the SQL, the SQL statement, the parameters of the SQL
Here's my code:
package main
import (
"os"
"fmt"
"io/ioutil"
"regexp"
)
func main(){
file,err:=os.OpenFile("./log.txt",os.O_RDWR,0755)
if err !=nil{
fmt.Println(err)
}
b,err:=ioutil.ReadAll(file)
str:=string(b)
r,err:=regexp.Compile(`\[ORM\][\w\W]+?(\d*.\d*ms|\d*.\d*s)\][ -]+\[([\w\W]+?)\]`)
s:=r.FindAllStringSubmatch(str,-1)
fmt.Println(s[0][3])
fmt.Println(s[1][3])
}
This is my log sample
[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] - `1` `ASDFASDF` 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=? ] - `111` `qwqwqw`
I hope so Print out the
`1` `ASDFASDF`
`111` `qwqwqw`
I'm now left with the last argument that I can't get
These parameters may or may not be multiple、You can also have too many arguments wrapping around the file
I have tried this myself:
r,err:=regexp.Compile(`\[ORM\][\w\W]+?(\d*.\d*ms|\d*.\d*s)\][ -]+\[([\w\W]+?)\][ -]*((\W\w*\W{1,2})*)`)
r,err:=regexp.Compile(`\[ORM\][\w\W]+?(\d*.\d*ms|\d*.\d*s)\][ -]+\[([\w\W]+?)\][- ]*([^
]*)`)