Skip to content

Commit 2d272df

Browse files
committed
added panic, moved fatal out of leveled logging
Signed-off-by: Vishal Rana <[email protected]>
1 parent 6058293 commit 2d272df

File tree

1 file changed

+38
-10
lines changed

1 file changed

+38
-10
lines changed

log/log.go

Lines changed: 38 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ type (
3737
)
3838

3939
const (
40-
DEBUG Lvl = iota
40+
DEBUG Lvl = iota + 1
4141
INFO
4242
WARN
4343
ERROR
44-
FATAL
4544
OFF
4645
)
4746

@@ -70,11 +69,11 @@ func New(prefix string) (l *Logger) {
7069

7170
func (l *Logger) initLevels() {
7271
l.levels = []string{
72+
"-",
7373
l.color.Blue("DEBUG"),
7474
l.color.Green("INFO"),
7575
l.color.Yellow("WARN"),
7676
l.color.Red("ERROR"),
77-
l.color.RedBg("FATAL"),
7877
}
7978
}
8079

@@ -128,15 +127,16 @@ func (l *Logger) SetHeader(h string) {
128127
}
129128

130129
func (l *Logger) Print(i ...interface{}) {
131-
fmt.Fprintln(l.output, i...)
130+
l.log(0, "", i...)
131+
// fmt.Fprintln(l.output, i...)
132132
}
133133

134134
func (l *Logger) Printf(format string, args ...interface{}) {
135-
fmt.Fprintf(l.output, format, args...)
135+
l.log(0, format, args...)
136136
}
137137

138138
func (l *Logger) Printj(j JSON) {
139-
json.NewEncoder(l.output).Encode(j)
139+
l.log(0, "json", j)
140140
}
141141

142142
func (l *Logger) Debug(i ...interface{}) {
@@ -188,17 +188,33 @@ func (l *Logger) Errorj(j JSON) {
188188
}
189189

190190
func (l *Logger) Fatal(i ...interface{}) {
191-
l.log(FATAL, "", i...)
191+
l.Print(i...)
192192
os.Exit(1)
193193
}
194194

195195
func (l *Logger) Fatalf(format string, args ...interface{}) {
196-
l.log(FATAL, format, args...)
196+
l.Printf(format, args...)
197197
os.Exit(1)
198198
}
199199

200200
func (l *Logger) Fatalj(j JSON) {
201-
l.log(FATAL, "json", j)
201+
l.Printj(j)
202+
os.Exit(1)
203+
}
204+
205+
func (l *Logger) Panic(i ...interface{}) {
206+
l.Print(i...)
207+
panic(fmt.Sprint(i...))
208+
}
209+
210+
func (l *Logger) Panicf(format string, args ...interface{}) {
211+
l.Printf(format, args...)
212+
panic(fmt.Sprintf(format, args))
213+
}
214+
215+
func (l *Logger) Panicj(j JSON) {
216+
l.Printj(j)
217+
panic(j)
202218
}
203219

204220
func DisableColor() {
@@ -309,6 +325,18 @@ func Fatalj(j JSON) {
309325
global.Fatalj(j)
310326
}
311327

328+
func Panic(i ...interface{}) {
329+
global.Panic(i...)
330+
}
331+
332+
func Panicf(format string, args ...interface{}) {
333+
global.Panicf(format, args...)
334+
}
335+
336+
func Panicj(j JSON) {
337+
global.Panicj(j)
338+
}
339+
312340
func (l *Logger) log(v Lvl, format string, args ...interface{}) {
313341
l.mutex.Lock()
314342
defer l.mutex.Unlock()
@@ -317,7 +345,7 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
317345
defer l.bufferPool.Put(buf)
318346
_, file, line, _ := runtime.Caller(3)
319347

320-
if v >= l.level {
348+
if v >= l.level || v == 0 {
321349
message := ""
322350
if format == "" {
323351
message = fmt.Sprint(args...)

0 commit comments

Comments
 (0)