Debian Codegraph @

logger.go

raw symbols SCIP
1 // Copyright (c) 2012-2016 Eli Janssen
2 // Use of this source code is governed by an MIT-style
3 // license that can be found in the LICENSE file.
4
5 package mlog
6
7 import (
8 "fmt"
9 "io"
10 "os"
11 "sync"
12 "sync/atomic"
13 )
14
15 // Emitter is the interface implemented by mlog logging format writers.
16 type Emitter interface {
17 Emit(logger *Logger, level int, message string, extra Map)
18 EmitAttrs(logger *Logger, level int, message string, extra ...*Attr)
19 }
20
21 // A Logger represents a logging object, that embeds log.Logger, and
22 // provides support for a toggle-able debug flag.
23 type Logger struct {
24 out io.Writer
25 e Emitter
26 mu sync.Mutex // ensures atomic writes are synchronized
27 flags uint64
28 }
29
30 // SetOutput sets the Logger output io.Writer
31 func (l *Logger) SetOutput(writer io.Writer) {
32 // lock writing to serialize log output (no scrambled log lines)
33 l.mu.Lock()
34 defer l.mu.Unlock()
35 l.out = writer
36 }
37
38 func (l *Logger) Write(b []byte) (int, error) {
39 // lock writing to serialize log output (no scrambled log lines)
40 l.mu.Lock()
41 defer l.mu.Unlock()
42 return l.out.Write(b)
43 }
44
45 // Emit invokes the FormatWriter and logs the event.
46 func (l *Logger) Emit(level int, message string, extra Map) {
47 l.e.Emit(l, level, message, extra)
48 }
49
50 // Emit invokes the FormatWriter and logs the event.
51 func (l *Logger) EmitAttrs(level int, message string, extra ...*Attr) {
52 l.e.EmitAttrs(l, level, message, extra...)
53 }
54
55 // SetEmitter sets the Emitter
56 func (l *Logger) SetEmitter(e Emitter) {
57 l.mu.Lock()
58 defer l.mu.Unlock()
59 l.e = e
60 }
61
62 // Flags returns the current FlagSet
63 func (l *Logger) Flags() FlagSet {
64 return FlagSet(atomic.LoadUint64(&l.flags))
65 }
66
67 // SetFlags sets the current FlagSet
68 func (l *Logger) SetFlags(flags FlagSet) {
69 atomic.StoreUint64(&l.flags, uint64(flags))
70 }
71
72 // HasDebug returns true if the debug logging FlagSet is enabled, false
73 // otherwise.
74 func (l *Logger) HasDebug() bool {
75 flags := FlagSet(atomic.LoadUint64(&l.flags))
76 return flags&Ldebug != 0
77 }
78
79 // Debugx conditionally logs message and any Attr elements at level="debug".
80 // If the Logger does not have the Ldebug flag, nothing is logged.
81 func (l *Logger) Debugx(message string, attrs ...*Attr) {
82 if l.HasDebug() {
83 l.EmitAttrs(-1, message, attrs...)
84 }
85 }
86
87 // Infox logs message and any Map elements at level="info".
88 func (l *Logger) Infox(message string, attrs ...*Attr) {
89 l.EmitAttrs(0, message, attrs...)
90 }
91
92 // Printx logs message and any Map elements at level="info".
93 func (l *Logger) Printx(message string, attrs ...*Attr) {
94 l.EmitAttrs(0, message, attrs...)
95 }
96
97 // Fatalx logs message and any Map elements at level="fatal", then calls
98 // os.Exit(1)
99 func (l *Logger) Fatalx(message string, attrs ...*Attr) {
100 l.EmitAttrs(1, message, attrs...)
101 os.Exit(1)
102 }
103
104 // Panicx logs message and any Map elements at level="fatal", then calls
105 // panic().
106 func (l *Logger) Panicx(message string, attrs ...*Attr) {
107 l.EmitAttrs(1, message, attrs...)
108 panic(message)
109 }
110
111 // Debugm conditionally logs message and any Map elements at level="debug".
112 // If the Logger does not have the Ldebug flag, nothing is logged.
113 func (l *Logger) Debugm(message string, v Map) {
114 if l.HasDebug() {
115 l.Emit(-1, message, v)
116 }
117 }
118
119 // Infom logs message and any Map elements at level="info".
120 func (l *Logger) Infom(message string, v Map) {
121 l.Emit(0, message, v)
122 }
123
124 // Printm logs message and any Map elements at level="info".
125 func (l *Logger) Printm(message string, v Map) {
126 l.Emit(0, message, v)
127 }
128
129 // Fatalm logs message and any Map elements at level="fatal", then calls
130 // os.Exit(1)
131 func (l *Logger) Fatalm(message string, v Map) {
132 l.Emit(1, message, v)
133 os.Exit(1)
134 }
135
136 // Panicm logs message and any Map elements at level="fatal", then calls
137 // panic().
138 func (l *Logger) Panicm(message string, v Map) {
139 l.Emit(1, message, v)
140 panic(message)
141 }
142
143 // Debugf formats and conditionally logs message at level="debug".
144 // If the Logger does not have the Ldebug flag, nothing is logged.
145 func (l *Logger) Debugf(format string, v ...interface{}) {
146 if l.HasDebug() {
147 l.Emit(-1, fmt.Sprintf(format, v...), nil)
148 }
149 }
150
151 // Infof formats and logs message at level="info".
152 func (l *Logger) Infof(format string, v ...interface{}) {
153 l.Emit(0, fmt.Sprintf(format, v...), nil)
154 }
155
156 // Printf formats and logs message at level="info".
157 func (l *Logger) Printf(format string, v ...interface{}) {
158 l.Emit(0, fmt.Sprintf(format, v...), nil)
159 }
160
161 // Fatalf formats and logs message at level="fatal", then calls
162 // os.Exit(1)
163 func (l *Logger) Fatalf(format string, v ...interface{}) {
164 l.Emit(1, fmt.Sprintf(format, v...), nil)
165 os.Exit(1)
166 }
167
168 // Panicf formats and logs message at level="fatal", then calls
169 // panic().
170 func (l *Logger) Panicf(format string, v ...interface{}) {
171 s := fmt.Sprintf(format, v...)
172 l.Emit(1, s, nil)
173 panic(s)
174 }
175
176 // Debug conditionally logs message at level="debug".
177 // If the Logger does not have the Ldebug flag, nothing is logged.
178 func (l *Logger) Debug(v ...interface{}) {
179 if l.HasDebug() {
180 l.Emit(-1, fmt.Sprint(v...), nil)
181 }
182 }
183
184 // Info logs message at level="info".
185 func (l *Logger) Info(v ...interface{}) {
186 l.Emit(0, fmt.Sprint(v...), nil)
187 }
188
189 // Print logs message at level="info".
190 func (l *Logger) Print(v ...interface{}) {
191 l.Emit(0, fmt.Sprint(v...), nil)
192 }
193
194 // Fatal logs message at level="fatal", then calls
195 // os.Exit(1)
196 func (l *Logger) Fatal(v ...interface{}) {
197 l.Emit(1, fmt.Sprint(v...), nil)
198 os.Exit(1)
199 }
200
201 // Panic logs message at level="fatal", then calls
202 // panic().
203 func (l *Logger) Panic(v ...interface{}) {
204 s := fmt.Sprint(v...)
205 l.Emit(1, s, nil)
206 panic(s)
207 }
208
209 // New creates a new Logger.
210 func New(out io.Writer, flags FlagSet) *Logger {
211 return NewFormatLogger(out, flags, &FormatWriterStructured{})
212 }
213
214 // NewFormatLogger creates a new Logger, using the specified Emitter.
215 func NewFormatLogger(out io.Writer, flags FlagSet, e Emitter) *Logger {
216 return &Logger{
217 out: out,
218 flags: uint64(flags),
219 e: e,
220 }
221 }