log

package
v0.2.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 23, 2022 License: MIT Imports: 18 Imported by: 28

Documentation

Index

Constants

View Source
const (
	StacktraceKey = "stacktrace"
	CallerSkip    = 1

	TraceIDKey = "trace_id"

	ComponentKey      = "component"
	WebComponentName  = "web"
	GrpcComponentName = "grpc"
)

Variables

View Source
var (
	GetLoggerWithCtx = func(ctx context.Context, l *Logger) *LoggerWithCtx {
		lc := loggerWithCtxPool.Get().(*LoggerWithCtx)
		lc.ctx = ctx
		lc.l = l
		return lc
	}
	PutLoggerWithCtx = func(lc *LoggerWithCtx) {
		lc.ctx = nil
		lc.l = nil
		loggerWithCtxPool.Put(lc)
	}
)

loggerWithCtxPool

View Source
var (

	// Get retrieves a buffer from the pool, creating one if necessary.
	Get = _pool.Get
)

Functions

func AppendToIncomingContext added in v0.1.0

func AppendToIncomingContext(ctx context.Context, fields ...zap.Field)

AppendToIncomingContext appends zap field to context logger

func DPanic

func DPanic(args ...any)

DPanic uses fmt.Sprint to construct and log a message. In development, the logger then panics. (See DPanicLevel for details.)

func DPanicf

func DPanicf(template string, args ...any)

DPanicf uses fmt.Sprintf to log a templated message. In development, the logger then panics. (See DPanicLevel for details.)

func Debug

func Debug(args ...any)

Debug uses fmt.Sprint to construct and log a message.

func Debugf

func Debugf(template string, args ...any)

Debugf uses fmt.Sprintf to log a templated message.

func DefaultTimeEncoder added in v0.0.3

func DefaultTimeEncoder(t time.Time, enc zapcore.PrimitiveArrayEncoder)

DefaultTimeEncoder serializes time.Time to a human-readable formatted string

func Error

func Error(args ...any)

Error uses fmt.Sprint to construct and log a message.

func Errorf

func Errorf(template string, args ...any)

Errorf uses fmt.Sprintf to log a templated message.

func Fatal

func Fatal(args ...any)

Fatal uses fmt.Sprint to construct and log a message, then calls os.Exit.

func Fatalf

func Fatalf(template string, args ...any)

Fatalf uses fmt.Sprintf to log a templated message, then calls os.Exit.

func Info

func Info(args ...any)

Info uses fmt.Sprint to construct and log a message.

func Infof

func Infof(template string, args ...any)

Infof uses fmt.Sprintf to log a templated message.

func NewIncomingContext added in v0.1.0

func NewIncomingContext(ctx context.Context, carrier *FieldCarrier, fields ...zap.Field) context.Context

NewIncomingContext creates a new context with logger carrier.

func NewTextEncoder

func NewTextEncoder(cfg *Config) zapcore.Encoder

NewTextEncoder creates a fast, low-allocation Text encoder. The encoder appropriately escapes all field keys and values. log format see https://github.com/tikv/rfcs/blob/master/text/0018-unified-log-format.md#log-fields-section

func Panic

func Panic(args ...any)

Panic uses fmt.Sprint to construct and log a message, then panics.

func Panicf

func Panicf(template string, args ...any)

Panicf uses fmt.Sprintf to log a templated message, then panics.

func PrintLogo()

func ShortCallerEncoder added in v0.0.3

func ShortCallerEncoder(caller zapcore.EntryCaller, enc zapcore.PrimitiveArrayEncoder)

ShortCallerEncoder serializes a caller in file:line format.

func StdPrintf

func StdPrintf(format string, v ...any)

func StdPrintln

func StdPrintln(v ...any)

StdPrintln use native log.Println

func Sync added in v0.0.3

func Sync() error

Sync calls the underlying Core's Sync method, flushing any buffered log entries. Applications should take care to call Sync before exiting.

func Warn

func Warn(args ...any)

Warn uses fmt.Sprint to construct and log a message.

func Warnf

func Warnf(template string, args ...any)

Warnf uses fmt.Sprintf to log a templated message.

Types

