This repository has been archived by the owner on Dec 3, 2017. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlogging.go
208 lines (178 loc) · 4.63 KB
/
logging.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
// Copyright (c) 2012-2016 Eli Janssen
// Use of this source code is governed by an MIT-style
// license that can be found in the LICENSE file.
// Package gologit implements a very simple wrapper around the
// Go "log" package, providing support for a toggle-able debug flag
// and a couple of functions that log or not based on that flag.
package gologit
import (
"fmt"
"log"
"os"
"os/signal"
"sync"
)
var Logger = New(false)
// A DebugLogger represents a logging object, that embeds log.Logger, and
// provides support for a toggle-able debug flag.
type DebugLogger struct {
*log.Logger
debug bool
mx sync.Mutex
}
// New creates a new DebugLogger.
// The debug argument specifies whether debug should be set or not.
func New(debug bool) *DebugLogger {
flags := log.LstdFlags
if debug == true {
flags = flags | log.Lshortfile
}
return &DebugLogger{log.New(os.Stderr, "", flags), debug, sync.Mutex{}}
}
func (l *DebugLogger) updateLogFlags() {
if l.debug == false {
l.Logger.SetFlags(l.Logger.Flags() | log.Lshortfile)
} else {
l.Logger.SetFlags(l.Logger.Flags() ^ log.Lshortfile)
}
}
// Toggles the debug state.
// If debug is true, sets it to false.
// If debug is false, sets it to true.
func (l *DebugLogger) Toggle() {
l.mx.Lock()
defer l.mx.Unlock()
if l.debug == false {
l.debug = true
} else {
l.debug = false
}
l.updateLogFlags()
}
func (l *DebugLogger) ToggleOnSignal(sig os.Signal) {
debugSig := make(chan os.Signal, 1)
// spawn goroutine to handle signal/toggle of debug logging
go func() {
for {
<-debugSig
l.Toggle()
if l.State() {
l.Printf("Debug logging enabled")
} else {
l.Printf("Debug logging disabled")
}
}
}()
// notify send to debug sign channel on signusr1
signal.Notify(debugSig, sig)
}
func (l *DebugLogger) State() bool {
return l.debug
}
// Set the debug state directly.
func (l *DebugLogger) Set(debug bool) {
l.mx.Lock()
defer l.mx.Unlock()
l.debug = debug
l.updateLogFlags()
}
// Debugf calls log.Printf if debug is true.
// If debug is false, does nothing.
func (l *DebugLogger) Debugf(format string, v ...interface{}) {
if l.debug == true {
l.Output(2, fmt.Sprintf(format, v...))
}
}
// Debug calls log.Print if debug is true.
// If debug is false, does nothing.
func (l *DebugLogger) Debug(v ...interface{}) {
if l.debug == true {
l.Output(2, fmt.Sprint(v...))
}
}
// Debugln calls log.Println if debug is true.
// If debug is false, does nothing.
func (l *DebugLogger) Debugln(v ...interface{}) {
if l.debug == true {
l.Output(2, fmt.Sprintln(v...))
}
}
// These functions call the default Logger
// Toggles the debug state of the default Logger. See Logger.Toggle
func Toggle() {
Logger.Toggle()
}
func ToggleOnSignal(sig os.Signal) {
Logger.ToggleOnSignal(sig)
}
// Gets the state of the default Logger. See Logger.State
func State() bool {
return Logger.State()
}
// Sets the state of the default Logger. See Logger.Set
func Set(debug bool) {
Logger.Set(debug)
}
// Logs to the default Logger. See Logger.Debugf
func Debugf(format string, v ...interface{}) {
if Logger.State() == true {
Logger.Output(2, fmt.Sprintf(format, v...))
}
}
// Logs to the default Logger. See Logger.Debug
func Debug(v ...interface{}) {
if Logger.State() == true {
Logger.Output(2, fmt.Sprint(v...))
}
}
// Logs to the default Logger. See Logger.Debugln
func Debugln(v ...interface{}) {
if Logger.State() == true {
Logger.Output(2, fmt.Sprintln(v...))
}
}
// Logs to the default Logger. See Logger.Printf
func Printf(format string, v ...interface{}) {
Logger.Output(2, fmt.Sprintf(format, v...))
}
// Logs to the default Logger. See Logger.Print
func Print(v ...interface{}) {
Logger.Output(2, fmt.Sprint(v...))
}
// Logs to the default Logger. See Logger.Println
func Println(v ...interface{}) {
Logger.Output(2, fmt.Sprintln(v...))
}
// Logs to the default Logger. See Logger.Fatal
func Fatal(v ...interface{}) {
Logger.Output(2, fmt.Sprint(v...))
os.Exit(1)
}
// Logs to the default Logger. See Logger.Fatalf
func Fatalf(format string, v ...interface{}) {
Logger.Output(2, fmt.Sprintf(format, v...))
os.Exit(1)
}
// Logs to the default Logger. See Logger.Fatalln
func Fatalln(v ...interface{}) {
Logger.Output(2, fmt.Sprintln(v...))
os.Exit(1)
}
// Logs to the default Logger. See Logger.Panic
func Panic(v ...interface{}) {
s := fmt.Sprint(v...)
Logger.Output(2, s)
panic(s)
}
// Logs to the default Logger. See Logger.Panicf
func Panicf(format string, v ...interface{}) {
s := fmt.Sprintf(format, v...)
Logger.Output(2, s)
panic(s)
}
// Logs to the default Logger. See Logger.Panicln
func Panicln(v ...interface{}) {
s := fmt.Sprintln(v...)
Logger.Output(2, s)
panic(s)
}