-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlogProcess.go
More file actions
74 lines (58 loc) · 1.1 KB
/
logProcess.go
File metadata and controls
74 lines (58 loc) · 1.1 KB
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
package main
import (
"bufio"
"fmt"
"os"
"strings"
"time"
)
type logProcess struct {
read Reader
write Writer
wc chan []byte
rc chan []byte
}
type Reader interface {
Read(rc chan []byte)
}
type Writer interface {
Write(wc chan []byte)
}
type ReaderFromFile struct {
path string
}
type WriteInfluxDB struct {
influxDBDsn string
}
func (w *WriteInfluxDB) Write(wc chan []byte) {
fmt.Println(<-wc)
}
func (r *ReaderFromFile) Read(rc chan []byte) {
// 打开文件
f, err := os.OpenFile(r.path)
if err != nil {
panic(fmt.Sprintf("open file error:%s", err.Error()))
}
//从文件末尾开始逐行 读取文件内容
rd := bufio.NewReader(f)
line , err := rd.ReadBytes(\n)
line := "message"
rc <- line
}
func (l *logProcess) Process() {
//解析模块
data := <-l.rc
l.wc <- strings.ToUpper(data)
}
func main() {
lp := &logProcess{
read: &ReaderFromFile{path: "test.log"},
write: &WriteInfluxDB{influxDBDsn: "username&password.."},
rc: make(chan string),
wc: make(chan string),
}
go lp.read.Read(lp.rc)
go lp.Process()
go lp.write.Write(lp.wc)
time.Sleep(1)
}