You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
kratos/log/log.go

69 lines
1.3 KiB

4 years ago
package log
import (
"log"
)
4 years ago
var (
// DefaultLogger is default logger.
DefaultLogger Logger = NewStdLogger(log.Writer())
4 years ago
)
// Logger is a logger interface.
type Logger interface {
Print(pairs ...interface{})
}
type context struct {
logs []Logger
prefix []interface{}
4 years ago
}
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...)
}
4 years ago
}
// 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,
}
4 years ago
}
return &context{logs: []Logger{l}, prefix: a}
}
// Wrap wraps multi logger.
func Wrap(logs ...Logger) Logger {
return &context{logs: logs}
4 years ago
}
// 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)
}