-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
80 lines (68 loc) · 1.77 KB
/
logger.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
package main
import (
"encoding/json"
"fmt"
"strconv"
"time"
)
type LogLevel int
const (
LogLevel_VERBOSE = 5
LogLevel_DEBUG = 4
LogLevel_INFO = 3
LogLevel_WARN = 2
LogLevel_ERROR = 1
)
type LogMessage struct {
Timestamp time.Time
Caller string
Level LogLevel
MessageFormat string
Error error
}
func LogForwarder(config *Config, msg LogMessage, messageObjects ...interface{}) {
if config != nil && config.LogLevel >= msg.Level {
return
}
type logMessageRaw struct {
Timestamp time.Time
Version string
LogLevelLabel string
Caller string
Level LogLevel
Message string
Error error
}
jsonMessage := logMessageRaw{
Timestamp: msg.Timestamp,
Version: GetMyVersion(),
LogLevelLabel: "",
Caller: msg.Caller,
Level: msg.Level,
Error: msg.Error,
}
if messageObjects == nil || len(messageObjects) == 0 {
jsonMessage.Message = msg.MessageFormat
} else {
jsonMessage.Message = fmt.Sprintf(msg.MessageFormat, messageObjects[:]...)
}
if jsonMessage.Timestamp.IsZero() {
jsonMessage.Timestamp = time.Now()
}
switch jsonMessage.Level {
case LogLevel_VERBOSE:
jsonMessage.LogLevelLabel = "[VERBOSE]"
case LogLevel_DEBUG:
jsonMessage.LogLevelLabel = "[DEBUG ]"
case LogLevel_WARN:
jsonMessage.LogLevelLabel = "[WARN ]"
case LogLevel_ERROR:
jsonMessage.LogLevelLabel = "[ERROR ]"
case LogLevel_INFO:
jsonMessage.LogLevelLabel = "[INFO ]"
default:
panic("Unknown log level " + strconv.FormatInt(int64(msg.Level), 10) + " for message " + fmt.Sprintf("%v", msg.MessageFormat) + " from " + fmt.Sprintf("%v", msg.Caller))
}
jsonMessageJsonString, _ := json.Marshal(jsonMessage)
fmt.Println(string(jsonMessageJsonString))
}