Documentation
¶
Overview ¶
Package log implements a leveled logger with a simple API.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var ( StdOutWriter = NewSimpWriter(os.Stdout) StdErrWriter = NewSimpWriter(os.Stderr) )
stdout and stderr writer is created by default.
var DefaultLevelWriter = NewLevelWriter( StdOutWriter, StdOutWriter, StdOutWriter, StdErrWriter, StdErrWriter, StdErrWriter, LLInfo, )
DefaultLevelWriter is the default LevelWriter.
var (
ErrClosedWriter = errors.New("closed writer")
)
Functions ¶
func WithTrace ¶
WithTrace assigns a trace ID to the context. when applying this context to the logger, the trace ID will be logged in the log message. The trace ID will be generated automatically. User can specify a name for the trace, which will be logged in the log message as well. If provided context have nested trace, they will be logged as a parent-child relationship.
Types ¶
type LevelLogger ¶
type LevelLogger interface {
Debug(args ...interface{})
// The method with suffix "xt" returns log function if the log level is
// enabled. It is useful to avoid unnecessary memory allocation when the
// log level is disabled. will improve performance when the log message is
// expensive to construct.
Debugxt() func(args ...interface{})
Info(args ...interface{})
Infoxt() func(args ...interface{})
Warn(args ...interface{})
Warnxt() func(args ...interface{})
Error(args ...interface{})
Errorxt() func(args ...interface{})
// Fatal logs a message at fatal level and exits the application os.Exit(1).
Fatal(args ...interface{})
// Panic logs a message at panic level and panics the application.
Panic(args ...interface{})
// Derivatively creates a new logger with context trace.
// An TraceID should be assigned by function WithTrace() before calling this
// method.
// The TraceID will be logged in the log message.
FromTrace(ctx context.Context) (LevelLogger, error)
// SetLevel sets the log level.
SetLevel(level Level)
}
LevelLogger represents a leveled logger.
func GetQuickLogger ¶
func GetQuickLogger(name string) LevelLogger
GetQuickLogger returns a quick logger by name.
func NewSimpleLogger ¶
func NewSimpleLogger( modulename string, timefmt string, writer LevelWriter, ) LevelLogger
NewSimpleLogger returns a new simple LevelLogger.
type LevelWriter ¶
type LevelWriter interface {
Write(level Level) RawWriter
SetLevel(level Level)
Clone() LevelWriter
}
LevelWriter provides a way to write log data with different levels.
func NewLevelWriter ¶
func NewLevelWriter( dbg, ifo, warn, err, ftl, pnc RawWriter, defaultLevel Level, ) LevelWriter
NewLevelWriter creates a new LevelWriter with the given RawWriters.
type RawWriter ¶
type RawWriter interface {
// WriteItem writes a single record to the writer.
//
// Upon the function's return, an internal EOR flag is triggered.
// This flag is used to separate log records, replacing the traditional line
// break, thereby allowing multiple lines within a single record.
//
// In such as database operations, each record can be stored as a separate
// row.
WriteItem(*logMeta, func(w io.Writer)) error
}
RawWriter represents a writer that writes raw log data.
func NewSimpWriter ¶
NewSimpFileWriter creates a new file writer with simple implementation.
type TraceID ¶
type TraceID [32]byte
TraceID represents a trace ID.
type TraceScope ¶
type TraceScope []TraceValue
TraceScope represents a trace chain. which includes a sequence of trace values. the last trace value is the current trace value, and the previous trace values are the parent traces.
func GetTrace ¶
func GetTrace(ctx context.Context) TraceScope
GetTrace returns the trace value from the context.
func (TraceScope) Format ¶
func (t TraceScope) Format(prefix, suffix, sep string) string
Format returns the formatted string representation of the trace chain.
func (TraceScope) String ¶
func (t TraceScope) String() string
String returns the string representation of the trace chain.
type TraceValue ¶
TraceValue represents a trace value.