type ComponentLogger added in v0.0.3

type ComponentLogger interface {
	// Logger return component's logger,if withFields is true,return logger with build in fields
	Logger(opts ...GetComponentLoggerOption) *Logger
	SetLogger(logger *Logger)
	Debug(msg string, fields ...zap.Field)
	Info(msg string, fields ...zap.Field)
	Warn(msg string, fields ...zap.Field)
	Error(msg string, fields ...zap.Field)
	DPanic(msg string, fields ...zap.Field)
	Panic(msg string, fields ...zap.Field)
	Fatal(msg string, fields ...zap.Field)
	Ctx(ctx context.Context) *LoggerWithCtx
}

ComponentLogger is sample and base using for component that also carries a context.Context. It uses the global logger.

func Component added in v0.0.3

func Component(name string, fields ...zap.Field) ComponentLogger

Component return a logger with name option base on a logger.

The logger will be lazy set up,Using the global logger by default.

func Global

func Global() ComponentLogger

Global return the global logger

type Config

type Config struct {
	// ZapConfigs is for initial zap multi core
	ZapConfigs []zap.Config `json:"cores" yaml:"cores"`
	// Rotate is for log rotate
	Rotate *rotate `json:"rotate" yaml:"rotate"`
	// Disable automatic timestamps in output if use textEncoder
	DisableTimestamp bool `json:"disableTimestamp" yaml:"disableTimestamp"`
	// DisableErrorVerbose stops annotating logs with the full verbose error
	// message.
	DisableErrorVerbose bool `json:"disableErrorVerbose" yaml:"disableErrorVerbose"`
	// WithTraceID configures the logger to add `trace_id` field to structured log messages.
	WithTraceID bool `json:"withTraceID" yaml:"withTraceID"`
	// TraceIDKey is the key used to store the trace ID. defaults to "trace_id".
	TraceIDKey string `json:"traceIDKey" yaml:"traceIDKey"`
	// contains filtered or unexported fields
}

Config is logger schema ZapConfigs use as zap advance,such as zapcore.NewTee() Sole use as one zap logger core

func NewConfig

func NewConfig(cfg *conf.Configuration) (*Config, error)

NewConfig return a Config instance

func (*Config) BuildZap

func (c *Config) BuildZap(opts ...zap.Option) (zl *zap.Logger, err error)

BuildZap build a zap.Logger by Config

type ContextLogger added in v0.1.0

type ContextLogger interface {
	// LogFields defined how to log field with context
	LogFields(logger *Logger, ctx context.Context, lvl zapcore.Level, msg string, fields []zap.Field)
}

ContextLogger is functions to help ContextLogger logging,the functions are called each ComponentLogger call the logging method

type DefaultContextLogger added in v0.1.0

type DefaultContextLogger struct {
}

DefaultContextLogger is hold a nothing

func (*DefaultContextLogger) LogFields added in v0.1.0

func (n *DefaultContextLogger) LogFields(log *Logger, _ context.Context, lvl zapcore.Level, msg string, fields []zap.Field)

type FieldCarrier added in v0.1.0

type FieldCarrier struct {
	Fields []zapcore.Field
}

FieldCarrier sample to carry context log, the carrier's fields will log by demand.

func FromIncomingContext added in v0.1.0

func FromIncomingContext(ctx context.Context) (*FieldCarrier, bool)

FromIncomingContext returns the logger stored in ctx, if any.

func NewCarrier added in v0.1.0

func NewCarrier() *FieldCarrier

NewCarrier create a new logger carrier

type GetComponentLoggerOption added in v0.1.0

type GetComponentLoggerOption func(*GetComponentLoggerOptions)

ComponentLogger is sample and base using for component that also carries a context.Context. It uses the global logger.

func WithContextLogger added in v0.1.0

func WithContextLogger() GetComponentLoggerOption

WithContextLogger returns the logger with context in the ComponentLogger.

func WithOriginalLogger added in v0.1.0

func WithOriginalLogger() GetComponentLoggerOption

WithOriginalLogger returns the original logger in the ComponentLogger.

type GetComponentLoggerOptions added in v0.1.0

