-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.go
87 lines (73 loc) · 2.13 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
package logger
import (
"github.com/syllabix/logger/console"
"github.com/syllabix/logger/encode"
"github.com/syllabix/logger/internal/registry"
"github.com/syllabix/logger/json"
"github.com/syllabix/logger/mode"
"go.uber.org/zap"
"go.uber.org/zap/zapcore"
)
func consoleConfig() console.Config {
config := console.Config{
Mode: global.mode,
}
if global.mode == mode.Production {
config.Config = encode.ProConsoleConfig
} else {
config.Config = encode.DevConsoleConfig
}
return config
}
// New returns an instance of a logger configured via the logger package
// global options
func New() *zap.Logger {
level := registry.Get(pkgname())
// configure console encoder
cEncoder := console.NewEncoder(consoleConfig())
cout := zapcore.AddSync(global.csink)
core := zapcore.NewCore(cEncoder, cout, level)
// if a json sink has been set, configure it
// with the zapcore JSON Encoder, and Tee the console
// encoder with it
if global.jsink != nil {
jEncoder := json.NewEncoder(encode.JSONConfig)
rsink := zapcore.AddSync(global.jsink)
core = zapcore.NewTee(
core,
zapcore.NewCore(jEncoder, rsink, level),
)
}
// TODO: determine the most efficient way to allocate
// core = zapcore.NewSampler(
// core,
// 500*time.Millisecond,
// 10, // first
// 50, // thereafter
// )
logger := zap.New(core,
zap.AddCaller(),
zap.AddStacktrace(zap.PanicLevel)).
With(zap.String("@source_host", hostname()))
logger = logger.With(zap.Namespace("@fields"))
if len(global.appname) > 0 {
logger = logger.With(zap.String("application", global.appname))
}
return logger
}
// SetLevelForPackage will set the log level for all instances
// of a logger in the provided package, returning an error if the
// package name provided does not exist in the registry
func SetLevelForPackage(pkg string, level zapcore.Level) error {
return registry.Set(registry.Package(pkg), level)
}
// GetPackages retuns all package names that logger instances
// have been created in
func GetPackages() []string {
pkgs := registry.GetPackages()
strpkgs := make([]string, len(pkgs))
for i := range pkgs {
strpkgs[i] = string(pkgs[i])
}
return strpkgs
}