项目初始化
This commit is contained in:
24
cfg.go
Normal file
24
cfg.go
Normal file
@@ -0,0 +1,24 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"gopkg.in/ini.v1"
|
||||
"log"
|
||||
)
|
||||
|
||||
type Cfg struct {
|
||||
Dir string `ini:"dir"` // "logs"
|
||||
Match string `ini:"match"` // "warn"
|
||||
Url string `ini:"url"` // ""
|
||||
Key string `ini:"key"` // ""
|
||||
Info string `ini:"info"` // ""
|
||||
}
|
||||
|
||||
var C = Cfg{}
|
||||
|
||||
func init() {
|
||||
load, err := ini.Load("log.ini")
|
||||
if err != nil {
|
||||
log.Fatal(err)
|
||||
}
|
||||
load.MapTo(&C)
|
||||
}
|
||||
5
go.mod
Normal file
5
go.mod
Normal file
@@ -0,0 +1,5 @@
|
||||
module log-node
|
||||
|
||||
go 1.21
|
||||
|
||||
require gopkg.in/ini.v1 v1.67.0 // indirect
|
||||
2
go.sum
Normal file
2
go.sum
Normal file
@@ -0,0 +1,2 @@
|
||||
gopkg.in/ini.v1 v1.67.0 h1:Dgnx+6+nfE+IfzjUEISNeydPJh9AXNNsWbGP9KzCsOA=
|
||||
gopkg.in/ini.v1 v1.67.0/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
|
||||
5
log.ini
Normal file
5
log.ini
Normal file
@@ -0,0 +1,5 @@
|
||||
dir=logs
|
||||
match=warn
|
||||
url=http://192.168.102.21:6801/upload_v2
|
||||
key=测试
|
||||
info=测试
|
||||
2
logs/warn.log
Normal file
2
logs/warn.log
Normal file
@@ -0,0 +1,2 @@
|
||||
{asdasd,asdasd}
|
||||
asdasdasd.asdsad
|
||||
48
main.go
Normal file
48
main.go
Normal file
@@ -0,0 +1,48 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"regexp"
|
||||
)
|
||||
|
||||
func main() {
|
||||
dir := C.Dir
|
||||
match := C.Match
|
||||
url := C.Url
|
||||
key := C.Key
|
||||
info := C.Info
|
||||
|
||||
regex := regexp.MustCompile(match)
|
||||
|
||||
dirs, err := os.ReadDir(dir)
|
||||
if err != nil {
|
||||
fmt.Println(err)
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range dirs {
|
||||
if regex.MatchString(item.Name()) {
|
||||
file, err := os.Open(filepath.Join(dir, item.Name()))
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
var logTexts []string
|
||||
ReadAfterSeq(file, 0, '\n', func(bytes []byte) {
|
||||
logTexts = append(logTexts, string(bytes))
|
||||
})
|
||||
file.Close()
|
||||
for _, text := range logTexts {
|
||||
err := requestDingApi(url, message{
|
||||
Text: text,
|
||||
Filename: item.Name(),
|
||||
Key: key,
|
||||
Info: info,
|
||||
})
|
||||
fmt.Println(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
53
reader.go
Normal file
53
reader.go
Normal file
@@ -0,0 +1,53 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"io"
|
||||
)
|
||||
|
||||
func ReadAfterSeq(r io.Reader, size int64, seq byte, callback func([]byte)) {
|
||||
var (
|
||||
bytes1 []byte
|
||||
pointer int64
|
||||
)
|
||||
for {
|
||||
buf := make([]byte, 200)
|
||||
n, err := r.Read(buf)
|
||||
int64n := int64(n)
|
||||
|
||||
if err == io.EOF {
|
||||
break
|
||||
}
|
||||
var begin int64 = -1
|
||||
// 若之前指针就大于设置长度开始指向头
|
||||
if pointer >= size {
|
||||
begin = 0
|
||||
} else if pointer+int64n >= size { // 若尾巴大于
|
||||
begin = size - pointer
|
||||
}
|
||||
pointer += int64n
|
||||
if begin == -1 {
|
||||
continue
|
||||
}
|
||||
|
||||
// 查看当前片段是否存在分隔符
|
||||
for _, b := range buf[begin:n] {
|
||||
// if b == 13 {
|
||||
// continue
|
||||
// }
|
||||
if b == seq {
|
||||
bytes2 := bytes.TrimSpace(bytes1)
|
||||
if len(bytes2) > 0 {
|
||||
callback(bytes2)
|
||||
bytes1 = nil
|
||||
}
|
||||
continue
|
||||
}
|
||||
bytes1 = append(bytes1, b)
|
||||
}
|
||||
|
||||
}
|
||||
if len(bytes1) > 0 {
|
||||
callback(bytes1)
|
||||
}
|
||||
}
|
||||
34
up.go
Normal file
34
up.go
Normal file
@@ -0,0 +1,34 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io/ioutil"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
type message struct {
|
||||
Text string `form:"text" json:"text"`
|
||||
Filename string `form:"filename" json:"filename"`
|
||||
Key string `form:"key" json:"key"`
|
||||
Info string `form:"info" json:"info"`
|
||||
}
|
||||
|
||||
func requestDingApi(url string, msg message) error {
|
||||
b, err := json.Marshal(msg)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
resp, err := http.Post(url, "application/json", bytes.NewBuffer(b))
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := ioutil.ReadAll(resp.Body)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
fmt.Println("body:%v", string(body))
|
||||
return err
|
||||
}
|
||||
Reference in New Issue
Block a user