log

package
v0.0.7 Latest Latest
Warning

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

Go to latest
Published: Apr 19, 2020 License: MIT Imports: 14 Imported by: 3

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(v ...interface{})

Debug print debug.

func Debugf

func Debugf(format string, v ...interface{})

Debugf print debug with format.

func Error

func Error(v ...interface{})

Error print error.

func Errorf

func Errorf(format string, v ...interface{})

Errorf print error with format.

func Info

func Info(v ...interface{})

Info print info.

func Infof

func Infof(format string, v ...interface{})

Infof print info with format.

func Init

func Init(opts ...Option) error

Init init the root logger with fields.

func NewContext

func NewContext(ctx context.Context, logger Logger) context.Context

NewContext return a new logger context.

func NewHTTPContextHandler

func NewHTTPContextHandler(l Logger) func(http.Handler) http.Handler

NewHTTPContextHandler adds a context logger based on the given logger to each request. After a request passes through this handler, WithContext(req.Context()).Error(, "foo") will log to that logger and add useful context to each log entry.

func Panic

func Panic(v ...interface{})

Panic panic.

func Panicf

func Panicf(format string, v ...interface{})

Panicf panic with format.

func StreamInterceptor

func StreamInterceptor(l Logger) grpc.StreamServerInterceptor

StreamInterceptor returns a grpc.StreamServerInterceptor that provides a context logger with request_id. If a request-id is already specified in the metadata, it will be used. Otherwise a new one will be generated. For REST API, use Grpc-Metadata-Request-Id as header key for passing a request id.

func UnaryInterceptor

func UnaryInterceptor(l Logger) grpc.UnaryServerInterceptor

UnaryInterceptor returns a grpc.UnaryServerInterceptor that provides a context logger with request_id. If a request-id is already specified in the metadata, it will be used. Otherwise a new one will be generated. For REST API, use Grpc-Metadata-Request-Id as header key for passing a request id.

func Warn

func Warn(v ...interface{})

Warn print warning.

func Warnf

func Warnf(format string, v ...interface{})

Warnf print warning with format.

Types

type Format

type Format string

Format is log format

const (
	FormatJSON Format = "json"
	FormatText Format = "text"
)

Formats of log output.

type Level

type Level int32

Level is log level.

const (
	// LevelPanic level, highest level of severity. Logs and then calls panic with the
	// message passed to Debug, Info, ...
	LevelPanic Level = iota
	// LevelFatal level. Logs and then calls os.Exit. It will exit even if the
	// logging level is set to Panic.
	LevelFatal
	// LevelError level. Logs. Used for errors that should definitely be noted.
	// Commonly used for hooks to send errors to an error tracking service.
	LevelError
	// LevelWarn level. Non-critical entries that deserve eyes.
	LevelWarn
	// LevelInfo level. General operational entries about what's going on inside the
	// application.
	LevelInfo
	// LevelDebug level. Usually only enabled when debugging. Very verbose logging.
	LevelDebug
	// LevelTrace level. Designates finer-grained informational events than the Debug.
	LevelTrace
)

These are the different logging levels.

type Logger

type Logger interface {
	// Init init the logger.
	Init(...Option) error

	// Infof print info with format.
	Infof(format string, v ...interface{})

	// Debugf print debug with format.
	Debugf(format string, v ...interface{})

	// Warnf print warning with format.
	Warnf(format string, v ...interface{})

	// Errorf print error with format.
	Errorf(format string, v ...interface{})

	// Panicf panic with format.
	Panicf(format string, v ...interface{})

	// Info print info.
	Info(v ...interface{})

	// Debug print debug.
	Debug(v ...interface{})

	// Warn print warning.
	Warn(v ...interface{})

	// Error print error.
	Error(v ...interface{})

	// Panic panic.
	Panic(v ...interface{})

	// Fields return new logger with the given fields.
	// The kv should be provided as key values pairs where key is a string.
	Fields(kv ...interface{}) Logger

	// Context provide a way to get a context logger,  i.e... with request-id.
	Context(ctx context.Context) Logger
}

Logger defines standard operations of a logger.

func Context

func Context(ctx context.Context) Logger

Context return a logger from the given context.

func Fields

func Fields(kv ...interface{}) Logger

Fields return a new logger entry with fields.

func FromContext

func FromContext(ctx context.Context) Logger

FromContext get logger form context.

func Root

func Root() Logger

Root return default logger instance. Root will try to init a root logger base on environment configuration. It will panic if failed to init.

type Logrus added in v0.0.2

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

Logrus implement Logger interface using logrus.

func NewLogrus added in v0.0.2

func NewLogrus(opts ...Option) (*Logrus, error)

NewLogrus return new logger with context.

func (*Logrus) Context added in v0.0.2

func (l *Logrus) Context(ctx context.Context) Logger

Context return new logger from context.

func (*Logrus) Debug added in v0.0.2

func (l *Logrus) Debug(v ...interface{})

Debug print debug

func (*Logrus) Debugf added in v0.0.2

func (l *Logrus) Debugf(format string, v ...interface{})

Debugf print debug with format.

func (*Logrus) Error added in v0.0.2

func (l *Logrus) Error(v ...interface{})

Error print error

func (*Logrus) Errorf added in v0.0.2

func (l *Logrus) Errorf(format string, v ...interface{})

Errorf print error with format.

func (*Logrus) Fields added in v0.0.2

func (l *Logrus) Fields(kv ...interface{}) Logger

Fields return a new logger with fields.

func (*Logrus) Info added in v0.0.2

func (l *Logrus) Info(args ...interface{})

Info print info

func (*Logrus) Infof added in v0.0.2

func (l *Logrus) Infof(format string, v ...interface{})

Infof print info with format.

func (*Logrus) Init added in v0.0.2

func (l *Logrus) Init(opts ...Option) error

Init init the logger.

func (*Logrus) Panic added in v0.0.2

func (l *Logrus) Panic(v ...interface{})

Panic panic

func (*Logrus) Panicf added in v0.0.2

func (l *Logrus) Panicf(format string, v ...interface{})

Panicf panic with format.

func (*Logrus) Warn added in v0.0.2

func (l *Logrus) Warn(v ...interface{})

Warn print warning

func (*Logrus) Warnf added in v0.0.2

func (l *Logrus) Warnf(format string, v ...interface{})

Warnf print warning with format.

type Option

type Option = func(*Options)

Option is an option for configure logger.

func FromEnv

func FromEnv() Option

FromEnv provides an option to load all options from environment. LOG_LEVEL default:"5" which is debug level LOG_FORMAT default:"json" LOG_TIME_FORMAT default:"Mon, 02 Jan 2006 15:04:05 -0700" LOG_OUTPUT, default to be stdout, use file://my.log for writing to a file. LOG_FIELDS is a map of key/value. i.e: name:myservice,site:vietnam

func WithFields

func WithFields(kv ...interface{}) Option

WithFields provides an option to set logger fields.

func WithFormat

func WithFormat(f Format) Option

WithFormat provides an option to set log format.

func WithLevel

func WithLevel(level Level) Option

WithLevel provides an option to set log level.

func WithTimeFormat

func WithTimeFormat(f string) Option

WithTimeFormat provides an option to set time format for logger.

func WithWriter added in v0.0.2

func WithWriter(w io.Writer) Option

WithWriter provides an option to set a output writer.

type Options

type Options struct {
	Level      Level             `envconfig:"LOG_LEVEL" default:"5"`
	Format     Format            `envconfig:"LOG_FORMAT" default:"json"`
	TimeFormat string            `envconfig:"LOG_TIME_FORMAT" default:"Mon, 02 Jan 2006 15:04:05 -0700"`
	Output     string            `envconfig:"LOG_OUTPUT"`
	Fields     map[string]string `envconfig:"LOG_FIELDS"`
	// contains filtered or unexported fields
}

Options hold logger options

func (Options) GetFormat

func (opts Options) GetFormat() (Format, error)

GetFormat return format of output log. If given format is not valid, JSON format is returned.

func (Options) GetLevel

func (opts Options) GetLevel() (Level, error)

GetLevel return log level. If the given level is not valid, LevelDebug is returned.

func (Options) GetWriter added in v0.0.2

func (opts Options) GetWriter() (io.Writer, error)

GetWriter return writer output. If the given output is not valid, os.Stdout is returned.

Jump to

Keyboard shortcuts

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