-
Notifications
You must be signed in to change notification settings - Fork 1
/
context.go
185 lines (158 loc) · 4.98 KB
/
context.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
175
176
177
178
179
180
181
182
183
184
185
// This file is part of graze/golang-service
//
// Copyright (c) 2016 Nature Delivered Ltd. <https://www.graze.com>
//
// For the full copyright and license information, please view the LICENSE
// file that was distributed with this source code.
//
// license: https://github.com/graze/golang-service/blob/master/LICENSE
// link: https://github.com/graze/golang-service
package log
import (
"context"
"io"
"os"
"github.com/Sirupsen/logrus"
)
var (
logEntry = New()
appName = "LOG_APPLICATION"
envName = "ENVIRONMENT"
levelName = "LOG_LEVEL"
)
// key is a type to ensure unique key for context
type key int
// LogKey is the key used for context
const logKey key = iota
// KV is a shorthand for logrus.Fields so less text is required to be typed:
//
// log.With(log.KV{"k":"v"})
type KV logrus.Fields
// FieldLogger represents a Logging FieldLogger
type FieldLogger interface {
NewContext(ctx context.Context) context.Context
AppendContext(ctx context.Context, fields KV) context.Context
Ctx(ctx context.Context) FieldLogger
With(fields KV) FieldLogger
Err(err error) FieldLogger
Fields() KV
Debugf(format string, args ...interface{})
Infof(format string, args ...interface{})
Printf(format string, args ...interface{})
Warnf(format string, args ...interface{})
Warningf(format string, args ...interface{})
Errorf(format string, args ...interface{})
Fatalf(format string, args ...interface{})
Panicf(format string, args ...interface{})
Debug(args ...interface{})
Info(args ...interface{})
Print(args ...interface{})
Warn(args ...interface{})
Warning(args ...interface{})
Error(args ...interface{})
Fatal(args ...interface{})
Panic(args ...interface{})
Debugln(args ...interface{})
Infoln(args ...interface{})
Println(args ...interface{})
Warnln(args ...interface{})
Warningln(args ...interface{})
Errorln(args ...interface{})
Fatalln(args ...interface{})
Panicln(args ...interface{})
}
// Logger represents a struct that can modify the output of a log
type Logger interface {
SetOutput(out io.Writer)
SetLevel(level logrus.Level)
Level() logrus.Level
SetFormatter(formatter logrus.Formatter)
AddHook(hook logrus.Hook)
}
// LoggerEntry is a logging context that can be passed around
type LoggerEntry struct {
*logrus.Entry
}
// NewContext returns the provided context with this LoggerEntry added
func (c *LoggerEntry) NewContext(ctx context.Context) context.Context {
return context.WithValue(ctx, logKey, c.Fields())
}
// Ctx will use any logging context contained in context.Context to append to the current log context
func (c *LoggerEntry) Ctx(ctx context.Context) FieldLogger {
if fields, ok := ctx.Value(logKey).(KV); ok {
return c.With(fields)
}
return c.With(KV{})
}
// AppendContext will create a new context.Context based on ctx with the fields appended
func (c *LoggerEntry) AppendContext(ctx context.Context, fields KV) context.Context {
return c.Ctx(ctx).With(fields).NewContext(ctx)
}
// With creates a new LoggerEntry and adds the fields to it
func (c *LoggerEntry) With(fields KV) FieldLogger {
// type conversion of same type without refection
data := make(logrus.Fields, len(fields))
for k, v := range fields {
data[k] = v
}
entry := c.Entry.WithFields(data)
return &LoggerEntry{entry}
}
// Err adds an error and returns a new LoggerEntry
func (c *LoggerEntry) Err(err error) FieldLogger {
entry := c.Entry.WithError(err)
return &LoggerEntry{entry}
}
// Fields will return the current fields attached to a context
func (c *LoggerEntry) Fields() (fields KV) {
fields = make(KV, len(c.Entry.Data))
for k, v := range c.Entry.Data {
fields[k] = v
}
return
}
// SetOutput changes the output of the current context
func (c *LoggerEntry) SetOutput(out io.Writer) {
c.Logger.Out = out
}
// SetFormatter will change the formatter for the current context
func (c *LoggerEntry) SetFormatter(formatter logrus.Formatter) {
c.Logger.Formatter = formatter
}
// SetLevel changes the default logging level of the current context
func (c *LoggerEntry) SetLevel(level logrus.Level) {
c.Logger.Level = level
}
// Level returns the current logging level this context will log at
func (c *LoggerEntry) Level() (level logrus.Level) {
return c.Logger.Level
}
// AddHook will add a hook to the current context
func (c *LoggerEntry) AddHook(hook logrus.Hook) {
c.Logger.Hooks.Add(hook)
}
// New creates a new FieldLogger with a new Logger (formatter, level, output, hooks)
func New() (entry *LoggerEntry) {
base := logrus.New()
logger := &LoggerEntry{logrus.NewEntry(base)}
fields := make(KV)
if app := os.Getenv(appName); app != "" {
fields["app"] = app
}
if env := os.Getenv(envName); env != "" {
fields["env"] = env
}
if level := os.Getenv(levelName); level != "" {
if l, err := logrus.ParseLevel(level); err == nil {
logger.SetLevel(l)
} else {
logger.Err(err).With(KV{
"module": "log_initialisation",
"tag": "log_new_failed",
"logLevel": level,
}).Error("The supplied log level is invalid")
}
}
entry, _ = logger.With(fields).(*LoggerEntry)
return
}