-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfactories.go
106 lines (98 loc) · 2.94 KB
/
factories.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
package log
import (
"fmt"
"os"
"testing"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
"go.uber.org/zap/zaptest"
)
// NewTextLogger builds a plain-text based logger and returns it to the caller. If the
// log directory has not been created then this is created here. An error may be returned
// if there was a problem in building the underlying logger or if the directory could not
// be created.
func NewTextLogger(directory, name string) (Log, error) {
_, err := os.Stat(directory)
if os.IsNotExist(err) {
if err := os.MkdirAll(directory, 0755); err != nil {
return nil, err
}
}
config := zapConfig(
"console",
fmt.Sprintf("%s/%s", directory, name),
)
logger, err := config.Build()
if err != nil {
return nil, err
}
return &log{
logger: logger,
attached: make([]Log, 0),
directory: directory,
fileName: name,
}, nil
}
// NewTestLogger returns a logger that has been configured for testing. None of the
// logs will be written anywhere and instead just discarded.
func NewTestLogger(t *testing.T) Log {
return &log{
logger: zaptest.NewLogger(t, zaptest.Level(zap.FatalLevel)),
attached: make([]Log, 0),
}
}
// NewJSONLogger builds a JSON based logger and returns it to the caller. If the log
// directory has not been created then this is created here. An error may be returned
// if there was a problem in building the underlying logger or if the directory could
// not be created.
func NewJSONLogger(directory, name string) (Log, error) {
_, err := os.Stat(directory)
if os.IsNotExist(err) {
if err := os.MkdirAll(directory, 0755); err != nil {
return nil, err
}
}
config := zapConfig(
"json",
fmt.Sprintf("%s/%s", directory, name),
)
logger, err := config.Build()
if err != nil {
return nil, err
}
return &log{
logger: logger,
attached: make([]Log, 0),
directory: directory,
fileName: name,
}, nil
}
// zapConfig returns a default zap.Config that can be shared between loggers for a
// more consistent experience when reading them. The type of encoding and the
// output path must be provided as parameters
func zapConfig(encoding, filePath string) zap.Config {
zap.NewProduction()
return zap.Config{
OutputPaths: []string{filePath},
Encoding: encoding,
Level: zap.NewAtomicLevelAt(zap.DebugLevel),
EncoderConfig: zapcore.EncoderConfig{
MessageKey: "message",
LevelKey: "level",
TimeKey: "timestamp",
EncodeTime: zapcore.TimeEncoderOfLayout("2006-01-02 15:04:05"),
EncodeLevel: zapcore.CapitalLevelEncoder,
EncodeCaller: zapcore.ShortCallerEncoder,
},
}
}
// Field returns a zap.Field which can be used for adding custom properties to
// a log entry.
func Field(key string, value interface{}) zap.Field {
return zap.Any(key, value)
}
// ErrField returns a zap.Field which is pre-configured with the key "cause" and
// with the value being the error message from the provided error.
func ErrField(err error) zap.Field {
return Field("cause", err.Error())
}