forked from simplesurance/baur
-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.go
120 lines (90 loc) · 2.18 KB
/
log.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
package log
import (
"fmt"
"os"
"sync"
)
var lock = sync.Mutex{}
// DebugEnabled set to true to print debug message otherwise they are suppressed
var DebugEnabled bool
const actionPrefix = "*"
const errorPrefix = "ERROR"
// Actionln prints something with the ActionPrefix preprended
func Actionln(v ...interface{}) {
lock.Lock()
defer lock.Unlock()
if len(v) != 0 {
v[0] = fmt.Sprintf("%s %s", actionPrefix, v[0])
}
fmt.Println(v...)
}
// Actionf prints something with the ActionPrefix preprended
func Actionf(format string, v ...interface{}) {
lock.Lock()
defer lock.Unlock()
fmt.Printf(actionPrefix+" "+format, v...)
}
// Debugln logs a debug message to stdout.
// It's only shown if debugging is enabled.
func Debugln(v ...interface{}) {
lock.Lock()
defer lock.Unlock()
if !DebugEnabled {
return
}
fmt.Println(v...)
}
// Debugf logs a debug message to stdout.
// It's only shown if debugging is enabled.
func Debugf(format string, v ...interface{}) {
lock.Lock()
defer lock.Unlock()
if !DebugEnabled {
return
}
fmt.Printf(format, v...)
}
// Fatalln logs a message to stderr and terminates the application with an error
func Fatalln(v ...interface{}) {
lock.Lock()
defer lock.Unlock()
if len(v) != 0 {
v[0] = fmt.Sprintf("%s: %s", errorPrefix, v[0])
}
fmt.Fprintln(os.Stderr, v...)
os.Exit(1)
}
// Fatalf logs a message to stderr and terminates the application with an error
func Fatalf(format string, v ...interface{}) {
lock.Lock()
defer lock.Unlock()
fmt.Fprintf(os.Stderr, errorPrefix+": "+format, v...)
os.Exit(1)
}
// Errorln logs a message to stderr
func Errorln(v ...interface{}) {
lock.Lock()
defer lock.Unlock()
if len(v) != 0 {
v[0] = fmt.Sprintf("%s: %s", errorPrefix, v[0])
}
fmt.Fprintln(os.Stderr, v...)
}
// Errorf logs a message to stderr
func Errorf(format string, v ...interface{}) {
lock.Lock()
defer lock.Unlock()
fmt.Fprintf(os.Stderr, errorPrefix+": "+format, v...)
}
// Infoln logs a message to stdout
func Infoln(v ...interface{}) {
lock.Lock()
defer lock.Unlock()
fmt.Println(v...)
}
// Infof logs a message to stdout
func Infof(format string, v ...interface{}) {
lock.Lock()
defer lock.Unlock()
fmt.Printf(format, v...)
}