micro/util/log/log.go

129 lines
2.2 KiB
Go
Raw Normal View History

2019-05-31 00:35:04 +01:00
// Package log is a global internal logger
package log
import (
"os"
"github.com/go-log/log"
golog "github.com/go-log/log/log"
)
2019-07-02 19:21:43 +01:00
// level is a log level
type Level int
const (
trace Level = iota
debug
info
fatal
)
2019-05-31 00:35:04 +01:00
var (
// the local logger
logger log.Logger = golog.New()
2019-07-02 19:21:43 +01:00
// default log level is debug
level = info
2019-05-31 00:35:04 +01:00
)
func init() {
switch os.Getenv("MICRO_LOG_LEVEL") {
case "debug":
level = debug
case "info":
level = info
case "trace":
level = trace
case "fatal":
level = fatal
}
}
2019-05-31 00:35:04 +01:00
// Log makes use of github.com/go-log/log.Log
func Log(v ...interface{}) {
logger.Log(v...)
}
// Logf makes use of github.com/go-log/log.Logf
func Logf(format string, v ...interface{}) {
logger.Logf(format, v...)
}
2019-07-02 19:21:43 +01:00
// WithLevel logs with the level specified
func WithLevel(l Level, v ...interface{}) {
if l < level {
return
}
Log(v...)
}
// WithLevel logs with the level specified
func WithLevelf(l Level, format string, v ...interface{}) {
if l < level {
return
}
Logf(format, v...)
}
// Trace provides trace level logging
func Trace(v ...interface{}) {
WithLevel(trace, v...)
}
// Tracef provides trace level logging
func Tracef(format string, v ...interface{}) {
WithLevelf(trace, format, v...)
}
// Debug provides debug level logging
func Debug(v ...interface{}) {
WithLevel(debug, v...)
}
// Debugf provides debug level logging
func Debugf(format string, v ...interface{}) {
WithLevelf(debug, format, v...)
}
// Info provides info level logging
func Info(v ...interface{}) {
WithLevel(info, v...)
}
// Infof provides info level logging
func Infof(format string, v ...interface{}) {
WithLevelf(info, format, v...)
}
2019-05-31 00:35:04 +01:00
// Fatal logs with Log and then exits with os.Exit(1)
func Fatal(v ...interface{}) {
2019-07-02 19:21:43 +01:00
WithLevel(fatal, v...)
2019-05-31 00:35:04 +01:00
os.Exit(1)
}
// Fatalf logs with Logf and then exits with os.Exit(1)
func Fatalf(format string, v ...interface{}) {
2019-07-02 19:21:43 +01:00
WithLevelf(fatal, format, v...)
2019-05-31 00:35:04 +01:00
os.Exit(1)
}
// SetLogger sets the local logger
func SetLogger(l log.Logger) {
logger = l
}
2019-07-02 19:21:43 +01:00
// GetLogger returns the local logger
func GetLogger() log.Logger {
return logger
}
// SetLevel sets the log level
func SetLevel(l Level) {
level = l
}
// GetLevel returns the current level
func GetLevel() Level {
return level
}