1
0
mirror of https://github.com/go-micro/go-micro.git synced 2024-11-30 08:06:40 +02:00
go-micro/logger/logger.go

69 lines
1.6 KiB
Go
Raw Normal View History

// Package log provides a log interface
package logger
import (
"fmt"
"sync"
)
// Logger is a generic logging interface
type Logger interface {
2020-02-06 23:36:33 +02:00
// Init initialises options
Init(options ...Option) error
// The Logger options
Options() Options
// Error set `error` field to be logged
Error(err error) Logger
// Fields set fields to always be logged
Fields(fields map[string]interface{}) Logger
// Log writes a log entry
Log(level Level, v ...interface{})
// Logf writes a formatted log entry
Logf(level Level, format string, v ...interface{})
2020-02-06 23:36:33 +02:00
// String returns the name of logger
String() string
}
var (
2020-02-06 23:37:17 +02:00
mtx sync.Mutex
loggerMap = map[string]Logger{}
)
func Register(logger Logger) {
2020-02-06 23:37:17 +02:00
mtx.Lock()
defer mtx.Unlock()
loggerMap[logger.String()] = logger
}
func GetLogger(name string) (Logger, error) {
l := loggerMap[name]
if l == nil {
return nil, fmt.Errorf("no such name logger found %s", name)
}
return l, nil
}
// GetLevel converts a level string into a logger Level value.
// returns an error if the input string does not match known values.
func GetLevel(levelStr string) (Level, error) {
switch levelStr {
case TraceLevel.String():
return TraceLevel, nil
case DebugLevel.String():
return DebugLevel, nil
case InfoLevel.String():
return InfoLevel, nil
case WarnLevel.String():
return WarnLevel, nil
case ErrorLevel.String():
return ErrorLevel, nil
case FatalLevel.String():
return FatalLevel, nil
case PanicLevel.String():
return PanicLevel, nil
}
return InfoLevel, fmt.Errorf("Unknown Level String: '%s', defaulting to NoLevel", levelStr)
}