-
Notifications
You must be signed in to change notification settings - Fork 1
/
llog.go
176 lines (147 loc) · 3.7 KB
/
llog.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
//llog is a simple *leveled* logging packages originally writen and used in Go
//services at Sqwiggle (https://www.sqwiggle.com). The API is designed to be
//conceptually similar to the standard library's log package
package llog
import (
"errors"
"fmt"
"io"
"os"
"time"
)
const (
RED = "\033[31m"
GREEN = "\033[32m"
YELLOW = "\033[93m"
CYAN = "\033[36m"
MAGENTA = "\033[95m"
RESET = "\033[0m"
)
//The available log levels are { levelNull:no logging, levelDebug:log out
//everything, levelInfo: log out Info, Warn, Error, Succes and FATAL, LevelWarn:
//log out Warn, Error, Success and FATAL and finally LevelError: log out Error,
//Success and FATAL}
const (
LevelNull = iota
LevelDebug
LevelInfo
LevelWarn
LevelError
)
var (
level int = 1
)
//llog.Logger wraps/embeds a writer so that you can log where ever you like.
//This package defaults to logging to os.Stdout
type Logger struct {
io.Writer
}
func write(w io.Writer, color string, s string, a ...interface{}) {
fmt.Fprintf(
w,
"%v[%v] %v%v\n",
color,
time.Now().Format("2006/01/02-15:04:05"),
fmt.Sprintf(s, a...),
RESET,
)
}
func SetLevel(i int) error {
if i < 1 || i > 4 {
return errors.New("log level out of range")
}
level = i
return nil
}
func (l *Logger) Debug(s interface{}) {
if !(level > LevelDebug || level == LevelNull) {
write(l, YELLOW, fmt.Sprint(s))
}
}
func (l *Logger) Debugf(s interface{}, a ...interface{}) {
if !(level > LevelDebug || level == LevelNull) {
write(l, YELLOW, fmt.Sprint(s), a...)
}
}
func (l *Logger) Info(s interface{}) {
if !(level > LevelInfo || level == LevelNull) {
write(l, CYAN, fmt.Sprint(s))
}
}
func (l *Logger) Infof(s interface{}, a ...interface{}) {
if !(level > LevelDebug || level == LevelNull) {
write(l, CYAN, fmt.Sprint(s), a...)
}
}
func (l *Logger) Warn(s interface{}) {
if !(level > LevelWarn || level == LevelNull) {
write(l, MAGENTA, fmt.Sprint(s))
}
}
func (l *Logger) Warnf(s interface{}, a ...interface{}) {
if !(level > LevelWarn || level == LevelNull) {
write(l, MAGENTA, fmt.Sprint(s), a...)
}
}
func (l *Logger) Error(s interface{}) {
write(l, RED, fmt.Sprint(s))
}
func (l *Logger) Errorf(s interface{}, a ...interface{}) {
write(l, RED, fmt.Sprint(s), a...)
}
func (l *Logger) Success(s interface{}) {
write(l, GREEN, fmt.Sprint(s))
}
func (l *Logger) Successf(s interface{}, a ...interface{}) {
write(l, GREEN, fmt.Sprint(s), a...)
}
func (l *Logger) FATAL(s interface{}) {
write(os.Stdout, RED, fmt.Sprint(s))
os.Exit(1)
}
func Debug(s interface{}) {
if !(level > LevelDebug || level == LevelNull) {
write(os.Stdout, YELLOW, fmt.Sprint(s))
}
}
func Debugf(s interface{}, a ...interface{}) {
if !(level > LevelDebug || level == LevelNull) {
write(os.Stdout, YELLOW, fmt.Sprint(s), a...)
}
}
func Info(s interface{}) {
if !(level > LevelInfo || level == LevelNull) {
write(os.Stdout, CYAN, fmt.Sprint(s))
}
}
func Infof(s interface{}, a ...interface{}) {
if !(level > LevelInfo || level == LevelNull) {
write(os.Stdout, CYAN, fmt.Sprint(s), a...)
}
}
func Warn(s interface{}) {
if !(level > LevelWarn || level == LevelNull) {
write(os.Stdout, MAGENTA, fmt.Sprint(s))
}
}
func Warnf(s interface{}, a ...interface{}) {
if !(level > LevelWarn || level == LevelNull) {
write(os.Stdout, MAGENTA, fmt.Sprint(s), a...)
}
}
func Error(s interface{}) {
write(os.Stdout, RED, fmt.Sprint(s))
}
func Errorf(s interface{}, a ...interface{}) {
write(os.Stdout, RED, fmt.Sprint(s), a...)
}
func FATAL(s interface{}) {
write(os.Stdout, RED, fmt.Sprint(s))
os.Exit(1)
}
func Success(s interface{}) {
write(os.Stdout, GREEN, fmt.Sprint(s))
}
func Successf(s interface{}, a ...interface{}) {
write(os.Stdout, GREEN, fmt.Sprint(s), a...)
}