-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoluent.go
102 lines (82 loc) · 1.8 KB
/
goluent.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
package goluent
import (
"fmt"
"github.com/fluent/fluent-logger-golang/fluent"
stdLog "log"
"os"
)
const (
infoLog severity = iota
warningLog
errorLog
fatalLog
numSeverity = 4
)
var severityName = []string{
infoLog: "INFO",
warningLog: "WARNING",
errorLog: "ERROR",
fatalLog: "FATAL",
}
var hostname string
type severity int8
func print(s severity, args ...interface{}) {
//connect fluent server
f, err := fluent.New(fluent.Config{
FluentPort: 24224,
FluentHost: "localhost",
TagPrefix: "goluent." + getHostname(),
})
defer f.Close()
message := fmt.Sprint(args...)
stdLog.Println(message)
if err == nil {
f.Post(severityName[s], map[string]string{"message": message})
}
}
func printf(s severity, format string, args ...interface{}) {
//connect fluent server
f, err := fluent.New(fluent.Config{
FluentPort: 24224,
FluentHost: "localhost",
TagPrefix: "goluent." + getHostname(),
})
defer f.Close()
message := fmt.Sprintf(format, args...)
stdLog.Println(message)
if err == nil {
f.Post(severityName[s], map[string]string{"message": message})
}
}
func Info(args ...interface{}) {
print(infoLog, args...)
}
func Infof(format string, args ...interface{}) {
printf(infoLog, format, args...)
}
func Warning(args ...interface{}) {
print(warningLog, args...)
}
func Warningf(format string, args ...interface{}) {
printf(warningLog, format, args...)
}
func Error(args ...interface{}) {
print(errorLog, args...)
}
func Errorf(format string, args ...interface{}) {
printf(errorLog, format, args...)
}
func Fatal(args ...interface{}) {
print(fatalLog, args...)
os.Exit(255)
}
func Fatalf(format string, args ...interface{}) {
printf(fatalLog, format, args...)
os.Exit(255)
}
func getHostname() string {
if hostname == "" {
hostname, _ = os.Hostname()
}
return hostname
}