/
lokiJson.go
122 lines (107 loc) · 2.71 KB
/
lokiJson.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package loki
import (
"bufio"
"encoding/json"
"fmt"
"os"
"runtime"
"time"
)
//LokiJson logs in json format
type LokiJson struct {
loglevel int
logFile *os.File
}
//JsonLog struct defines the structure of a log message
type JsonLog struct {
Timestamp time.Time `json:"timestamp"`
Message string `json:"message"`
Error error `json:"error,omitempty"`
Level string `json:"level"`
Caller Caller `json:"caller"`
Context []*json.RawMessage `json:"context,omitempty"`
}
//Caller defines json structure to store data on where the log message orginated
type Caller struct {
Function string `json:"function"`
Line int `json:"line"`
File string `json:"file"`
}
//NewJsonLogger return a new loki logger that logs in json format.
//By default writes to stdout
func NewJsonLogger() *LokiJson {
return &LokiJson{
loglevel: INFO,
logFile: os.Stdout,
}
}
//Set the logging level
func (l *LokiJson) Set(level int) {
if level >= 1 && level <= 6 {
l.loglevel = level
} else {
l.loglevel = INFO
}
}
func (l *LokiJson) WriteFile(path string) error {
var err error
l.logFile, err = os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return fmt.Errorf(`failed to open "%s" for writing`, path)
}
return nil
}
//Debug write a log with level DEBUG
func (l *LokiJson) Debug(mesg string, args ...interface{}) {
if l.loglevel <= DEBUG {
l.logMessage(mesg, "DEBUG", args...)
}
}
//Info write a log with level INFO
func (l *LokiJson) Info(mesg string, args ...interface{}) {
if l.loglevel <= INFO {
l.logMessage(mesg, "INFO", args...)
}
}
//Warn write a log with level WARN
func (l *LokiJson) Warn(mesg string, args ...interface{}) {
if l.loglevel <= WARN {
l.logMessage(mesg, "WARN", args...)
}
}
//Error write a log with level ERROR
func (l *LokiJson) Error(mesg string, args ...interface{}) {
if l.loglevel <= ERROR {
l.logMessage(mesg, "ERROR", args...)
}
}
//Fatal write a log with level FATAL
func (l *LokiJson) Fatal(mesg string, args ...interface{}) {
if l.loglevel <= FATAL {
l.logMessage(mesg, "FATAL", args...)
}
}
func (l *LokiJson) logMessage(mesg, level string, args ...interface{}) {
pc, file, line, _ := runtime.Caller(2)
function := runtime.FuncForPC(pc).Name()
j := JsonLog{
Timestamp: time.Now(),
Message: mesg,
Level: level,
Caller: Caller{
Function: function,
Line: line,
File: file,
},
}
writer := bufio.NewWriter(l.logFile)
_, err := writer.WriteString(l.structToJson(&j))
if err != nil {
fmt.Println("failed to write to buffer")
}
writer.Flush()
}
func (l *LokiJson) structToJson(s *JsonLog) string {
b, _ := json.Marshal(s)
return string(b) + "\n"
}