Skip to content

Commit

Permalink
added panic, moved fatal out of leveled logging
Browse files Browse the repository at this point in the history
Signed-off-by: Vishal Rana <[email protected]>
  • Loading branch information
vishr committed Oct 23, 2016
1 parent 6058293 commit 2d272df
Showing 1 changed file with 38 additions and 10 deletions.
48 changes: 38 additions & 10 deletions log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,11 +37,10 @@ type (
)

const (
DEBUG Lvl = iota
DEBUG Lvl = iota + 1
INFO
WARN
ERROR
FATAL
OFF
)

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

func (l *Logger) initLevels() {
l.levels = []string{
"-",
l.color.Blue("DEBUG"),
l.color.Green("INFO"),
l.color.Yellow("WARN"),
l.color.Red("ERROR"),
l.color.RedBg("FATAL"),
}
}

Expand Down Expand Up @@ -128,15 +127,16 @@ func (l *Logger) SetHeader(h string) {
}

func (l *Logger) Print(i ...interface{}) {
fmt.Fprintln(l.output, i...)
l.log(0, "", i...)
// fmt.Fprintln(l.output, i...)
}

func (l *Logger) Printf(format string, args ...interface{}) {
fmt.Fprintf(l.output, format, args...)
l.log(0, format, args...)
}

func (l *Logger) Printj(j JSON) {
json.NewEncoder(l.output).Encode(j)
l.log(0, "json", j)
}

func (l *Logger) Debug(i ...interface{}) {
Expand Down Expand Up @@ -188,17 +188,33 @@ func (l *Logger) Errorj(j JSON) {
}

func (l *Logger) Fatal(i ...interface{}) {
l.log(FATAL, "", i...)
l.Print(i...)
os.Exit(1)
}

func (l *Logger) Fatalf(format string, args ...interface{}) {
l.log(FATAL, format, args...)
l.Printf(format, args...)
os.Exit(1)
}

func (l *Logger) Fatalj(j JSON) {
l.log(FATAL, "json", j)
l.Printj(j)
os.Exit(1)
}

func (l *Logger) Panic(i ...interface{}) {
l.Print(i...)
panic(fmt.Sprint(i...))
}

func (l *Logger) Panicf(format string, args ...interface{}) {
l.Printf(format, args...)
panic(fmt.Sprintf(format, args))
}

func (l *Logger) Panicj(j JSON) {
l.Printj(j)
panic(j)
}

func DisableColor() {
Expand Down Expand Up @@ -309,6 +325,18 @@ func Fatalj(j JSON) {
global.Fatalj(j)
}

func Panic(i ...interface{}) {
global.Panic(i...)
}

func Panicf(format string, args ...interface{}) {
global.Panicf(format, args...)
}

func Panicj(j JSON) {
global.Panicj(j)
}

func (l *Logger) log(v Lvl, format string, args ...interface{}) {
l.mutex.Lock()
defer l.mutex.Unlock()
Expand All @@ -317,7 +345,7 @@ func (l *Logger) log(v Lvl, format string, args ...interface{}) {
defer l.bufferPool.Put(buf)
_, file, line, _ := runtime.Caller(3)

if v >= l.level {
if v >= l.level || v == 0 {
message := ""
if format == "" {
message = fmt.Sprint(args...)
Expand Down

0 comments on commit 2d272df

Please sign in to comment.