-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlog_sender.go
58 lines (54 loc) · 1.17 KB
/
log_sender.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// Copyright (c) 2023 William Dode. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for details.
package webo
import (
"bytes"
"fmt"
"net/http"
"net/url"
"sync"
"time"
)
type LogSender struct {
sync.RWMutex
name string
poste string
version string
url_log string
ch chan []byte
last_send time.Time
}
func NewLogSender(name, poste, version, url_log string) *LogSender {
ch := make(chan []byte, 555)
ls := &LogSender{name: name, poste: poste, version: version, url_log: url_log, ch: ch}
go ls.go_send()
return ls
}
func (w *LogSender) go_send() {
buf := &bytes.Buffer{}
tickChan := time.NewTicker(time.Second * 60).C
for {
select {
case <-tickChan:
if buf.Len() > 0 {
w.send(buf.String())
buf.Reset()
}
case b := <-w.ch:
buf.Write(b)
}
}
}
func (w *LogSender) Write(output []byte) (int, error) {
w.ch <- output
return len(output), nil
}
func (w *LogSender) send(buf string) {
_, err := http.PostForm(w.url_log+"/"+w.name+"_"+w.version+"/archive", url.Values{"name": {w.name},
"version": {w.version},
"poste": {w.poste},
"msg": {buf}})
if err != nil {
fmt.Println(err)
}
}