micro/util/log/log.go

163 lines
2.9 KiB
Go
Raw Normal View History

2019-05-31 02:35:04 +03:00
// Package log is a global internal logger
package log
import (
2019-10-02 19:42:34 +03:00
"fmt"
2019-05-31 02:35:04 +03:00
"os"
"github.com/go-log/log"
golog "github.com/go-log/log/log"
)
2019-07-02 21:21:43 +03:00
// level is a log level
type Level int
const (
LevelFatal Level = iota
LevelInfo
2019-08-29 19:21:43 +03:00
LevelError
LevelDebug
LevelTrace
2019-07-02 21:21:43 +03:00
)
2019-05-31 02:35:04 +03:00
var (
// the local logger
logger log.Logger = golog.New()
2019-07-02 21:21:43 +03:00
// default log level is info
level = LevelInfo
2019-10-02 19:42:34 +03:00
// prefix for all messages
prefix string
2019-05-31 02:35:04 +03:00
)
func init() {
switch os.Getenv("MICRO_LOG_LEVEL") {
2019-08-29 19:21:43 +03:00
case "trace":
level = LevelTrace
case "debug":
level = LevelDebug
case "info":
level = LevelInfo
2019-08-29 19:21:43 +03:00
case "error":
level = LevelError
case "fatal":
level = LevelFatal
}
}
2019-05-31 02:35:04 +03:00
// Log makes use of github.com/go-log/log.Log
func Log(v ...interface{}) {
2019-10-02 19:42:34 +03:00
if len(prefix) > 0 {
logger.Log(append([]interface{}{prefix, " "}, v...)...)
return
}
2019-05-31 02:35:04 +03:00
logger.Log(v...)
}
// Logf makes use of github.com/go-log/log.Logf
func Logf(format string, v ...interface{}) {
2019-10-02 19:42:34 +03:00
if len(prefix) > 0 {
format = prefix + " " + format
}
2019-05-31 02:35:04 +03:00
logger.Logf(format, v...)
}
2019-07-02 21:21:43 +03:00
// WithLevel logs with the level specified
func WithLevel(l Level, v ...interface{}) {
if l > level {
2019-07-02 21:21:43 +03:00
return
}
Log(v...)
}
// WithLevel logs with the level specified
func WithLevelf(l Level, format string, v ...interface{}) {
if l > level {
2019-07-02 21:21:43 +03:00
return
}
Logf(format, v...)
}
// Trace provides trace level logging
func Trace(v ...interface{}) {
WithLevel(LevelTrace, v...)
2019-07-02 21:21:43 +03:00
}
// Tracef provides trace level logging
func Tracef(format string, v ...interface{}) {
WithLevelf(LevelTrace, format, v...)
2019-07-02 21:21:43 +03:00
}
// Debug provides debug level logging
func Debug(v ...interface{}) {
WithLevel(LevelDebug, v...)
2019-07-02 21:21:43 +03:00
}
// Debugf provides debug level logging
func Debugf(format string, v ...interface{}) {
WithLevelf(LevelDebug, format, v...)
2019-07-02 21:21:43 +03:00
}
// Info provides info level logging
func Info(v ...interface{}) {
WithLevel(LevelInfo, v...)
2019-07-02 21:21:43 +03:00
}
// Infof provides info level logging
func Infof(format string, v ...interface{}) {
WithLevelf(LevelInfo, format, v...)
2019-07-02 21:21:43 +03:00
}
2019-08-29 19:21:43 +03:00
// Error provides warn level logging
func Error(v ...interface{}) {
WithLevel(LevelError, v...)
}
2019-08-29 19:21:43 +03:00
// Errorf provides warn level logging
func Errorf(format string, v ...interface{}) {
WithLevelf(LevelError, format, v...)
}
2019-05-31 02:35:04 +03:00
// Fatal logs with Log and then exits with os.Exit(1)
func Fatal(v ...interface{}) {
WithLevel(LevelFatal, v...)
2019-05-31 02:35:04 +03:00
os.Exit(1)
}
// Fatalf logs with Logf and then exits with os.Exit(1)
func Fatalf(format string, v ...interface{}) {
WithLevelf(LevelFatal, format, v...)
2019-05-31 02:35:04 +03:00
os.Exit(1)
}
// SetLogger sets the local logger
func SetLogger(l log.Logger) {
logger = l
}
2019-07-02 21:21:43 +03: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
}
2019-10-02 19:42:34 +03: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)
}