doudang4568 2018-08-04 09:32
浏览 379
已采纳

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

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

  • 写回答

1条回答 默认 最新

  • dongxiang3205 2018-08-04 09:43
    关注

    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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 Matlab打开默认名称带有/的光谱数据
  • ¥50 easyExcel模板 动态单元格合并列
  • ¥15 res.rows如何取值使用
  • ¥15 在odoo17开发环境中,怎么实现库存管理系统,或独立模块设计与AGV小车对接?开发方面应如何设计和开发?请详细解释MES或WMS在与AGV小车对接时需完成的设计和开发
  • ¥15 CSP算法实现EEG特征提取,哪一步错了?
  • ¥15 游戏盾如何溯源服务器真实ip?需要30个字。后面的字是凑数的
  • ¥15 vue3前端取消收藏的不会引用collectId
  • ¥15 delphi7 HMAC_SHA256方式加密
  • ¥15 关于#qt#的问题:我想实现qcustomplot完成坐标轴
  • ¥15 下列c语言代码为何输出了多余的空格