micro/util/log/log.go
Vasiliy Tolstov f23638c036 fix import paths for v2 release
Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
2020-01-30 14:44:40 +03:00

206 lines
3.7 KiB
Go

// Package log is a global internal logger
package log
import (
"fmt"
"os"
"time"
"github.com/micro/go-micro/v2/debug/log"
)
// level is a log level
type Level int
const (
LevelFatal Level = iota
LevelError
LevelInfo
LevelWarn
LevelDebug
LevelTrace
)
var (
// the local logger
logger log.Log = log.DefaultLog
// default log level is info
level = LevelInfo
// prefix for all messages
prefix string
)
func init() {
switch os.Getenv("MICRO_LOG_LEVEL") {
case "trace":
level = LevelTrace
case "debug":
level = LevelDebug
case "warn":
level = LevelWarn
case "info":
level = LevelInfo
case "error":
level = LevelError
case "fatal":
level = LevelFatal
}
}
func (l Level) String() string {
switch l {
case LevelTrace:
return "trace"
case LevelDebug:
return "debug"
case LevelWarn:
return "warn"
case LevelInfo:
return "info"
case LevelError:
return "error"
case LevelFatal:
return "fatal"
default:
return "unknown"
}
}
// Log makes use of github.com/micro/debug/log
func Log(v ...interface{}) {
if len(prefix) > 0 {
v = append([]interface{}{prefix, " "}, v...)
}
logger.Write(log.Record{
Timestamp: time.Now(),
Message: fmt.Sprint(v...),
Metadata: map[string]string{
"level": level.String(),
},
})
}
// Logf makes use of github.com/micro/debug/log
func Logf(format string, v ...interface{}) {
if len(prefix) > 0 {
format = prefix + " " + format
}
logger.Write(log.Record{
Timestamp: time.Now(),
Message: fmt.Sprintf(format, v...),
Metadata: map[string]string{
"level": level.String(),
},
})
}
// 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(LevelTrace, v...)
}
// Tracef provides trace level logging
func Tracef(format string, v ...interface{}) {
WithLevelf(LevelTrace, format, v...)
}
// Debug provides debug level logging
func Debug(v ...interface{}) {
WithLevel(LevelDebug, v...)
}
// Debugf provides debug level logging
func Debugf(format string, v ...interface{}) {
WithLevelf(LevelDebug, 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...)
}
// Info provides info level logging
func Info(v ...interface{}) {
WithLevel(LevelInfo, v...)
}
// Infof provides info level logging
func Infof(format string, v ...interface{}) {
WithLevelf(LevelInfo, format, v...)
}
// Error provides warn level logging
func Error(v ...interface{}) {
WithLevel(LevelError, v...)
}
// Errorf provides warn level logging
func Errorf(format string, v ...interface{}) {
WithLevelf(LevelError, format, v...)
}
// Fatal logs with Log and then exits with os.Exit(1)
func Fatal(v ...interface{}) {
WithLevel(LevelFatal, v...)
os.Exit(1)
}
// Fatalf logs with Logf and then exits with os.Exit(1)
func Fatalf(format string, v ...interface{}) {
WithLevelf(LevelFatal, format, v...)
os.Exit(1)
}
// SetLogger sets the local logger
func SetLogger(l log.Log) {
logger = l
}
// GetLogger returns the local logger
func GetLogger() log.Log {
return logger
}
// SetLevel sets the log level
func SetLevel(l Level) {
level = l
}
// GetLevel returns the current level
func GetLevel() Level {
return level
}
// Set a prefix for the logger
func SetPrefix(p string) {
prefix = p
}
// Set service name
func Name(name string) {
prefix = fmt.Sprintf("[%s]", name)
}