2019-05-31 00:35:04 +01:00
|
|
|
// Package log is a global internal logger
|
|
|
|
package log
|
|
|
|
|
|
|
|
import (
|
2019-10-02 17:42:34 +01:00
|
|
|
"fmt"
|
2019-05-31 00:35:04 +01:00
|
|
|
"os"
|
2020-02-06 02:16:57 +08:00
|
|
|
"sync/atomic"
|
2019-12-18 16:02:11 +00:00
|
|
|
"time"
|
2019-05-31 00:35:04 +01:00
|
|
|
|
2020-01-30 14:39:00 +03:00
|
|
|
"github.com/micro/go-micro/v2/debug/log"
|
2019-05-31 00:35:04 +01:00
|
|
|
)
|
|
|
|
|
2019-07-02 19:21:43 +01:00
|
|
|
// level is a log level
|
2020-02-06 02:16:57 +08:00
|
|
|
type Level int32
|
2019-07-02 19:21:43 +01:00
|
|
|
|
|
|
|
const (
|
2019-08-14 02:18:22 +03:00
|
|
|
LevelFatal Level = iota
|
2019-08-29 17:21:43 +01:00
|
|
|
LevelError
|
2019-11-11 15:57:14 +08:00
|
|
|
LevelWarn
|
2020-02-06 02:16:57 +08:00
|
|
|
LevelInfo
|
2019-08-14 02:18:22 +03:00
|
|
|
LevelDebug
|
|
|
|
LevelTrace
|
2019-07-02 19:21:43 +01:00
|
|
|
)
|
|
|
|
|
2019-05-31 00:35:04 +01:00
|
|
|
var (
|
|
|
|
// the local logger
|
2019-12-18 16:02:11 +00:00
|
|
|
logger log.Log = log.DefaultLog
|
2019-07-02 19:21:43 +01:00
|
|
|
|
2019-08-14 02:18:22 +03:00
|
|
|
// default log level is info
|
|
|
|
level = LevelInfo
|
2019-10-02 17:42:34 +01:00
|
|
|
|
|
|
|
// prefix for all messages
|
|
|
|
prefix string
|
2019-05-31 00:35:04 +01:00
|
|
|
)
|
|
|
|
|
2019-08-08 13:07:04 +01:00
|
|
|
func init() {
|
|
|
|
switch os.Getenv("MICRO_LOG_LEVEL") {
|
2019-08-29 17:21:43 +01:00
|
|
|
case "trace":
|
|
|
|
level = LevelTrace
|
2019-08-08 13:07:04 +01:00
|
|
|
case "debug":
|
2019-08-14 02:18:22 +03:00
|
|
|
level = LevelDebug
|
2019-11-11 15:57:14 +08:00
|
|
|
case "warn":
|
|
|
|
level = LevelWarn
|
2019-08-08 13:07:04 +01:00
|
|
|
case "info":
|
2019-08-14 02:18:22 +03:00
|
|
|
level = LevelInfo
|
2019-08-29 17:21:43 +01:00
|
|
|
case "error":
|
|
|
|
level = LevelError
|
2019-08-08 13:07:04 +01:00
|
|
|
case "fatal":
|
2019-08-14 02:18:22 +03:00
|
|
|
level = LevelFatal
|
2019-08-08 13:07:04 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-18 16:02:11 +00:00
|
|
|
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
|
2019-05-31 00:35:04 +01:00
|
|
|
func Log(v ...interface{}) {
|
2019-10-02 17:42:34 +01:00
|
|
|
if len(prefix) > 0 {
|
2019-12-18 16:02:11 +00:00
|
|
|
v = append([]interface{}{prefix, " "}, v...)
|
2019-10-02 17:42:34 +01:00
|
|
|
}
|
2019-12-18 16:02:11 +00:00
|
|
|
logger.Write(log.Record{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Message: fmt.Sprint(v...),
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"level": level.String(),
|
|
|
|
},
|
|
|
|
})
|
2019-05-31 00:35:04 +01:00
|
|
|
}
|
|
|
|
|
2019-12-18 16:02:11 +00:00
|
|
|
// Logf makes use of github.com/micro/debug/log
|
2019-05-31 00:35:04 +01:00
|
|
|
func Logf(format string, v ...interface{}) {
|
2019-10-02 17:42:34 +01:00
|
|
|
if len(prefix) > 0 {
|
|
|
|
format = prefix + " " + format
|
|
|
|
}
|
2019-12-18 16:02:11 +00:00
|
|
|
logger.Write(log.Record{
|
|
|
|
Timestamp: time.Now(),
|
|
|
|
Message: fmt.Sprintf(format, v...),
|
|
|
|
Metadata: map[string]string{
|
|
|
|
"level": level.String(),
|
|
|
|
},
|
|
|
|
})
|
2019-05-31 00:35:04 +01:00
|
|
|
}
|
|
|
|
|
2019-07-02 19:21:43 +01:00
|
|
|
// WithLevel logs with the level specified
|
|
|
|
func WithLevel(l Level, v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
if l > level {
|
2019-07-02 19:21:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
Log(v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLevel logs with the level specified
|
|
|
|
func WithLevelf(l Level, format string, v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
if l > level {
|
2019-07-02 19:21:43 +01:00
|
|
|
return
|
|
|
|
}
|
|
|
|
Logf(format, v...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Trace provides trace level logging
|
|
|
|
func Trace(v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
WithLevel(LevelTrace, v...)
|
2019-07-02 19:21:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Tracef provides trace level logging
|
|
|
|
func Tracef(format string, v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
WithLevelf(LevelTrace, format, v...)
|
2019-07-02 19:21:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debug provides debug level logging
|
|
|
|
func Debug(v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
WithLevel(LevelDebug, v...)
|
2019-07-02 19:21:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Debugf provides debug level logging
|
|
|
|
func Debugf(format string, v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
WithLevelf(LevelDebug, format, v...)
|
2019-07-02 19:21:43 +01:00
|
|
|
}
|
|
|
|
|
2019-11-11 15:57:14 +08: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-07-02 19:21:43 +01:00
|
|
|
// Info provides info level logging
|
|
|
|
func Info(v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
WithLevel(LevelInfo, v...)
|
2019-07-02 19:21:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// Infof provides info level logging
|
|
|
|
func Infof(format string, v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
WithLevelf(LevelInfo, format, v...)
|
2019-07-02 19:21:43 +01:00
|
|
|
}
|
|
|
|
|
2019-08-29 17:21:43 +01:00
|
|
|
// Error provides warn level logging
|
|
|
|
func Error(v ...interface{}) {
|
|
|
|
WithLevel(LevelError, v...)
|
2019-08-14 02:26:51 +03:00
|
|
|
}
|
|
|
|
|
2019-08-29 17:21:43 +01:00
|
|
|
// Errorf provides warn level logging
|
|
|
|
func Errorf(format string, v ...interface{}) {
|
|
|
|
WithLevelf(LevelError, format, v...)
|
2019-08-14 02:26:51 +03:00
|
|
|
}
|
|
|
|
|
2019-05-31 00:35:04 +01:00
|
|
|
// Fatal logs with Log and then exits with os.Exit(1)
|
|
|
|
func Fatal(v ...interface{}) {
|
2019-08-14 02:18:22 +03:00
|
|
|
WithLevel(LevelFatal, 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-08-14 02:18:22 +03:00
|
|
|
WithLevelf(LevelFatal, format, v...)
|
2019-05-31 00:35:04 +01:00
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLogger sets the local logger
|
2019-12-18 16:02:11 +00:00
|
|
|
func SetLogger(l log.Log) {
|
2019-05-31 00:35:04 +01:00
|
|
|
logger = l
|
|
|
|
}
|
2019-07-02 19:21:43 +01:00
|
|
|
|
|
|
|
// GetLogger returns the local logger
|
2019-12-18 16:02:11 +00:00
|
|
|
func GetLogger() log.Log {
|
2019-07-02 19:21:43 +01:00
|
|
|
return logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetLevel sets the log level
|
|
|
|
func SetLevel(l Level) {
|
2020-02-06 02:16:57 +08:00
|
|
|
atomic.StoreInt32((*int32)(&level), int32(l))
|
2019-07-02 19:21:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetLevel returns the current level
|
|
|
|
func GetLevel() Level {
|
|
|
|
return level
|
|
|
|
}
|
2019-10-02 17:42:34 +01:00
|
|
|
|
|
|
|
// Set a prefix for the logger
|
|
|
|
func SetPrefix(p string) {
|
|
|
|
prefix = p
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set service name
|
|
|
|
func Name(name string) {
|
|
|
|
prefix = fmt.Sprintf("[%s]", name)
|
|
|
|
}
|