-
Notifications
You must be signed in to change notification settings - Fork 2
/
backend.go
137 lines (117 loc) · 3.25 KB
/
backend.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
package zerolog
import (
"fmt"
"io"
"os"
"strings"
"github.com/rs/zerolog"
logpkg "github.com/rs/zerolog/log"
"github.com/tliron/kutil/logging"
"github.com/tliron/kutil/terminal"
"github.com/tliron/kutil/util"
)
func init() {
backend := NewBackend()
backend.Configure(0, nil)
logging.SetBackend(backend)
}
const LOG_FILE_WRITE_PERMISSIONS = 0600
const TIME_FORMAT = "2006/01/02 15:04:05.000"
//
// Backend
//
// Note: using kutil to wrap zerolog will circumvent its primary optimization, which is for high
// performance and low resource use due to aggressively avoiding allocations. If you need that
// optimization then you should use zerolog's API directly.
type Backend struct {
writer io.Writer
hierarchy *logging.Hierarchy
}
func NewBackend() *Backend {
return &Backend{
hierarchy: logging.NewMaxLevelHierarchy(),
}
}
// logging.Backend interface
func (self *Backend) Configure(verbosity int, path *string) {
maxLevel := logging.VerbosityToMaxLevel(verbosity)
if maxLevel == logging.None {
self.writer = io.Discard
self.hierarchy.SetMaxLevel(nil, logging.None)
logpkg.Logger = zerolog.New(self.writer)
zerolog.SetGlobalLevel(zerolog.Disabled)
} else {
if path != nil {
if file, err := os.OpenFile(*path, os.O_WRONLY|os.O_CREATE|os.O_APPEND, LOG_FILE_WRITE_PERMISSIONS); err == nil {
util.OnExitError(file.Close)
self.writer = file
logpkg.Logger = zerolog.New(self.writer)
} else {
util.Failf("log file error: %s", err.Error())
}
} else {
self.writer = os.Stderr
if terminal.Colorize {
logpkg.Logger = zerolog.New(zerolog.ConsoleWriter{
Out: self.writer,
TimeFormat: TIME_FORMAT,
})
} else {
logpkg.Logger = zerolog.New(zerolog.ConsoleWriter{
Out: self.writer,
TimeFormat: TIME_FORMAT,
NoColor: true,
})
}
}
zerolog.TimeFieldFormat = zerolog.TimeFormatUnixMicro
logpkg.Logger = logpkg.With().Timestamp().Logger()
self.hierarchy.SetMaxLevel(nil, maxLevel)
}
}
// logging.Backend interface
func (self *Backend) GetWriter() io.Writer {
return self.writer
}
// logging.Backend interface
func (self *Backend) NewMessage(name []string, level logging.Level, depth int) logging.Message {
if self.AllowLevel(name, level) {
context := logpkg.With()
if name := strings.Join(name, "."); len(name) > 0 {
context = context.Str("name", name)
}
logger := context.Logger()
var event *zerolog.Event
switch level {
case logging.Critical:
event = logger.Error()
case logging.Error:
event = logger.Error()
case logging.Warning:
event = logger.Warn()
case logging.Notice:
event = logger.Info()
case logging.Info:
event = logger.Debug()
case logging.Debug:
event = logger.Trace()
default:
panic(fmt.Sprintf("unsupported level: %d", level))
}
return NewMessage(event)
} else {
return nil
}
}
// logging.Backend interface
func (self *Backend) AllowLevel(name []string, level logging.Level) bool {
return self.hierarchy.AllowLevel(name, level)
}
// logging.Backend interface
func (self *Backend) SetMaxLevel(name []string, level logging.Level) {
self.hierarchy.SetMaxLevel(name, level)
}
// logging.Backend interface
func (self *Backend) GetMaxLevel(name []string) logging.Level {
return self.hierarchy.GetMaxLevel(name)
}