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.

64 lines
1.3 KiB

3 years ago
package zap
import (
"context"
"gitoa.ru/go-4devs/log"
"gitoa.ru/go-4devs/log/entry"
"gitoa.ru/go-4devs/log/level"
"go.uber.org/zap"
)
func Nop() log.Logger {
return New(zap.NewNop())
3 years ago
}
func Example(options ...zap.Option) log.Logger {
return New(zap.NewExample(options...))
3 years ago
}
func Production(options ...zap.Option) log.Logger {
z, err := zap.NewProduction(options...)
3 years ago
if err != nil {
panic(err)
}
return New(z)
3 years ago
}
func Development(options ...zap.Option) log.Logger {
z, err := zap.NewDevelopment(options...)
if err != nil {
panic(err)
3 years ago
}
return New(z)
3 years ago
}
// New create handler by zap logger.
3 years ago
func New(logger *zap.Logger) log.Logger {
return func(ctx context.Context, data *entry.Entry) (int, error) {
zf := make([]zap.Field, data.Fields().Len())
for i, field := range data.Fields() {
zf[i] = zap.Any(string(field.Key()), field.AsInterface())
}
3 years ago
switch data.Level() {
case level.Emergency:
3 years ago
logger.Fatal(data.Message(), zf...)
case level.Alert:
3 years ago
logger.Panic(data.Message(), zf...)
case level.Critical, level.Error:
3 years ago
logger.Error(data.Message(), zf...)
case level.Warning:
3 years ago
logger.Warn(data.Message(), zf...)
case level.Notice, level.Info:
3 years ago
logger.Info(data.Message(), zf...)
case level.Debug:
3 years ago
logger.Debug(data.Message(), zf...)
}
return 0, nil
}
3 years ago
}