influxdata / go-syslog

Blazing fast syslog parser
MIT License
476 stars 69 forks source link

我通过此类去解析 UDP 协议的内容,但无法生效。 #47

Closed flc1125 closed 1 year ago

flc1125 commented 1 year ago

Code

package main

import (
    "fmt"
    "github.com/davecgh/go-spew/spew"
    "github.com/influxdata/go-syslog/v3/rfc3164"
    "log"
    "net"
)

func main() {
    p := rfc3164.NewParser()
    p.WithBestEffort()
    msg, _ := p.Parse([]byte("<14>Dec 21 09:08:10 Flcs-MacBook-Pro.local tw8591_financial_fas_laravel[17753]: [2022-12-21 17:08:10] dev.INFO: aasdfasdf=====0  \\n"))

    spew.Config.DisableCapacities = true
    spew.Config.DisablePointerAddresses = true
    spew.Dump(msg)
    spew.Dump(msg.(*rfc3164.SyslogMessage).Message)
    // return

    // 创建一个 PacketConn 接口
    conn, err := net.ListenPacket("udp", ":12190")
    if err != nil {
        log.Fatal(err)
        return
    }
    defer conn.Close()

    // 接收数据
    buf := make([]byte, 1024)
    for {
        n, addr, err := conn.ReadFrom(buf)
        if err != nil {
            fmt.Println(err)
            continue
        }

        spew.Dump(buf)
        spew.Dump(string(buf[:n]))
        spew.Dump(msg, err)
        spew.Dump(addr)
        spew.Dump(n)

        // 解析数据
        msg, err := p.Parse([]byte(fmt.Sprintf("%s", buf[:n])))
        switch msg.(type) {
        case *rfc3164.SyslogMessage:
            spew.Dump(fmt.Sprintln("123123"))
            spew.Dump(msg.(*rfc3164.SyslogMessage))
        }
    }
}

Out

(*rfc3164.SyslogMessage)({
 Base: (syslog.Base) {
  Facility: (*uint8)(1),
  Severity: (*uint8)(6),
  Priority: (*uint8)(14),
  Timestamp: (*time.Time)(0000-12-21 09:08:10 +0000 UTC),
  Hostname: (*string)((len=22) "Flcs-MacBook-Pro.local"),
  Appname: (*string)((len=1) "f"),
  ProcID: (*string)((len=20) "[2022-12-21 17:08:10"),
  MsgID: (*string)(<nil>),
  Message: (*string)((len=51) "[2022-12-21 17:08:10] dev.INFO: aasdfasdf=====0  \\n")
 }
})
(*string)((len=51) "[2022-12-21 17:08:10] dev.INFO: aasdfasdf=====0  \\n")
([]uint8) (len=1024) {
 00000000  3c 31 34 3e 44 65 63 20  32 31 20 31 30 3a 33 35  |<14>Dec 21 10:35|
 00000010  3a 30 30 20 46 6c 63 73  2d 4d 61 63 42 6f 6f 6b  |:00 Flcs-MacBook|
 00000020  2d 50 72 6f 2e 6c 6f 63  61 6c 20 66 5b 32 31 31  |-Pro.local f[211|
 00000030  39 32 5d 3a 20 5b 32 30  32 32 2d 31 32 2d 32 31  |92]: [2022-12-21|
 00000040  20 31 38 3a 33 35 3a 30  30 5d 20 64 65 76 2e 49  | 18:35:00] dev.I|
 00000050  4e 46 4f 3a 20 61 61 73  64 66 61 73 64 66 3d 3d  |NFO: aasdfasdf==|
 00000060  3d 3d 3d 30 20 20 0a 00  00 00 00 00 00 00 00 00  |===0  ..........|
 00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000080  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000090  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000000a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000000b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000000c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000000d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000000e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000000f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000110  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000120  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000130  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000140  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000150  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000160  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000170  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000180  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000190  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000001a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000001b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000200  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000210  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000220  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000230  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000240  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000250  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000260  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000270  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000280  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000290  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000002a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000002b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000002c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000002d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000002e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000002f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000300  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000310  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000320  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000330  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000340  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000350  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000360  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000370  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000380  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 00000390  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000003a0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000003b0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000003c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000003d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000003e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
 000003f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00  |................|
}
(string) (len=103) "<14>Dec 21 10:35:00 Flcs-MacBook-Pro.local f[21192]: [2022-12-21 18:35:00] dev.INFO: aasdfasdf=====0  \n"
(*rfc3164.SyslogMessage)({
 Base: (syslog.Base) {
  Facility: (*uint8)(1),
  Severity: (*uint8)(6),
  Priority: (*uint8)(14),
  Timestamp: (*time.Time)(0000-12-21 09:08:10 +0000 UTC),
  Hostname: (*string)((len=22) "Flcs-MacBook-Pro.local"),
  Appname: (*string)((len=1) "f"),
  ProcID: (*string)((len=20) "[2022-12-21 17:08:10"),
  MsgID: (*string)(<nil>),
  Message: (*string)((len=51) "[2022-12-21 17:08:10] dev.INFO: aasdfasdf=====0  \\n")
 }
})
(interface {}) <nil>
(*net.UDPAddr)(127.0.0.1:52055)
(int) 103
(string) (len=7) "123123\n"
(*rfc3164.SyslogMessage)({
 Base: (syslog.Base) {
  Facility: (*uint8)(1),
  Severity: (*uint8)(6),
  Priority: (*uint8)(14),
  Timestamp: (*time.Time)(0000-12-21 10:35:00 +0000 UTC),
  Hostname: (*string)((len=22) "Flcs-MacBook-Pro.local"),
  Appname: (*string)((len=1) "f"),
  ProcID: (*string)((len=20) "[2022-12-21 18:35:00"),
  MsgID: (*string)(<nil>),
  Message: (*string)(<nil>)
 }
})
flc1125 commented 1 year ago
p.Parse([]byte(fmt.Sprintf("%s", bytes.TrimRight(buf[:n], "\x0a"))))

就好了,但我想知道原因……