2019-05-31 01:35:04 +02: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 20:21:43 +02:00
|
|
|
// level is a log level
|
|
|
|
type Level int
|
|
|
|
|
|
|
|
const (
|
2019-08-14 01:18:22 +02:00
|
|
|
LevelFatal Level = iota
|
|
|
|
LevelInfo
|
2019-08-14 01:26:51 +02:00
|
|
|
LevelWarn
|
2019-08-14 01:18:22 +02:00
|
|
|
LevelDebug
|
|
|
|
LevelTrace
|
2019-07-02 20:21:43 +02:00
|
|
|
)
|
|
|
|
|
2019-05-31 01:35:04 +02:00
|
|
|
var (
|
|
|
|
// the local logger
|
|
|
|
logger log.Logger = golog.New()
|
2019-07-02 20:21:43 +02:00
|
|
|
|
2019-08-14 01:18:22 +02:00
|
|
|
// default log level is info
|
|
|
|
level = LevelInfo
|
2019-05-31 01:35:04 +02:00
|
|
|
)
|
|
|
|
|
2019-08-08 14:07:04 +02:00
|
|
|
func init() {
|
|
|
|
switch os.Getenv("MICRO_LOG_LEVEL") {
|
|
|
|
case "debug":
|
2019-08-14 01:18:22 +02:00
|
|
|
level = LevelDebug
|
2019-08-08 14:07:04 +02:00
|
|
|
case "info":
|
2019-08-14 01:18:22 +02:00
|
|
|
level = LevelInfo
|
2019-08-08 14:07:04 +02:00
|
|
|
case "trace":
|
2019-08-14 01:18:22 +02:00
|
|
|
level = LevelTrace
|
2019-08-08 14:07:04 +02:00
|
|
|
case "fatal":
|
2019-08-14 01:18:22 +02:00
|
|
|
level = LevelFatal
|
2019-08-14 01:26:51 +02:00
|
|
|
case "warn":
|
|
|
|
level = LevelWarn
|
2019-08-08 14:07:04 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:35:04 +02: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 20:21:43 +02:00
|
|
|
// WithLevel logs with the level specified
|
|
|
|
func WithLevel(l Level, v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
if l > level {
|
2019-07-02 20:21:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
Log(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLevel logs with the level specified
|
|
|
|
func WithLevelf(l Level, format string, v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
if l > level {
|
2019-07-02 20:21:43 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
Logf(format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trace provides trace level logging
|
|
|
|
func Trace(v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
WithLevel(LevelTrace, v...)
|
2019-07-02 20:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tracef provides trace level logging
|
|
|
|
func Tracef(format string, v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
WithLevelf(LevelTrace, format, v...)
|
2019-07-02 20:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debug provides debug level logging
|
|
|
|
func Debug(v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
WithLevel(LevelDebug, v...)
|
2019-07-02 20:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf provides debug level logging
|
|
|
|
func Debugf(format string, v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
WithLevelf(LevelDebug, format, v...)
|
2019-07-02 20:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Info provides info level logging
|
|
|
|
func Info(v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
WithLevel(LevelInfo, v...)
|
2019-07-02 20:21:43 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Infof provides info level logging
|
|
|
|
func Infof(format string, v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
WithLevelf(LevelInfo, format, v...)
|
2019-07-02 20:21:43 +02:00
|
|
|
}
|
|
|
|
|
2019-08-14 01:26:51 +02:00
|
|
|
// 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...)
|
|
|
|
}
|
|
|
|
|
2019-05-31 01:35:04 +02:00
|
|
|
// Fatal logs with Log and then exits with os.Exit(1)
|
|
|
|
func Fatal(v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
WithLevel(LevelFatal, v...)
|
2019-05-31 01:35:04 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Fatalf logs with Logf and then exits with os.Exit(1)
|
|
|
|
func Fatalf(format string, v ...interface{}) {
|
2019-08-14 01:18:22 +02:00
|
|
|
WithLevelf(LevelFatal, format, v...)
|
2019-05-31 01:35:04 +02:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogger sets the local logger
|
|
|
|
func SetLogger(l log.Logger) {
|
|
|
|
logger = l
|
|
|
|
}
|
2019-07-02 20:21:43 +02: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
|
|
|
|
}
|