2019-03-15 15:14:45 +00:00
|
|
|
package logging
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
2019-03-15 16:50:16 +00:00
|
|
|
"github.com/edouardparis/lntop/config"
|
2019-03-15 15:14:45 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Field = zapcore.Field
|
2019-03-15 16:02:16 +00:00
|
|
|
type ObjectEncoder = zapcore.ObjectEncoder
|
2019-03-15 15:14:45 +00:00
|
|
|
|
|
|
|
type Logger interface {
|
|
|
|
Info(string, ...Field)
|
|
|
|
Error(string, ...Field)
|
2019-03-15 16:02:16 +00:00
|
|
|
Sync() error
|
|
|
|
Debug(string, ...Field)
|
|
|
|
With(fields ...Field) *zap.Logger
|
2019-03-15 15:14:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func String(k, v string) Field {
|
|
|
|
return zap.String(k, v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Duration(k string, d time.Duration) Field {
|
|
|
|
return zap.Duration(k, d)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Int(k string, i int) Field {
|
|
|
|
return zap.Int(k, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Int64(k string, i int64) Field {
|
|
|
|
return zap.Int64(k, i)
|
|
|
|
}
|
|
|
|
|
|
|
|
func Error(v error) Field {
|
|
|
|
return zap.Error(v)
|
|
|
|
}
|
|
|
|
|
2019-03-15 16:02:16 +00:00
|
|
|
func Object(key string, val zapcore.ObjectMarshaler) Field {
|
|
|
|
return zap.Object(key, val)
|
|
|
|
}
|
|
|
|
|
2019-04-13 15:52:33 +00:00
|
|
|
func New(cfg config.Logger) (Logger, error) {
|
|
|
|
switch cfg.Type {
|
|
|
|
case "development":
|
|
|
|
return NewDevelopmentLogger(cfg.Dest)
|
|
|
|
case "production":
|
|
|
|
return NewProductionLogger(cfg.Dest)
|
|
|
|
default:
|
|
|
|
return NewDevelopmentLogger(cfg.Dest)
|
2019-03-15 16:50:16 +00:00
|
|
|
}
|
2019-03-15 15:14:45 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 13:02:59 +00:00
|
|
|
func NewProductionLogger(dest string) (Logger, error) {
|
|
|
|
config := zap.NewProductionConfig()
|
|
|
|
config.OutputPaths = []string{dest}
|
|
|
|
return config.Build()
|
2019-03-15 16:50:16 +00:00
|
|
|
}
|
|
|
|
|
2019-03-26 13:02:59 +00:00
|
|
|
func NewDevelopmentLogger(dest string) (Logger, error) {
|
|
|
|
config := zap.NewDevelopmentConfig()
|
|
|
|
config.OutputPaths = []string{dest}
|
|
|
|
return config.Build()
|
2019-03-15 16:50:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func NewNopLogger() (Logger, error) {
|
|
|
|
return zap.NewNop(), nil
|
2019-03-15 15:14:45 +00:00
|
|
|
}
|