mirror of
https://github.com/go-micro/go-micro.git
synced 2024-11-24 08:02:32 +02:00
1db36357d5
* feat(logger): add logger option to all components * fix: refactor api/rpc.go * fix: refactor api/stream.go * fix: api/options.go comment * fix(logger): do not use logger.Helper internally * fix(logger): fix comments * fix(logger): use level.Enabled method * fix: rename mlogger to log * fix: run go fmt * fix: log level * fix: factories Co-authored-by: Mohamed MHAMDI <mmhamdi@hubside.com> Co-authored-by: Davincible <david.brouwer.99@gmail.com>
139 lines
3.1 KiB
Go
139 lines
3.1 KiB
Go
package logger
|
|
|
|
import (
|
|
"context"
|
|
"os"
|
|
)
|
|
|
|
type Helper struct {
|
|
logger Logger
|
|
}
|
|
|
|
func NewHelper(logger Logger) *Helper {
|
|
return &Helper{logger: logger}
|
|
}
|
|
|
|
// Extract always returns valid Helper with logger from context or with DefaultLogger as fallback.
|
|
// Can be used in pair with function Inject.
|
|
// Example: propagate RequestID to logger in service handler methods.
|
|
func Extract(ctx context.Context) *Helper {
|
|
if l, ok := FromContext(ctx); ok {
|
|
return NewHelper(l)
|
|
}
|
|
|
|
return NewHelper(DefaultLogger)
|
|
}
|
|
|
|
func (h *Helper) Inject(ctx context.Context) context.Context {
|
|
return NewContext(ctx, h.logger)
|
|
}
|
|
|
|
func (h *Helper) Log(level Level, args ...interface{}) {
|
|
h.logger.Log(level, args...)
|
|
}
|
|
|
|
func (h *Helper) Logf(level Level, template string, args ...interface{}) {
|
|
h.logger.Logf(level, template, args...)
|
|
}
|
|
|
|
func (h *Helper) Info(args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(InfoLevel) {
|
|
return
|
|
}
|
|
h.logger.Log(InfoLevel, args...)
|
|
}
|
|
|
|
func (h *Helper) Infof(template string, args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(InfoLevel) {
|
|
return
|
|
}
|
|
h.logger.Logf(InfoLevel, template, args...)
|
|
}
|
|
|
|
func (h *Helper) Trace(args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(TraceLevel) {
|
|
return
|
|
}
|
|
h.logger.Log(TraceLevel, args...)
|
|
}
|
|
|
|
func (h *Helper) Tracef(template string, args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(TraceLevel) {
|
|
return
|
|
}
|
|
h.logger.Logf(TraceLevel, template, args...)
|
|
}
|
|
|
|
func (h *Helper) Debug(args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(DebugLevel) {
|
|
return
|
|
}
|
|
h.logger.Log(DebugLevel, args...)
|
|
}
|
|
|
|
func (h *Helper) Debugf(template string, args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(DebugLevel) {
|
|
return
|
|
}
|
|
h.logger.Logf(DebugLevel, template, args...)
|
|
}
|
|
|
|
func (h *Helper) Warn(args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(WarnLevel) {
|
|
return
|
|
}
|
|
h.logger.Log(WarnLevel, args...)
|
|
}
|
|
|
|
func (h *Helper) Warnf(template string, args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(WarnLevel) {
|
|
return
|
|
}
|
|
h.logger.Logf(WarnLevel, template, args...)
|
|
}
|
|
|
|
func (h *Helper) Error(args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(ErrorLevel) {
|
|
return
|
|
}
|
|
h.logger.Log(ErrorLevel, args...)
|
|
}
|
|
|
|
func (h *Helper) Errorf(template string, args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(ErrorLevel) {
|
|
return
|
|
}
|
|
h.logger.Logf(ErrorLevel, template, args...)
|
|
}
|
|
|
|
func (h *Helper) Fatal(args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(FatalLevel) {
|
|
return
|
|
}
|
|
h.logger.Log(FatalLevel, args...)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func (h *Helper) Fatalf(template string, args ...interface{}) {
|
|
if !h.logger.Options().Level.Enabled(FatalLevel) {
|
|
return
|
|
}
|
|
h.logger.Logf(FatalLevel, template, args...)
|
|
os.Exit(1)
|
|
}
|
|
|
|
func (h *Helper) WithError(err error) *Helper {
|
|
return &Helper{logger: h.logger.Fields(map[string]interface{}{"error": err})}
|
|
}
|
|
|
|
func (h *Helper) WithFields(fields map[string]interface{}) *Helper {
|
|
return &Helper{logger: h.logger.Fields(fields)}
|
|
}
|
|
|
|
func HelperOrDefault(h *Helper) *Helper {
|
|
if h == nil {
|
|
return DefaultHelper
|
|
}
|
|
return h
|
|
}
|