1
0
mirror of https://github.com/go-kratos/kratos.git synced 2025-01-10 00:29:01 +02:00
kratos/log/log.go

69 lines
1.3 KiB
Go
Raw Normal View History

2021-02-17 11:14:47 +02:00
package log
import (
"log"
)
2021-02-17 11:14:47 +02:00
var (
// DefaultLogger is default logger.
DefaultLogger Logger = NewStdLogger(log.Writer())
2021-02-17 11:14:47 +02:00
)
// Logger is a logger interface.
type Logger interface {
Print(pairs ...interface{})
}
type context struct {
logs []Logger
prefix []interface{}
2021-02-17 11:14:47 +02:00
}
func (c *context) Print(a ...interface{}) {
kvs := make([]interface{}, 0, len(c.prefix)+len(a))
kvs = append(kvs, c.prefix...)
kvs = append(kvs, a...)
for _, log := range c.logs {
log.Print(kvs...)
}
2021-02-17 11:14:47 +02:00
}
// With with logger fields.
func With(l Logger, a ...interface{}) Logger {
if c, ok := l.(*context); ok {
kvs := make([]interface{}, 0, len(c.prefix)+len(a))
kvs = append(kvs, a...)
kvs = append(kvs, c.prefix...)
return &context{
logs: c.logs,
prefix: kvs,
}
2021-02-17 11:14:47 +02:00
}
return &context{logs: []Logger{l}, prefix: a}
}
// Wrap wraps multi logger.
func Wrap(logs ...Logger) Logger {
return &context{logs: logs}
2021-02-17 11:14:47 +02:00
}
// Debug returns a debug logger.
func Debug(log Logger) Logger {
return With(log, LevelKey, LevelDebug)
}
// Info returns a info logger.
func Info(log Logger) Logger {
return With(log, LevelKey, LevelInfo)
}
// Warn return a warn logger.
func Warn(log Logger) Logger {
return With(log, LevelKey, LevelWarn)
}
// Error returns a error logger.
func Error(log Logger) Logger {
return With(log, LevelKey, LevelError)
}