forked from sofixa/nomad_follower
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.go
174 lines (149 loc) · 3.88 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
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
package main
import (
"encoding/json"
"fmt"
"time"
"github.com/mitchellh/mapstructure"
)
const RFC3339Milli = "2006-01-02T15:04:05.000Z07:00"
// LogLevel provides compariable levels and a string representation.
type LogLevel int
const (
_ LogLevel = iota * 10
TRACE
DEBUG
INFO
ERROR
DEADLETTER
)
// String formats LogLevels as a string for readability.
func (l LogLevel) String() string {
var values = make(map[LogLevel]string)
values[TRACE] = "trace"
values[DEBUG] = "debug"
values[INFO] = "info"
values[ERROR] = "error"
values[DEADLETTER] = "deadletter"
s, ok := values[l]
if !ok {
return "unknown"
}
return s
}
func (l LogLevel) MarshalJSON() ([]byte, error) {
return []byte(`"` + l.String() + `"`), nil
}
// FollowerLog structures log output from Nomad Follower and dead letters.
type FollowerLog struct {
Name string `json:"name"`
Message string `json:"message"`
Datetime string `json:"datetime"`
Level LogLevel `json:"log_level,string"`
Data map[string]interface{} `json:"data"`
}
func (f FollowerLog) String() string {
// function to handle retries
marshal := func(l FollowerLog) ([]byte, error) {
return json.Marshal(l)
}
s, err := marshal(f)
if err != nil {
// error likely with data map
retryMsg := fmt.Sprintf(
"Original Msg: '%s' Original Level: '%s' Original Data: '%#v'",
f.Message,
f.Level,
f.Data,
)
retryLog := NewFollowerLog(
fmt.Sprintf("DeadLetter.%s", f.Name),
retryMsg,
DEADLETTER,
nil,
)
s, err = marshal(retryLog)
if err != nil {
// should really never get here
panic("Unparsable dead letter")
}
}
return string(s)
}
// NewFollowerLog creates a new FollowerLog and attempts to recover dead letters.
func NewFollowerLog(name, message string, level LogLevel, data map[string]interface{}) FollowerLog {
now := time.Now()
datetime := now.Format(RFC3339Milli)
l := FollowerLog{}
l.Name = name
l.Message = message
l.Level = level
l.Data = data
l.Datetime = datetime
return l
}
func NewDeadLetter(name, message string, rawLog NomadLog) FollowerLog {
data := make(map[string]interface{})
// TODO determine what to do if err occurs here
_ = mapstructure.Decode(rawLog, &data)
return NewFollowerLog(name, message, DEADLETTER, data)
}
// Logger acts as a single config point for emitting FollowerLogs as JSON.
type Logger struct {
verbosity LogLevel
}
func (l Logger) logAtLevel(name string, level LogLevel, message string) {
n := NewFollowerLog(
name,
message,
level,
make(map[string]interface{}),
)
if level >= l.verbosity {
fmt.Println(n)
}
}
func (l Logger) logFormatAtLevel(name string, level LogLevel, message string, f ...interface{}) {
msg := fmt.Sprintf(message, f...)
l.logAtLevel(name, level, msg)
}
func (l Logger) Trace(name, message string) {
l.logAtLevel(name, TRACE, message)
}
func (l Logger) Tracef(name, message string, f ...interface{}) {
l.logFormatAtLevel(name, TRACE, message, f...)
}
func (l Logger) Debug(name, message string) {
l.logAtLevel(name, DEBUG, message)
}
func (l Logger) Debugf(name, message string, f ...interface{}) {
l.logFormatAtLevel(name, DEBUG, message, f...)
}
func (l Logger) Info(name, message string) {
l.logAtLevel(name, INFO, message)
}
func (l Logger) Infof(name, message string, f ...interface{}) {
l.logFormatAtLevel(name, INFO, message, f...)
}
func (l Logger) Error(name, message string) {
l.logAtLevel(name, ERROR, message)
}
func (l Logger) Errorf(name, message string, f ...interface{}) {
l.logFormatAtLevel(name, ERROR, message, f...)
}
func (l Logger) DeadLetter(name string, rawLog NomadLog, message string) {
n := NewDeadLetter(
name,
message,
rawLog,
)
fmt.Println(n)
}
func (l Logger) DeadLetterf(name string, rawLog NomadLog, message string, f ...interface{}) {
msg := fmt.Sprintf(message, f...)
n := NewDeadLetter(
name,
msg,
rawLog,
)
fmt.Println(n)
}