Merge pull request #666 from unistack-org/log

export log levels and reverse log level order
This commit is contained in:
Asim Aslam 2019-08-14 07:41:01 +01:00 committed by GitHub
commit dc0fbfc3c0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -12,30 +12,33 @@ import (
type Level int type Level int
const ( const (
trace Level = iota LevelFatal Level = iota
debug LevelInfo
info LevelWarn
fatal LevelDebug
LevelTrace
) )
var ( var (
// the local logger // the local logger
logger log.Logger = golog.New() logger log.Logger = golog.New()
// default log level is debug // default log level is info
level = info level = LevelInfo
) )
func init() { func init() {
switch os.Getenv("MICRO_LOG_LEVEL") { switch os.Getenv("MICRO_LOG_LEVEL") {
case "debug": case "debug":
level = debug level = LevelDebug
case "info": case "info":
level = info level = LevelInfo
case "trace": case "trace":
level = trace level = LevelTrace
case "fatal": case "fatal":
level = fatal level = LevelFatal
case "warn":
level = LevelWarn
} }
} }
@ -51,7 +54,7 @@ func Logf(format string, v ...interface{}) {
// WithLevel logs with the level specified // WithLevel logs with the level specified
func WithLevel(l Level, v ...interface{}) { func WithLevel(l Level, v ...interface{}) {
if l < level { if l > level {
return return
} }
Log(v...) Log(v...)
@ -59,7 +62,7 @@ func WithLevel(l Level, v ...interface{}) {
// WithLevel logs with the level specified // WithLevel logs with the level specified
func WithLevelf(l Level, format string, v ...interface{}) { func WithLevelf(l Level, format string, v ...interface{}) {
if l < level { if l > level {
return return
} }
Logf(format, v...) Logf(format, v...)
@ -67,43 +70,53 @@ func WithLevelf(l Level, format string, v ...interface{}) {
// Trace provides trace level logging // Trace provides trace level logging
func Trace(v ...interface{}) { func Trace(v ...interface{}) {
WithLevel(trace, v...) WithLevel(LevelTrace, v...)
} }
// Tracef provides trace level logging // Tracef provides trace level logging
func Tracef(format string, v ...interface{}) { func Tracef(format string, v ...interface{}) {
WithLevelf(trace, format, v...) WithLevelf(LevelTrace, format, v...)
} }
// Debug provides debug level logging // Debug provides debug level logging
func Debug(v ...interface{}) { func Debug(v ...interface{}) {
WithLevel(debug, v...) WithLevel(LevelDebug, v...)
} }
// Debugf provides debug level logging // Debugf provides debug level logging
func Debugf(format string, v ...interface{}) { func Debugf(format string, v ...interface{}) {
WithLevelf(debug, format, v...) WithLevelf(LevelDebug, format, v...)
} }
// Info provides info level logging // Info provides info level logging
func Info(v ...interface{}) { func Info(v ...interface{}) {
WithLevel(info, v...) WithLevel(LevelInfo, v...)
} }
// Infof provides info level logging // Infof provides info level logging
func Infof(format string, v ...interface{}) { func Infof(format string, v ...interface{}) {
WithLevelf(info, format, v...) WithLevelf(LevelInfo, format, v...)
}
// Warn provides warn level logging
func Warn(v ...interface{}) {
WithLevel(LevelWarn, v...)
}
// Warnf provides warn level logging
func Warnf(format string, v ...interface{}) {
WithLevelf(LevelWarn, format, v...)
} }
// Fatal logs with Log and then exits with os.Exit(1) // Fatal logs with Log and then exits with os.Exit(1)
func Fatal(v ...interface{}) { func Fatal(v ...interface{}) {
WithLevel(fatal, v...) WithLevel(LevelFatal, v...)
os.Exit(1) os.Exit(1)
} }
// Fatalf logs with Logf and then exits with os.Exit(1) // Fatalf logs with Logf and then exits with os.Exit(1)
func Fatalf(format string, v ...interface{}) { func Fatalf(format string, v ...interface{}) {
WithLevelf(fatal, format, v...) WithLevelf(LevelFatal, format, v...)
os.Exit(1) os.Exit(1)
} }