This repository has been archived by the owner on Oct 23, 2021. It is now read-only.
/
printer.go
146 lines (113 loc) · 5.81 KB
/
printer.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
// Copyright 2019 Smart-Edge.com, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package log
import (
"fmt"
"log/syslog"
)
// Printer formats and writes logs conditionally based on the current priority
// level.
type Printer struct {
Format func(frmt string, a ...interface{}) string
Write func(lvl syslog.Priority, msg string)
WriteSyslog func(lvl syslog.Priority, msg string)
}
// WithField returns a Printer tagged with a single field.
func (l *Logger) WithField(key string, value interface{}) Printer {
return l.WithFields(map[string]interface{}{key: value})
}
// WithFields returns a Printer tagged with multiple fields.
func (l *Logger) WithFields(kvs map[string]interface{}) Printer {
return Printer{
Format: l.format(kvs),
Write: l.write,
WriteSyslog: l.writeSyslog,
}
}
// Printf writes message with severity and set facility to output and syslog if connected.
func (p Printer) Printf(lvl syslog.Priority, frmt string, a ...interface{}) {
// get formatter and writer with defaults
formatter := p.Format
if formatter == nil {
if frmt == "" {
formatter = func(_ string, a ...interface{}) string { return fmt.Sprint(a...) }
} else {
formatter = fmt.Sprintf
}
}
write := p.Write
if write == nil {
write = (&Logger{priority: lvl}).write
}
writeSyslog := p.WriteSyslog
if writeSyslog == nil {
writeSyslog = (&Logger{}).writeSyslog
}
// write formatted string
msg := formatter(frmt, a...)
write(lvl, msg)
writeSyslog(lvl, msg)
}
// Print writes message with severity and set facility to output and syslog if connected.
func (p Printer) Print(lvl syslog.Priority, a ...interface{}) { p.Printf(lvl, "", a...) }
// Println writes message with severity and set facility to output and syslog if connected.
func (p Printer) Println(lvl syslog.Priority, a ...interface{}) { p.Print(lvl, append(a, "\n")...) }
// Debug writes DEBUG message to output and syslog if connected.
func (p Printer) Debug(a ...interface{}) { p.Debugf("", a...) }
// Debugln writes DEBUG message to output and syslog if connected.
func (p Printer) Debugln(a ...interface{}) { p.Debugf("", a...) }
// Debugf writes formatted DEBUG message to output and syslog if connected.
func (p Printer) Debugf(frmt string, a ...interface{}) { p.Printf(syslog.LOG_DEBUG, frmt, a...) }
// Info writes INFO message to output and syslog if connected.
func (p Printer) Info(a ...interface{}) { p.Infof("", a...) }
// Infoln writes INFO message to output and syslog if connected.
func (p Printer) Infoln(a ...interface{}) { p.Infof("", a...) }
// Infof writes formatted INFO message to output and syslog if connected.
func (p Printer) Infof(frmt string, a ...interface{}) { p.Printf(syslog.LOG_INFO, frmt, a...) }
// Notice writes NOTICE message to output and syslog if connected.
func (p Printer) Notice(a ...interface{}) { p.Noticef("", a...) }
// Noticeln writes NOTICE message to output and syslog if connected.
func (p Printer) Noticeln(a ...interface{}) { p.Noticef("", a...) }
// Noticef writes formatted NOTICE message to output and syslog if connected.
func (p Printer) Noticef(frmt string, a ...interface{}) { p.Printf(syslog.LOG_NOTICE, frmt, a...) }
// Warning writes WARNING message to output and syslog if connected.
func (p Printer) Warning(a ...interface{}) { p.Warningf("", a...) }
// Warningln writes WARNING message to output and syslog if connected.
func (p Printer) Warningln(a ...interface{}) { p.Warningf("", a...) }
// Warningf writes formatted WARNING message to output and syslog if connected.
func (p Printer) Warningf(frmt string, a ...interface{}) { p.Printf(syslog.LOG_WARNING, frmt, a...) }
// Err writes ERROR message to output and syslog if connected.
func (p Printer) Err(a ...interface{}) { p.Errf("", a...) }
// Errln writes ERROR message to output and syslog if connected.
func (p Printer) Errln(a ...interface{}) { p.Errf("", a...) }
// Errf writes formatted ERROR message to output and syslog if connected.
func (p Printer) Errf(frmt string, a ...interface{}) { p.Printf(syslog.LOG_ERR, frmt, a...) }
// Crit writes CRITICAL message to output and syslog if connected.
func (p Printer) Crit(a ...interface{}) { p.Critf("", a...) }
// Critln writes CRITICAL message to output and syslog if connected.
func (p Printer) Critln(a ...interface{}) { p.Critf("", a...) }
// Critf writes formatted CRITICAL message to output and syslog if connected.
func (p Printer) Critf(frmt string, a ...interface{}) { p.Printf(syslog.LOG_CRIT, frmt, a...) }
// Alert writes ALERT message to output and syslog if connected.
func (p Printer) Alert(a ...interface{}) { p.Alertf("", a...) }
// Alertln writes ALERT message to output and syslog if connected.
func (p Printer) Alertln(a ...interface{}) { p.Alertf("", a...) }
// Alertf writes formatted ALERT message to output and syslog if connected.
func (p Printer) Alertf(frmt string, a ...interface{}) { p.Printf(syslog.LOG_ALERT, frmt, a...) }
// Emerg writes EMERGENCY message to output and syslog if connected.
func (p Printer) Emerg(a ...interface{}) { p.Emergf("", a...) }
// Emergln writes EMERGENCY message to output and syslog if connected.
func (p Printer) Emergln(a ...interface{}) { p.Emergf("", a...) }
// Emergf writes formatted EMERGENCY message to output and syslog if connected.
func (p Printer) Emergf(frmt string, a ...interface{}) { p.Printf(syslog.LOG_EMERG, frmt, a...) }