Skip to content

Commit

Permalink
update document for log pkg
Browse files Browse the repository at this point in the history
  • Loading branch information
yinheli committed Jul 13, 2021
1 parent 5a0dda8 commit 65c1ad4
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 0 deletions.
3 changes: 3 additions & 0 deletions pkg/log/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ var (
loggerFileMapMutex sync.Mutex
)

// Cfg is the log config
type Cfg struct {
Level string `yaml:"level"`
File string `yaml:"file"`
Expand All @@ -44,6 +45,7 @@ func init() {
Config(v)
}

// Config for log configuration
func Config(v *viper.Viper) {
if err := v.UnmarshalKey("log", &cfg); err != nil {
Fatal("parse log config", zap.Error(err))
Expand All @@ -66,6 +68,7 @@ func Config(v *viper.Viper) {
logger = newLogger(zaplogger)
}

// NewLoggerFile create a new logger with file and use the global log configuration
func NewLogger(file string) (Logger, error) {
c := cfg
if c.File == file {
Expand Down
32 changes: 32 additions & 0 deletions pkg/log/echo_logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,131 +10,163 @@ import (
"github.com/labstack/gommon/log"
)

// EchoLogger echo logger
// see echo.Logger
type EchoLogger struct {
prefix string
logger Logger
}

// NewEchoLogger crate new echo log instance
func NewEchoLogger() *EchoLogger {
return &EchoLogger{
logger: Named(""),
}
}

// For with context.Context
func (t *EchoLogger) For(ctx context.Context) *EchoLogger {
return &EchoLogger{prefix: t.prefix, logger: t.logger.For(ctx)}
}

// Output get output
func (t *EchoLogger) Output() io.Writer {
return nil
}

// SetOutput not implemented, ignored
func (t *EchoLogger) SetOutput(w io.Writer) {}

// Prefix get prefix
func (t *EchoLogger) Prefix() string {
return t.prefix
}

// SetOutput set output prefix
func (t *EchoLogger) SetPrefix(p string) {
t.prefix = p
t.logger = Named(p)
}

// Level get log level
func (t *EchoLogger) Level() log.Lvl {
return log.INFO
}

// SetLvl set log level
func (t *EchoLogger) SetLevel(v log.Lvl) {}

// SetHeader set header, not implemented
func (t *EchoLogger) SetHeader(h string) {}

// Print log
func (t *EchoLogger) Print(i ...interface{}) {
t.logger.Info(strings.TrimSpace(fmt.Sprintln(i...)))
}

// Printf log formatted
func (t *EchoLogger) Printf(format string, args ...interface{}) {
t.logger.Info(strings.TrimSpace(fmt.Sprintf(format, args...)))
}

// Printj log json formatted
func (t *EchoLogger) Printj(j log.JSON) {
b, _ := json.Marshal(j)
t.logger.Info(strings.TrimSpace(string(b)))
}

// Debug log debug message
func (t *EchoLogger) Debug(i ...interface{}) {
t.logger.Debug(strings.TrimSpace(fmt.Sprintln(i...)))
}

// Debugf log debug formatted message
func (t *EchoLogger) Debugf(format string, args ...interface{}) {
t.logger.Debug(strings.TrimSpace(fmt.Sprintf(format, args...)))
}

// Debugj log json formatted
func (t *EchoLogger) Debugj(j log.JSON) {
b, _ := json.Marshal(j)
t.logger.Debug(strings.TrimSpace(string(b)))
}

// Info log
func (t *EchoLogger) Info(i ...interface{}) {
t.logger.Info(strings.TrimSpace(fmt.Sprintln(i...)))
}

// Infof log formatted
func (t *EchoLogger) Infof(format string, args ...interface{}) {
t.logger.Info(strings.TrimSpace(fmt.Sprintf(format, args...)))
}

// Infoj log json
func (t *EchoLogger) Infoj(j log.JSON) {
b, _ := json.Marshal(j)
t.logger.Info(strings.TrimSpace(string(b)))
}

// Warn log warn message
func (t *EchoLogger) Warn(i ...interface{}) {
t.logger.Warn(strings.TrimSpace(fmt.Sprintln(i...)))
}

// Warnf log warn formatted message
func (t *EchoLogger) Warnf(format string, args ...interface{}) {
t.logger.Warn(strings.TrimSpace(fmt.Sprintf(format, args...)))
}

// Warnj log warn json
func (t *EchoLogger) Warnj(j log.JSON) {
b, _ := json.Marshal(j)
t.logger.Warn(strings.TrimSpace(string(b)))
}

// Error log error message
func (t *EchoLogger) Error(i ...interface{}) {
t.logger.Error(strings.TrimSpace(fmt.Sprintln(i...)))
}

// Errorf log error formatted message
func (t *EchoLogger) Errorf(format string, args ...interface{}) {
t.logger.Error(strings.TrimSpace(fmt.Sprintf(format, args...)))
}

// Errorj log error json
func (t *EchoLogger) Errorj(j log.JSON) {
b, _ := json.Marshal(j)
t.logger.Error(strings.TrimSpace(string(b)))
}

// Fatal log fatal message
func (t *EchoLogger) Fatal(i ...interface{}) {
t.logger.Error(strings.TrimSpace(fmt.Sprintln(i...)))
}

// Fatalj log fatal json message
func (t *EchoLogger) Fatalj(j log.JSON) {
b, _ := json.Marshal(j)
t.logger.Error(strings.TrimSpace(string(b)))
}

// Fatalf log formatted message
func (t *EchoLogger) Fatalf(format string, args ...interface{}) {
t.logger.Error(strings.TrimSpace(fmt.Sprintf(format, args...)))
}

// Panic log panic message, which will cause panic
func (t *EchoLogger) Panic(i ...interface{}) {
t.logger.Fatal(strings.TrimSpace(fmt.Sprintln(i...)))
}

// Panicj log panic json message, which will cause panic
func (t *EchoLogger) Panicj(j log.JSON) {
b, _ := json.Marshal(j)
t.logger.Fatal(strings.TrimSpace(string(b)))
}

// Panicf log formatted message, which will cause panic
func (t *EchoLogger) Panicf(format string, args ...interface{}) {
t.logger.Fatal(strings.TrimSpace(fmt.Sprintf(format, args...)))
}
10 changes: 10 additions & 0 deletions pkg/log/log.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,38 +6,48 @@ import (
"go.uber.org/zap"
)

// Named name log
// see zap log named
func Named(name string) Logger {
return logger.Named(name)
}

// Debug log message
func Debug(msg string, fields ...zap.Field) {
logger.Debug(msg, fields...)
}

// Info log message
func Info(msg string, fields ...zap.Field) {
logger.Info(msg, fields...)
}

// Warn log message
func Warn(msg string, fields ...zap.Field) {
logger.Warn(msg, fields...)
}

// Error log message
func Error(msg string, fields ...zap.Field) {
logger.Error(msg, fields...)
}

// Fatal log message
func Fatal(msg string, fields ...zap.Field) {
logger.Fatal(msg, fields...)
}

// With log message
func With(fields ...zap.Field) Logger {
return logger.With(fields...)
}

// For log with context.Context, which will log trace_id and span_id if opentracing enabled
func For(ctx context.Context) Logger {
return logger.For(ctx)
}

// Get get zap log instance (global instance only)
func Get() *zap.Logger {
return zaplogger
}
29 changes: 29 additions & 0 deletions pkg/log/logger.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,56 +9,83 @@ import (
"go.uber.org/zap/zapcore"
)

// Logger log interface definition
type Logger interface {
// Named see zap Named
Named(name string) Logger

// Debug log message
Debug(msg string, fields ...zapcore.Field)

// Info log message
Info(msg string, fields ...zapcore.Field)

// Warn log message
Warn(msg string, fields ...zapcore.Field)

// Error log message
Error(msg string, fields ...zapcore.Field)

// Fatal log message
Fatal(msg string, fields ...zapcore.Field)

// With add zap fields
With(fields ...zapcore.Field) Logger

// For log with context.Context, which will log trace_id and span_id if opentracing enabled
For(ctx context.Context) Logger

// CallerSkip skip caller for adjust caller
CallerSkip(int) Logger
}

// default logger implementation
type defaultLogger struct {
logger *zap.Logger
additionalFields []zapcore.Field
skiped bool
}

// newLogger new logger instance
func newLogger(l *zap.Logger) Logger {
return &defaultLogger{
logger: l,
additionalFields: make([]zapcore.Field, 0, 8),
}
}

// Named see zap Named
func (t defaultLogger) Named(name string) Logger {
t.logger = t.logger.Named(name)
return t
}

// Debug log message
func (t defaultLogger) Debug(msg string, fields ...zapcore.Field) {
t.logger.Debug(msg, fields...)
}

// Info log message
func (t defaultLogger) Info(msg string, fields ...zapcore.Field) {
t.logger.Info(msg, fields...)
}

// Warn log message
func (t defaultLogger) Warn(msg string, fields ...zapcore.Field) {
t.logger.Warn(msg, fields...)
}

// Error log message
func (t defaultLogger) Error(msg string, fields ...zapcore.Field) {
t.logger.Error(msg, fields...)
}

// Fatal log message
func (t defaultLogger) Fatal(msg string, fields ...zapcore.Field) {
t.logger.Fatal(msg, fields...)
}

// With add zap fields
func (t defaultLogger) With(fields ...zapcore.Field) Logger {
return defaultLogger{
logger: t.logger.WithOptions(zap.AddCallerSkip(t.skip())).With(fields...),
Expand All @@ -67,6 +94,7 @@ func (t defaultLogger) With(fields ...zapcore.Field) Logger {
}
}

// For log with context.Context, which will log trace_id and span_id if opentracing enabled
func (t defaultLogger) For(ctx context.Context) Logger {
if span := opentracing.SpanFromContext(ctx); span != nil {
l := spanLogger{span: span, logger: t.logger.WithOptions(zap.AddCallerSkip(t.skip())), additionalFields: t.additionalFields}
Expand All @@ -84,6 +112,7 @@ func (t defaultLogger) For(ctx context.Context) Logger {
return defaultLogger{logger: t.logger.WithOptions(zap.AddCallerSkip(-1))}
}

// CallerSkip skip caller for adjust caller
func (t defaultLogger) CallerSkip(skip int) Logger {
t.logger = t.logger.WithOptions(zap.AddCallerSkip(skip))
return t
Expand Down
Loading

0 comments on commit 65c1ad4

Please sign in to comment.