type GetComponentLoggerOptions struct {
	// contains filtered or unexported fields
}

ComponentLogger is sample and base using for component that also carries a context.Context. It uses the global logger.

type Logger

type Logger struct {
	*zap.Logger
	WithTraceID bool
	TraceIDKey  string
	// contains filtered or unexported fields
}

Logger integrate the Uber Zap library to use in woocoo

if you prefer to golang builtin log style,use log.Info or log.Infof, if zap style,you should use log.Operator().Info() if you want to clone Logger,you can call WithOption WithTraceID indicate whether to add trace_id field to the log.

  • web: from X-Request-Id header
  • grpc: from metadata key "trace_id"

func InitGlobalLogger added in v0.1.0

func InitGlobalLogger() *Logger

func New

func New(zl *zap.Logger) *Logger

New create an Instance from zap

func NewBuiltIn

func NewBuiltIn() *Logger

NewBuiltIn create a logger by configuration path "log", it will be set as global logger but run only once.

func (*Logger) Apply

func (l *Logger) Apply(cfg *conf.Configuration)

Apply implement Configurable interface which can initial from a file used in JSON,YAML Logger init trough Apply method will be set as Global.

func (*Logger) AsGlobal

func (l *Logger) AsGlobal() *Logger

AsGlobal set the Logger as global logger

func (*Logger) ContextLogger added in v0.1.0

func (l *Logger) ContextLogger() ContextLogger

ContextLogger return contextLogger field

func (*Logger) Ctx added in v0.1.0

func (l *Logger) Ctx(ctx context.Context) *LoggerWithCtx

Ctx returns a new logger with the context.

func (*Logger) Operator

func (l *Logger) Operator() *zap.Logger

Operator returns the underlying zap logger.

func (*Logger) SetContextLogger added in v0.1.0

func (l *Logger) SetContextLogger(f ContextLogger)

SetContextLogger set contextLogger field,if you use the contextLogger,can set or override it.

func (*Logger) With

func (l *Logger) With(fields ...zap.Field) *Logger

With creates a child logger and adds structured context to it. Fields added to the child don't affect the parent, and vice versa.

func (*Logger) WithOptions added in v0.1.0

func (l *Logger) WithOptions(opts ...zap.Option) *Logger

WithOptions clones the current Logger, applies the supplied Options, and returns the resulting Logger. It's safe to use concurrently.

type LoggerWithCtx added in v0.1.0

type LoggerWithCtx struct {
	// contains filtered or unexported fields
}

LoggerWithCtx is a wrapper for Logger that also carries a context.Context.

func NewLoggerWithCtx added in v0.1.0

func NewLoggerWithCtx(ctx context.Context, l *Logger) *LoggerWithCtx

NewLoggerWithCtx get a logger with context from pool

func (*LoggerWithCtx) DPanic added in v0.1.0

func (c *LoggerWithCtx) DPanic(msg string, fields ...zap.Field)

func (*LoggerWithCtx) Debug added in v0.1.0

func (c *LoggerWithCtx) Debug(msg string, fields ...zap.Field)

func (*LoggerWithCtx) Error added in v0.1.0

func (c *LoggerWithCtx) Error(msg string, fields ...zap.Field)

func (*LoggerWithCtx) Fatal added in v0.1.0

func (c *LoggerWithCtx) Fatal(msg string, fields ...zap.Field)

func (*LoggerWithCtx) Info added in v0.1.0

func (c *LoggerWithCtx) Info(msg string, fields ...zap.Field)

func (*LoggerWithCtx) Log added in v0.1.0

func (c *LoggerWithCtx) Log(lvl zapcore.Level, msg string, fields []zap.Field)

func (*LoggerWithCtx) Panic added in v0.1.0

func (c *LoggerWithCtx) Panic(msg string, fields ...zap.Field)

func (*LoggerWithCtx) Warn added in v0.1.0

func (c *LoggerWithCtx) Warn(msg string, fields ...zap.Field)

func (*LoggerWithCtx) WithOptions added in v0.1.0

func (c *LoggerWithCtx) WithOptions(opts ...zap.Option) *LoggerWithCtx

WithOptions reset the logger with options.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL