Documentation
¶
Overview ¶
Package log provides support for logging to stdout, stderr and file.
Index ¶
- func Debug(ctx context.Context, v ...interface{})
- func Debugf(ctx context.Context, format string, v ...interface{})
- func Error(ctx context.Context, v ...interface{})
- func Errorf(ctx context.Context, format string, v ...interface{})
- func Fatal(ctx context.Context, v ...interface{})
- func Fatalf(ctx context.Context, format string, v ...interface{})
- func Info(ctx context.Context, v ...interface{})
- func Infof(ctx context.Context, format string, v ...interface{})
- func Panic(ctx context.Context, v ...interface{})
- func Panicf(ctx context.Context, format string, v ...interface{})
- func SetGlobalLogger(l *Logger)
- func Warn(ctx context.Context, v ...interface{})
- func Warnf(ctx context.Context, format string, v ...interface{})
- type Event
- func (e *Event) Bool(key string, value bool) *Event
- func (e *Event) Byte(key string, value byte) *Event
- func (e *Event) Bytes(key string, value []byte) *Event
- func (e *Event) Error(key string, err error) *Event
- func (e *Event) Float32(key string, value float32) *Event
- func (e *Event) Float64(key string, value float64) *Event
- func (e *Event) Free()
- func (e *Event) Int(key string, value int) *Event
- func (e *Event) Int32(key string, value int32) *Event
- func (e *Event) Int64(key string, value int64) *Event
- func (e *Event) Interface(key string, value interface{}) *Event
- func (e *Event) Message(message string)
- func (e *Event) Messagef(format string, v ...interface{})
- func (e *Event) String(key string, value string) *Event
- func (e *Event) Time(key string, value time.Time, format string) *Event
- func (e *Event) Uint(key string, value uint) *Event
- func (e *Event) Uint32(key string, value uint32) *Event
- func (e *Event) Uint64(key string, value uint64) *Event
- type EventCaller
- type EventCallerPool
- type EventPool
- type Flusher
- type Level
- type LevelWriter
- type Logger
- func GlobalLogger() *Logger
- func NewBufferedFileLogger(filePath string, flushInterval int, level ...string) (*Logger, error)
- func NewBufferedFileLoggerWithError(filePath, errFilePath string, flushInterval int, level ...string) (*Logger, error)
- func NewBufferedTerminalLogger(level ...string) (*Logger, error)
- func NewFileLogger(filePath string, level ...string) (*Logger, error)
- func NewFileLoggerWithError(filePath, errFilePath string, level ...string) (*Logger, error)
- func NewLogger(out io.Writer, level ...string) (*Logger, error)
- func NewLoggerWithError(out, errOut io.Writer, level ...string) (l *Logger, err error)
- func NewTerminalLogger(level ...string) (*Logger, error)
- func (l *Logger) Debug(ctx context.Context, v ...interface{})
- func (l *Logger) DebugEvent(ctx context.Context) *Event
- func (l *Logger) Debugf(ctx context.Context, format string, v ...interface{})
- func (l *Logger) Error(ctx context.Context, v ...interface{})
- func (l *Logger) ErrorEvent(ctx context.Context) *Event
- func (l *Logger) Errorf(ctx context.Context, format string, v ...interface{})
- func (l *Logger) Fatal(ctx context.Context, v ...interface{})
- func (l *Logger) FatalEvent(ctx context.Context) *Event
- func (l *Logger) Fatalf(ctx context.Context, format string, v ...interface{})
- func (l *Logger) Flush()
- func (l *Logger) GetLevel() string
- func (l *Logger) Info(ctx context.Context, v ...interface{})
- func (l *Logger) InfoEvent(ctx context.Context) *Event
- func (l *Logger) Infof(ctx context.Context, format string, v ...interface{})
- func (l *Logger) Panic(ctx context.Context, v ...interface{})
- func (l *Logger) PanicEvent(ctx context.Context) *Event
- func (l *Logger) Panicf(ctx context.Context, format string, v ...interface{})
- func (l *Logger) SetCallerFlag(isEnabled bool)
- func (l *Logger) SetInterestContextKeys(keys []interface{})
- func (l *Logger) SetLevel(level string) (err error)
- func (l *Logger) Warn(ctx context.Context, v ...interface{})
- func (l *Logger) WarnEvent(ctx context.Context) *Event
- func (l *Logger) Warnf(ctx context.Context, format string, v ...interface{})
- type StandardWriter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func SetGlobalLogger ¶
func SetGlobalLogger(l *Logger)
SetGlobalLogger sets a logger as global logger.
Types ¶
type Event ¶
type Event struct {
// contains filtered or unexported fields
}
Event represents a log event. It is instanced by one of the with method of logger and finalized by the log method such as Debug().
func DebugEvent ¶
DebugEvent returns a log event with severity DEBUG.
func ErrorEvent ¶
ErrorEvent returns a log event with severity ERROR.
func FatalEvent ¶
FatalEvent returns a log event with severity FATAL.
func PanicEvent ¶
PanicEvent returns a log event with severity PANIC.
func (*Event) Free ¶
func (e *Event) Free()
Free returns the Event to its EventPool. Callers must not retain references to the Event after calling Free.
func (*Event) Message ¶
Message writes the *Event to level writer.
NOTICE: Once this method is called, the *Event should be disposed. Calling twice can have unexpected result.
func (*Event) Messagef ¶
Messagef writes the *Event to level writer.
NOTICE: Once this method is called, the *Event should be disposed. Calling twice can have unexpected result.
type EventCaller ¶
type EventCaller struct { Defined bool PC uintptr File string Line int // contains filtered or unexported fields }
EventCaller represents the caller of a logging function.
func (*EventCaller) Free ¶
func (ec *EventCaller) Free()
Free returns the EventCaller to its EventCallerPool. Callers must not retain references to the EventCaller after calling Free.
func (EventCaller) FullPath ¶
func (ec EventCaller) FullPath() string
FullPath returns a /full/path/to/package/file:line description of the caller.
func (EventCaller) String ¶
func (ec EventCaller) String() string
String returns the full path and line number of the caller.
func (EventCaller) TrimmedPath ¶
func (ec EventCaller) TrimmedPath() string
TrimmedPath returns a package/file:line description of the caller, preserving only the leaf directory name and file name.
type EventCallerPool ¶
type EventCallerPool struct {
// contains filtered or unexported fields
}
A EventCallerPool is a type-safe wrapper around a sync.BytesBufferPool.
func NewEventCallerPool ¶
func NewEventCallerPool() EventCallerPool
NewEventCallerPool constructs a new BytesBufferPool.
func (EventCallerPool) Get ¶
func (p EventCallerPool) Get() *EventCaller
Get retrieves a EventCaller from the pool, creating one if necessary.
type EventPool ¶
type EventPool struct {
// contains filtered or unexported fields
}
A EventPool is a type-safe wrapper around a sync.BytesBufferPool.
type Flusher ¶
type Flusher interface {
Flush() error
}
Flusher defines a interface with Flush() method.
type Level ¶
type Level uint8
Level defines log levels.
const ( // MuteLevel disables the logger. MuteLevel Level = iota // FatalLevel defines fatal log level. FatalLevel // PanicLevel defines panic log level. PanicLevel // ErrorLevel defines error log level. ErrorLevel // WarnLevel defines warn log level. WarnLevel // InfoLevel defines info log level. InfoLevel // DebugLevel defines debug log level. DebugLevel )
func ParseLevel ¶
ParseLevel takes a string level and returns the log level constant.
type LevelWriter ¶
LevelWriter defines as interface a writer may implement in order to receive level information with payload.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger presents a logger. The only way to initialize a logger is using the convention construct functions like NewLogger().
func NewBufferedFileLogger ¶
NewBufferedFileLogger creates a logger that write into file with buffer. The flushSeconds's unit is second.
func NewBufferedFileLoggerWithError ¶
func NewBufferedFileLoggerWithError(filePath, errFilePath string, flushInterval int, level ...string) (*Logger, error)
NewBufferedFileLoggerWithError creates a logger that write into files with buffer. The flushSeconds's unit is second.
func NewBufferedTerminalLogger ¶
NewBufferedTerminalLogger creates a buffered logger that write into terminal.
func NewFileLogger ¶
NewFileLogger creates a logger that write into file.
func NewFileLoggerWithError ¶
NewFileLoggerWithError creates a logger that write into files.
func NewLoggerWithError ¶
NewLoggerWithError creates a new logger for given out, err out, level, and the err out can be nil, and the level is optional.
func NewTerminalLogger ¶
NewTerminalLogger creates a logger that write into terminal.
func (*Logger) DebugEvent ¶
DebugEvent returns a log event with severity DEBUG.
func (*Logger) ErrorEvent ¶
ErrorEvent returns a log event with severity ERROR.
func (*Logger) FatalEvent ¶
FatalEvent returns a log event with severity FATAL.
func (*Logger) Fatalf ¶
Fatalf logs a message with severity FATAL in format followed by a call to os.Exit(1).
func (*Logger) PanicEvent ¶
PanicEvent returns a log event with severity PANIC.
func (*Logger) Panicf ¶
Panicf logs a message with severity PANIC in format followed by a call to panic().
func (*Logger) SetCallerFlag ¶
SetCallerFlag sets whether to annotating logs with the caller.
func (*Logger) SetInterestContextKeys ¶
func (l *Logger) SetInterestContextKeys(keys []interface{})
SetInterestContextKeys sets the contexts keys that the logger should be interested in. Value of the interested context key will extract and print as newEvent filed.
func (*Logger) SetLevel ¶
SetLevel sets the log level. Valid levels are "debug", "info", "warn", "error", and "fatal".
type StandardWriter ¶
type StandardWriter struct {
// contains filtered or unexported fields
}
StandardWriter implements io.Writer{} and LevelWriter{} interface.
func (*StandardWriter) Flush ¶
func (sw *StandardWriter) Flush() (err error)
Flush implements the Flusher{} interface.
func (*StandardWriter) Write ¶
func (sw *StandardWriter) Write(p []byte) (n int, err error)
Write implements the io.Writer{} interface.
func (*StandardWriter) WriteLevel ¶
func (sw *StandardWriter) WriteLevel(level Level, message []byte) (n int, err error)
WriteLevel implements the LevelWriter{} interface.