Documentation
¶
Overview ¶
Package rainbowlog implements a comprehensive logging solution with multiple writer implementations including buffered, synchronized, and multi-writer capabilities. It supports structured logging with different encoders and offers flexible configuration options for various logging needs.
Index ¶
- Constants
- Variables
- func LongCallerMarshalFunc(file string, line int) string
- func SyncWriter(w io.Writer) io.Writer
- type BufferedWriter
- type CallerMarshalFunc
- type ConsolePacker
- func (j *ConsolePacker) Any(key string, i any)
- func (j *ConsolePacker) Bytes(key string, val []byte)
- func (j *ConsolePacker) Done()
- func (j *ConsolePacker) Dur(key string, unit, val time.Duration)
- func (j *ConsolePacker) Durs(key string, unit time.Duration, vals ...time.Duration)
- func (j *ConsolePacker) Err(err error)
- func (j *ConsolePacker) Float32(key string, val float32)
- func (j *ConsolePacker) Float32s(key string, vals ...float32)
- func (j *ConsolePacker) Float64(key string, val float64)
- func (j *ConsolePacker) Float64s(key string, vals ...float64)
- func (j *ConsolePacker) Hex(key string, val []byte)
- func (j *ConsolePacker) IPAddr(key string, ip net.IP)
- func (j *ConsolePacker) IPPrefix(key string, pfx net.IPNet)
- func (j *ConsolePacker) Int(key string, val int)
- func (j *ConsolePacker) Int8(key string, val int8)
- func (j *ConsolePacker) Int8s(key string, vals ...int8)
- func (j *ConsolePacker) Int16(key string, val int16)
- func (j *ConsolePacker) Int16s(key string, vals ...int16)
- func (j *ConsolePacker) Int32(key string, val int32)
- func (j *ConsolePacker) Int32s(key string, vals ...int32)
- func (j *ConsolePacker) Int64(key string, val int64)
- func (j *ConsolePacker) Int64s(key string, vals ...int64)
- func (j *ConsolePacker) Ints(key string, vals ...int)
- func (j *ConsolePacker) MACAddr(key string, ha net.HardwareAddr)
- func (j *ConsolePacker) Msg(msg string)
- func (j *ConsolePacker) Str(key, val string)
- func (j *ConsolePacker) Stringer(key string, val fmt.Stringer)
- func (j *ConsolePacker) Stringers(key string, vals ...fmt.Stringer)
- func (j *ConsolePacker) Strs(key string, vals ...string)
- func (j *ConsolePacker) Time(key, fmt string, val time.Time)
- func (j *ConsolePacker) Times(key, fmt string, vals ...time.Time)
- func (j *ConsolePacker) Uint(key string, val uint)
- func (j *ConsolePacker) Uint8(key string, val uint8)
- func (j *ConsolePacker) Uint8s(key string, vals ...uint8)
- func (j *ConsolePacker) Uint16(key string, val uint16)
- func (j *ConsolePacker) Uint16s(key string, vals ...uint16)
- func (j *ConsolePacker) Uint32(key string, val uint32)
- func (j *ConsolePacker) Uint32s(key string, vals ...uint32)
- func (j *ConsolePacker) Uint64(key string, val uint64)
- func (j *ConsolePacker) Uint64s(key string, vals ...uint64)
- func (j *ConsolePacker) Uints(key string, vals ...uint)
- type Encoder
- type EncoderParseFunc
- type ErrorMarshalFunc
- type ErrorStackMarshalFunc
- type Hook
- type HookFunc
- type LevelFieldMarshalFunc
- type LevelHook
- type LevelWriter
- type LogRecord
- func (r *LogRecord) Any(key string, i any) Record
- func (r *LogRecord) Bytes(key string, val []byte) Record
- func (r *LogRecord) Discard() Record
- func (r *LogRecord) Done()
- func (r *LogRecord) Dur(key string, unit, val time.Duration) Record
- func (r *LogRecord) Durs(key string, unit time.Duration, vals ...time.Duration) Record
- func (r *LogRecord) Err(err error) Record
- func (r *LogRecord) Float32(key string, val float32) Record
- func (r *LogRecord) Float32s(key string, vals ...float32) Record
- func (r *LogRecord) Float64(key string, val float64) Record
- func (r *LogRecord) Float64s(key string, vals ...float64) Record
- func (r *LogRecord) Hex(key string, val []byte) Record
- func (r *LogRecord) IPAddr(key string, ip net.IP) Record
- func (r *LogRecord) IPPrefix(key string, pfx net.IPNet) Record
- func (r *LogRecord) Int(key string, val int) Record
- func (r *LogRecord) Int8(key string, val int8) Record
- func (r *LogRecord) Int8s(key string, vals ...int8) Record
- func (r *LogRecord) Int16(key string, val int16) Record
- func (r *LogRecord) Int16s(key string, vals ...int16) Record
- func (r *LogRecord) Int32(key string, val int32) Record
- func (r *LogRecord) Int32s(key string, vals ...int32) Record
- func (r *LogRecord) Int64(key string, val int64) Record
- func (r *LogRecord) Int64s(key string, vals ...int64) Record
- func (r *LogRecord) Ints(key string, vals ...int) Record
- func (r *LogRecord) MACAddr(key string, ha net.HardwareAddr) Record
- func (r *LogRecord) Msg(msg string) Record
- func (r *LogRecord) Msgf(format string, v ...interface{}) Record
- func (r *LogRecord) Reset()
- func (r *LogRecord) Str(key, val string) Record
- func (r *LogRecord) Stringer(key string, val fmt.Stringer) Record
- func (r *LogRecord) Stringers(key string, vals ...fmt.Stringer) Record
- func (r *LogRecord) Strs(key string, vals ...string) Record
- func (r *LogRecord) Time(key, format string, val time.Time) Record
- func (r *LogRecord) Times(key, format string, vals ...time.Time) Record
- func (r *LogRecord) Uint(key string, val uint) Record
- func (r *LogRecord) Uint8(key string, val uint8) Record
- func (r *LogRecord) Uint8s(key string, vals ...uint8) Record
- func (r *LogRecord) Uint16(key string, val uint16) Record
- func (r *LogRecord) Uint16s(key string, vals ...uint16) Record
- func (r *LogRecord) Uint32(key string, val uint32) Record
- func (r *LogRecord) Uint32s(key string, vals ...uint32) Record
- func (r *LogRecord) Uint64(key string, val uint64) Record
- func (r *LogRecord) Uint64s(key string, vals ...uint64) Record
- func (r *LogRecord) Uints(key string, vals ...uint) Record
- func (r *LogRecord) UseIntDur() Record
- func (r *LogRecord) WithCallerSkip(skip int) Record
- func (r *LogRecord) WithDoneFunc(f func(msg string)) Record
- func (r *LogRecord) WithLabels(label ...string) Record
- func (r *LogRecord) WithLevel(lv level.Level) Record
- type Logger
- func (l *Logger) Debug() Record
- func (l *Logger) Error() Record
- func (l *Logger) Fatal() Record
- func (l *Logger) Flush() (err error)
- func (l *Logger) Info() Record
- func (l *Logger) Level(le level.Level) Record
- func (l *Logger) Panic() Record
- func (l *Logger) Record() Record
- func (l *Logger) SubLogger(opts ...Option) *Logger
- func (l *Logger) Warn() Record
- type NewRecordFunc
- type NilRecord
- func (n *NilRecord) Any(key string, i any) Record
- func (n *NilRecord) Bytes(key string, val []byte) Record
- func (n *NilRecord) Discard() Record
- func (n *NilRecord) Done()
- func (n *NilRecord) Dur(key string, unit time.Duration, val time.Duration) Record
- func (n *NilRecord) Durs(key string, unit time.Duration, vals ...time.Duration) Record
- func (n *NilRecord) Err(err error) Record
- func (n *NilRecord) Float32(key string, val float32) Record
- func (n *NilRecord) Float32s(key string, vals ...float32) Record
- func (n *NilRecord) Float64(key string, val float64) Record
- func (n *NilRecord) Float64s(key string, vals ...float64) Record
- func (n *NilRecord) Hex(key string, val []byte) Record
- func (n *NilRecord) IPAddr(key string, ip net.IP) Record
- func (n *NilRecord) IPPrefix(key string, pfx net.IPNet) Record
- func (n *NilRecord) Int(key string, val int) Record
- func (n *NilRecord) Int8(key string, val int8) Record
- func (n *NilRecord) Int8s(key string, vals ...int8) Record
- func (n *NilRecord) Int16(key string, val int16) Record
- func (n *NilRecord) Int16s(key string, vals ...int16) Record
- func (n *NilRecord) Int32(key string, val int32) Record
- func (n *NilRecord) Int32s(key string, vals ...int32) Record
- func (n *NilRecord) Int64(key string, val int64) Record
- func (n *NilRecord) Int64s(key string, vals ...int64) Record
- func (n *NilRecord) Ints(key string, vals ...int) Record
- func (n *NilRecord) MACAddr(key string, ha net.HardwareAddr) Record
- func (n *NilRecord) Msg(msg string) Record
- func (n *NilRecord) Msgf(format string, v ...interface{}) Record
- func (n *NilRecord) Reset()
- func (n *NilRecord) Str(key string, val string) Record
- func (n *NilRecord) Stringer(key string, val fmt.Stringer) Record
- func (n *NilRecord) Stringers(key string, vals ...fmt.Stringer) Record
- func (n *NilRecord) Strs(key string, vals ...string) Record
- func (n *NilRecord) Time(key string, format string, val time.Time) Record
- func (n *NilRecord) Times(key string, format string, vals ...time.Time) Record
- func (n *NilRecord) Uint(key string, val uint) Record
- func (n *NilRecord) Uint8(key string, val uint8) Record
- func (n *NilRecord) Uint8s(key string, vals ...uint8) Record
- func (n *NilRecord) Uint16(key string, val uint16) Record
- func (n *NilRecord) Uint16s(key string, vals ...uint16) Record
- func (n *NilRecord) Uint32(key string, val uint32) Record
- func (n *NilRecord) Uint32s(key string, vals ...uint32) Record
- func (n *NilRecord) Uint64(key string, val uint64) Record
- func (n *NilRecord) Uint64s(key string, vals ...uint64) Record
- func (n *NilRecord) Uints(key string, vals ...uint) Record
- func (n *NilRecord) UseIntDur() Record
- func (n *NilRecord) WithCallerSkip(skip int) Record
- func (n *NilRecord) WithDoneFunc(f func(msg string)) Record
- func (n *NilRecord) WithLabels(label ...string) Record
- func (n *NilRecord) WithLevel(lv level.Level) Record
- type Option
- func AppendsEncoderWriters(encoder Encoder, writers ...io.Writer) Option
- func AppendsHooks(hooks ...Hook) Option
- func WithCallerMarshalFunc(callerMarshalFunc CallerMarshalFunc) Option
- func WithConfig(config config.LoggerConfig) Option
- func WithConfigFile(configFile string) Option
- func WithConsolePrint(enable bool) Option
- func WithDefault() Option
- func WithErrorMarshalFunc(errorMarshalFunc ErrorMarshalFunc) Option
- func WithErrorStackMarshalFunc(errorStackMarshalFunc ErrorStackMarshalFunc) Option
- func WithLabels(labels ...string) Option
- func WithLevel(lv level.Level) Option
- func WithLevelFieldMarshalFunc(levelFieldMarshalFunc LevelFieldMarshalFunc) Option
- func WithMetaKeyColors(key string, colors ...int) Option
- func WithMetaKeys(keys ...string) Option
- func WithRainbowConsole(enable bool) Option
- func WithStack(enable bool) Option
- func WithTimeFormat(timeFormat string) Option
- type Record
- type RecordPackerForWriter
- func (j *RecordPackerForWriter) Any(key string, i any)
- func (j *RecordPackerForWriter) Bytes(key string, val []byte)
- func (j *RecordPackerForWriter) CallerSkip(skip int)
- func (j *RecordPackerForWriter) Done()
- func (j *RecordPackerForWriter) Dur(key string, unit, val time.Duration)
- func (j *RecordPackerForWriter) Durs(key string, unit time.Duration, vals ...time.Duration)
- func (j *RecordPackerForWriter) Err(err error)
- func (j *RecordPackerForWriter) Float32(key string, val float32)
- func (j *RecordPackerForWriter) Float32s(key string, vals ...float32)
- func (j *RecordPackerForWriter) Float64(key string, val float64)
- func (j *RecordPackerForWriter) Float64s(key string, vals ...float64)
- func (j *RecordPackerForWriter) Hex(key string, val []byte)
- func (j *RecordPackerForWriter) IPAddr(key string, ip net.IP)
- func (j *RecordPackerForWriter) IPPrefix(key string, pfx net.IPNet)
- func (j *RecordPackerForWriter) Int(key string, val int)
- func (j *RecordPackerForWriter) Int8(key string, val int8)
- func (j *RecordPackerForWriter) Int8s(key string, vals ...int8)
- func (j *RecordPackerForWriter) Int16(key string, val int16)
- func (j *RecordPackerForWriter) Int16s(key string, vals ...int16)
- func (j *RecordPackerForWriter) Int32(key string, val int32)
- func (j *RecordPackerForWriter) Int32s(key string, vals ...int32)
- func (j *RecordPackerForWriter) Int64(key string, val int64)
- func (j *RecordPackerForWriter) Int64s(key string, vals ...int64)
- func (j *RecordPackerForWriter) Ints(key string, vals ...int)
- func (j *RecordPackerForWriter) MACAddr(key string, ha net.HardwareAddr)
- func (j *RecordPackerForWriter) Msg(msg string)
- func (j *RecordPackerForWriter) Reset()
- func (j *RecordPackerForWriter) Str(key, val string)
- func (j *RecordPackerForWriter) Stringer(key string, val fmt.Stringer)
- func (j *RecordPackerForWriter) Stringers(key string, vals ...fmt.Stringer)
- func (j *RecordPackerForWriter) Strs(key string, vals ...string)
- func (j *RecordPackerForWriter) Time(key, fmt string, val time.Time)
- func (j *RecordPackerForWriter) Times(key, fmt string, vals ...time.Time)
- func (j *RecordPackerForWriter) Uint(key string, val uint)
- func (j *RecordPackerForWriter) Uint8(key string, val uint8)
- func (j *RecordPackerForWriter) Uint8s(key string, vals ...uint8)
- func (j *RecordPackerForWriter) Uint16(key string, val uint16)
- func (j *RecordPackerForWriter) Uint16s(key string, vals ...uint16)
- func (j *RecordPackerForWriter) Uint32(key string, val uint32)
- func (j *RecordPackerForWriter) Uint32s(key string, vals ...uint32)
- func (j *RecordPackerForWriter) Uint64(key string, val uint64)
- func (j *RecordPackerForWriter) Uint64s(key string, vals ...uint64)
- func (j *RecordPackerForWriter) Uints(key string, vals ...uint)
- type WriterEncoderPair
Examples ¶
Constants ¶
const ( // TimeFormatUnix defines a time format that makes time fields to be // serialized as Unix timestamp integers. TimeFormatUnix = "UNIX" // TimeFormatUnixMs defines a time format that makes time fields to be // serialized as Unix timestamp integers in milliseconds. TimeFormatUnixMs = "UNIXMS" // TimeFormatUnixMicro defines a time format that makes time fields to be // serialized as Unix timestamp integers in microseconds. TimeFormatUnixMicro = "UNIXMICRO" // TimeFormatUnixNano defines a time format that makes time fields to be // serialized as Unix timestamp integers in nanoseconds. TimeFormatUnixNano = "UNIXNANO" )
const ( ColorBold = 1 ColorUnderline = 4 ColorStrikeThrough = 9 ColorUnderlineBold = 21 )
const ( ColorBlack = iota + 30 ColorRed ColorGreen ColorYellow ColorBlue ColorMagenta ColorCyan ColorPale )
const ( ColorBgBlack = iota + 40 ColorBgRed ColorBgGreen ColorBgYellow ColorBgBlue ColorBgMagenta ColorBgCyan ColorBgPale )
const ( ColorHlBlack = iota + 90 ColorHlRed ColorHlGreen ColorHlYellow ColorHlBlue ColorHlMagenta ColorHlCyan ColorHlPale )
const ( ColorHlBgBlack = iota + 100 ColorHlBgRed ColorHlBgGreen ColorHlBgYellow ColorHlBgBlue ColorHlBgMagenta ColorHlBgCyan ColorHlBgPale )
const ( DefaultLevel = level.Debug DefaultLabel = "" DefaultConsolePrint = false DefaultConsoleColor = true )
Variables ¶
var ( // MsgFieldName is the field name for core message. MsgFieldName = "message" // ErrFieldName is the field name for err. ErrFieldName = "error" // ErrStackFieldName is the field name for err stack. ErrStackFieldName = "stack" MetaTimeFieldName = "_TIME_" MetaCallerFieldName = "_CALLER_" MetaLevelFieldName = "_LEVEL_" MetaLabelFieldName = "_LABEL_" GlobalDurationValueUseInt = false DefaultStack = false // ErrorHandler will be called whenever some error threw when logger working. // If not set, the error will be printed on the stderr. // This handler must be thread safe and non-blocking. ErrorHandler = func(err error) { fmt.Fprintf(os.Stderr, "rainbowlog: error found: %v\n", err) } GlobalTimeFormat = "2006-01-02 15:04:05.000" GlobalLevelFieldMarshalFunc LevelFieldMarshalFunc = func(l level.Level) string { return strings.ToUpper(l.String()) } // CallerSkipFrameCount is the number of stack frames to skip to find the caller. CallerSkipFrameCount = 0 // GlobalCallerMarshalFunc allows customization of global caller marshaling. GlobalCallerMarshalFunc CallerMarshalFunc = LongCallerMarshalFunc // GlobalErrorMarshalFunc allows customization of global error marshaling. GlobalErrorMarshalFunc ErrorMarshalFunc = func(err error) string { if err == nil { return "" } return err.Error() } // GlobalErrorStackMarshalFunc extract the stack from err if any. GlobalErrorStackMarshalFunc ErrorStackMarshalFunc = func(err error) any { return nil } TimestampFunc = func() time.Time { return time.Now() } GlobalEncoderParseFunc EncoderParseFunc = func(encoder string) Encoder { switch strings.ToLower(encoder) { case "json": return JsonEnc case "txt", "text": return TextEnc default: panic("unsupported encoder: " + encoder) } } )
var NewTextEncoder = func(metaKeys ...string) Encoder { return encoder.TextEncoder{MetaKeys: metaKeys} }
Functions ¶
func LongCallerMarshalFunc ¶ added in v0.0.12
LongCallerMarshalFunc is the default caller marshal function which returns the full file path with line number.
func SyncWriter ¶
SyncWriter wraps a writer to make it thread-safe. Each call to Write is synchronized with a mutex. This wrapper is useful for writers that are not thread-safe. Note: os.File operations on POSIX and Windows systems are already thread-safe and do not require this wrapper.
Types ¶
type BufferedWriter ¶ added in v0.0.8
type BufferedWriter struct {
// contains filtered or unexported fields
}
BufferedWriter is a buffered writer that uses a double buffering mechanism to improve write performance. It automatically switches to another buffer when the current buffer is full and asynchronously flushes the filled buffer data to the underlying writer.
func NewBufferedWriter ¶ added in v0.0.8
func NewBufferedWriter(w io.Writer, bufSize int) *BufferedWriter
NewBufferedWriter creates a new buffered writer. The parameter w is the underlying io.Writer, and bufSize is the size of each buffer. The return value is a BufferedWriter instance that implements the io.Writer interface.
func (*BufferedWriter) Close ¶ added in v0.0.8
func (bw *BufferedWriter) Close() error
Close closes the writer, ensuring all buffer data is flushed. This method waits for all asynchronous flush operations to complete before returning.
func (*BufferedWriter) Flush ¶ added in v0.0.8
func (bw *BufferedWriter) Flush() error
Flush forcibly flushes the data in the current buffer to the underlying writer.
type CallerMarshalFunc ¶
func NewShortCallerMarshalFunc ¶ added in v0.0.12
func NewShortCallerMarshalFunc(callerTruncationIdentifier string) CallerMarshalFunc
NewShortCallerMarshalFunc creates a caller marshal function that returns a shortened file path with line number. It truncates the file path based on the provided callerTruncationIdentifier to make it more readable. The shortened paths are cached in a map for better performance.
type ConsolePacker ¶
type ConsolePacker struct {
RecordPackerForWriter
// contains filtered or unexported fields
}
func (*ConsolePacker) Any ¶ added in v0.0.4
func (j *ConsolePacker) Any(key string, i any)
func (*ConsolePacker) Bytes ¶ added in v0.0.4
func (j *ConsolePacker) Bytes(key string, val []byte)
func (*ConsolePacker) Done ¶
func (j *ConsolePacker) Done()
func (*ConsolePacker) Dur ¶ added in v0.0.4
func (j *ConsolePacker) Dur(key string, unit, val time.Duration)
func (*ConsolePacker) Err ¶
func (j *ConsolePacker) Err(err error)
func (*ConsolePacker) Float32 ¶ added in v0.0.4
func (j *ConsolePacker) Float32(key string, val float32)
func (*ConsolePacker) Float32s ¶ added in v0.0.4
func (j *ConsolePacker) Float32s(key string, vals ...float32)
func (*ConsolePacker) Float64 ¶ added in v0.0.4
func (j *ConsolePacker) Float64(key string, val float64)
func (*ConsolePacker) Float64s ¶ added in v0.0.4
func (j *ConsolePacker) Float64s(key string, vals ...float64)
func (*ConsolePacker) Hex ¶ added in v0.0.4
func (j *ConsolePacker) Hex(key string, val []byte)
func (*ConsolePacker) IPAddr ¶ added in v0.0.4
func (j *ConsolePacker) IPAddr(key string, ip net.IP)
func (*ConsolePacker) IPPrefix ¶ added in v0.0.4
func (j *ConsolePacker) IPPrefix(key string, pfx net.IPNet)
func (*ConsolePacker) Int ¶ added in v0.0.4
func (j *ConsolePacker) Int(key string, val int)
func (*ConsolePacker) Int8 ¶ added in v0.0.4
func (j *ConsolePacker) Int8(key string, val int8)
func (*ConsolePacker) Int8s ¶ added in v0.0.4
func (j *ConsolePacker) Int8s(key string, vals ...int8)
func (*ConsolePacker) Int16 ¶ added in v0.0.4
func (j *ConsolePacker) Int16(key string, val int16)
func (*ConsolePacker) Int16s ¶ added in v0.0.4
func (j *ConsolePacker) Int16s(key string, vals ...int16)
func (*ConsolePacker) Int32 ¶ added in v0.0.4
func (j *ConsolePacker) Int32(key string, val int32)
func (*ConsolePacker) Int32s ¶ added in v0.0.4
func (j *ConsolePacker) Int32s(key string, vals ...int32)
func (*ConsolePacker) Int64 ¶ added in v0.0.4
func (j *ConsolePacker) Int64(key string, val int64)
func (*ConsolePacker) Int64s ¶ added in v0.0.4
func (j *ConsolePacker) Int64s(key string, vals ...int64)
func (*ConsolePacker) Ints ¶ added in v0.0.4
func (j *ConsolePacker) Ints(key string, vals ...int)
func (*ConsolePacker) MACAddr ¶ added in v0.0.4
func (j *ConsolePacker) MACAddr(key string, ha net.HardwareAddr)
func (*ConsolePacker) Msg ¶
func (j *ConsolePacker) Msg(msg string)
func (*ConsolePacker) Str ¶ added in v0.0.4
func (j *ConsolePacker) Str(key, val string)
func (*ConsolePacker) Stringer ¶ added in v0.0.4
func (j *ConsolePacker) Stringer(key string, val fmt.Stringer)
func (*ConsolePacker) Stringers ¶ added in v0.0.4
func (j *ConsolePacker) Stringers(key string, vals ...fmt.Stringer)
func (*ConsolePacker) Strs ¶ added in v0.0.4
func (j *ConsolePacker) Strs(key string, vals ...string)
func (*ConsolePacker) Time ¶ added in v0.0.4
func (j *ConsolePacker) Time(key, fmt string, val time.Time)
func (*ConsolePacker) Times ¶ added in v0.0.4
func (j *ConsolePacker) Times(key, fmt string, vals ...time.Time)
func (*ConsolePacker) Uint ¶ added in v0.0.4
func (j *ConsolePacker) Uint(key string, val uint)
func (*ConsolePacker) Uint8 ¶ added in v0.0.4
func (j *ConsolePacker) Uint8(key string, val uint8)
func (*ConsolePacker) Uint8s ¶ added in v0.0.4
func (j *ConsolePacker) Uint8s(key string, vals ...uint8)
func (*ConsolePacker) Uint16 ¶ added in v0.0.4
func (j *ConsolePacker) Uint16(key string, val uint16)
func (*ConsolePacker) Uint16s ¶ added in v0.0.4
func (j *ConsolePacker) Uint16s(key string, vals ...uint16)
func (*ConsolePacker) Uint32 ¶ added in v0.0.4
func (j *ConsolePacker) Uint32(key string, val uint32)
func (*ConsolePacker) Uint32s ¶ added in v0.0.4
func (j *ConsolePacker) Uint32s(key string, vals ...uint32)
func (*ConsolePacker) Uint64 ¶ added in v0.0.4
func (j *ConsolePacker) Uint64(key string, val uint64)
func (*ConsolePacker) Uint64s ¶ added in v0.0.4
func (j *ConsolePacker) Uint64s(key string, vals ...uint64)
func (*ConsolePacker) Uints ¶ added in v0.0.4
func (j *ConsolePacker) Uints(key string, vals ...uint)
type Encoder ¶
type Encoder interface {
BeginMarker(dst []byte) []byte
BlankSpace(dst []byte) []byte
Bool(dst []byte, val bool) []byte
Bytes(dst []byte, s []byte) []byte
Comma(dst []byte) []byte
Delim(dst []byte) []byte
Duration(dst []byte, unit time.Duration, useInt bool, d time.Duration) []byte
EndMarker(dst []byte) []byte
Float32(dst []byte, val float32) []byte
Float64(dst []byte, val float64) []byte
Hex(dst []byte, s []byte) []byte
IPAddr(dst []byte, ip net.IP) []byte
IPPrefix(dst []byte, pfx net.IPNet) []byte
Int(dst []byte, val int) []byte
Int16(dst []byte, val int16) []byte
Int32(dst []byte, val int32) []byte
Int64(dst []byte, val int64) []byte
Int8(dst []byte, val int8) []byte
Interface(dst []byte, i interface{}) []byte
Key(dst []byte, key string) []byte
LineBreak(dst []byte) []byte
MACAddr(dst []byte, ha net.HardwareAddr) []byte
Nil(dst []byte) []byte
ObjectData(dst []byte, o []byte) []byte
String(dst []byte, s string) []byte
Time(dst []byte, format string, t time.Time) []byte
Uint(dst []byte, val uint) []byte
Uint16(dst []byte, val uint16) []byte
Uint32(dst []byte, val uint32) []byte
Uint64(dst []byte, val uint64) []byte
Uint8(dst []byte, val uint8) []byte
MetaEnd(dst []byte) []byte
// contains filtered or unexported methods
}
var JsonEnc Encoder = encoder.JsonEncoder{}
var TextEnc Encoder = encoder.TextEncoder{
MetaKeys: defaultMetaKeys().Keys(),
}
type EncoderParseFunc ¶
type ErrorMarshalFunc ¶
type ErrorStackMarshalFunc ¶
type Hook ¶
type Hook interface {
// RunHook runs the hook with the event.
RunHook(r Record, level level.Level, message string)
}
Hook defines an interface to a Record hook.
type LevelFieldMarshalFunc ¶
type LevelHook ¶
type LevelHook struct {
NoneLevelHook, TraceHook, DebugHook, InfoHook, WarnHook, ErrorHook, FatalHook, PanicHook Hook
}
LevelHook applies a different hook for each level.
func NewLevelHook ¶
func NewLevelHook() LevelHook
type LevelWriter ¶
type LevelWriter interface {
io.Writer
WriteLevel(level level.Level, bz []byte) (n int, err error)
}
LevelWriter is an interface that wraps the standard io.Writer interface and adds a WriteLevel method to allow writing data with log level information.
func BufferedLevelWriter ¶ added in v0.0.8
func BufferedLevelWriter(w io.Writer, bufSize int) LevelWriter
BufferedLevelWriter creates a new LevelWriter that wraps the provided io.Writer with buffering capabilities. It uses the BufferedWriter to buffer writes and flushes them asynchronously for improved performance. The bufSize parameter specifies the size of each internal buffer.
func LevelWriterAdapter ¶
func LevelWriterAdapter(writer io.Writer) LevelWriter
LevelWriterAdapter converts an io.Writer to a LevelWriter. If the writer already implements LevelWriter, it is returned as-is. Otherwise, it is wrapped in a levelWriterAdapter.
func MultiLevelWriter ¶
func MultiLevelWriter(writers ...io.Writer) LevelWriter
MultiLevelWriter creates a writer that duplicates its writes to all the provided writers, similar to the Unix tee(1) command. Each provided writer is wrapped with LevelWriterAdapter to ensure it implements the LevelWriter interface.
type LogRecord ¶ added in v0.0.4
type LogRecord struct {
// contains filtered or unexported fields
}
LogRecord represents a log Record. It is finalized by the Done method. Done method also writes the encoded bytes to the writer.
func (*LogRecord) Done ¶ added in v0.0.4
func (r *LogRecord) Done()
Done finish appending data and writing Record.
NOTICE: once this method is called, the *Record should be disposed. Calling Done twice can have unexpected result.
func (*LogRecord) Err ¶ added in v0.0.4
Err sets the given err to the error field when err is not nil。 NOTICE: This method should only be called once。 Calling multiple times may cause unpredictable results。
func (*LogRecord) MACAddr ¶ added in v0.0.4
func (r *LogRecord) MACAddr(key string, ha net.HardwareAddr) Record
func (*LogRecord) Msg ¶ added in v0.0.4
Msg adds the msg as the message field if not empty. NOTICE: This method should only be called once。 Calling multiple times may cause unpredictable results。
func (*LogRecord) Msgf ¶ added in v0.0.4
Msgf adds the formatted msg as the message field if not empty.
func (*LogRecord) Reset ¶ added in v0.0.4
func (r *LogRecord) Reset()
Reset the Record instance. This should be called before the Record is used again.
func (*LogRecord) UseIntDur ¶ added in v0.0.4
UseIntDur enables the calculation of Duration to retain only integer digits.
func (*LogRecord) WithCallerSkip ¶ added in v0.0.4
WithCallerSkip adds skip frames when calling caller.
func (*LogRecord) WithDoneFunc ¶ added in v0.0.4
func (*LogRecord) WithLabels ¶ added in v0.0.4
WithLabels sets the labels for the Logger using one or more strings. The labels are joined using the "|" separator.
type Logger ¶
type Logger struct {
// contains filtered or unexported fields
}
Logger is the rainbow-log structure. This is the main entrance of rainbow log.
func New ¶
New creates a new *Logger with options optional.
Example ¶
package main
import (
"os"
"github.com/rambollwong/rainbowlog"
)
func main() {
log := rainbowlog.New(
rainbowlog.AppendsEncoderWriters(rainbowlog.JsonEnc, os.Stdout),
rainbowlog.WithMetaKeys(rainbowlog.MetaLevelFieldName, rainbowlog.MsgFieldName),
)
log.Info().Msg("Hello world!").Done()
log2 := rainbowlog.New(
rainbowlog.AppendsEncoderWriters(rainbowlog.TextEnc, os.Stdout),
rainbowlog.WithMetaKeys(rainbowlog.MetaLevelFieldName, rainbowlog.MsgFieldName))
log2.Debug().Msg("Hello world!").Done()
}
Output: {"_LEVEL_":"INFO","message":"Hello world!"} DEBUG > Hello world!
func (*Logger) Flush ¶ added in v0.0.9
Flush forces any buffered data to be written out to all writers that implement the Flush() method. It iterates through all the writer-encoder pairs and checks if the writer supports flushing. For multiLevelWriter, it recursively flushes all underlying writers that support flushing. Returns the first error encountered during flushing, or nil if all flush operations succeed.
func (*Logger) Info ¶
Info create a new Record with info level setting.
Example ¶
package main
import (
"os"
"github.com/rambollwong/rainbowlog"
)
func main() {
log := rainbowlog.New(
rainbowlog.AppendsEncoderWriters(rainbowlog.JsonEnc, os.Stdout),
rainbowlog.WithMetaKeys(rainbowlog.MetaLevelFieldName),
)
log.Info().Msg("Hello world!").Done()
}
Output: {"_LEVEL_":"INFO","message":"Hello world!"}
func (*Logger) Record ¶
Record create a new Record with basic.
Example ¶
package main
import (
"os"
"github.com/rambollwong/rainbowlog"
"github.com/rambollwong/rainbowlog/level"
)
func main() {
log := rainbowlog.New(
rainbowlog.AppendsEncoderWriters(rainbowlog.JsonEnc, os.Stdout),
rainbowlog.WithMetaKeys(rainbowlog.MetaLevelFieldName),
)
log.Record().WithLevel(level.Info).Msg("Hello world!").Done()
log.Record().WithLevel(level.Debug).Msg("Hello world!").Done()
}
Output: {"_LEVEL_":"INFO","message":"Hello world!"} {"_LEVEL_":"DEBUG","message":"Hello world!"}
func (*Logger) SubLogger ¶
SubLogger creates a new *Logger that inherit from parent logger. Optional options for sub logger also be supported.
Example ¶
package main
import (
"os"
"github.com/rambollwong/rainbowlog"
)
func main() {
log := rainbowlog.New(rainbowlog.AppendsEncoderWriters(rainbowlog.JsonEnc, os.Stdout))
subLog := log.SubLogger(
rainbowlog.WithMetaKeys(rainbowlog.MetaLabelFieldName),
rainbowlog.WithLabels("NewLabel"),
)
subLog.Info().Msg("Hello world!").Done()
}
Output: {"_LABEL_":"NewLabel","message":"Hello world!"}
type NewRecordFunc ¶
type NewRecordFunc func() Record
type NilRecord ¶ added in v0.0.4
type NilRecord struct {
}
func (*NilRecord) MACAddr ¶ added in v0.0.4
func (n *NilRecord) MACAddr(key string, ha net.HardwareAddr) Record
func (*NilRecord) WithCallerSkip ¶ added in v0.0.4
func (*NilRecord) WithDoneFunc ¶ added in v0.0.4
func (*NilRecord) WithLabels ¶ added in v0.0.4
type Option ¶
type Option func(logger *Logger)
Option defines logger options for custom using.
func AppendsEncoderWriters ¶
AppendsEncoderWriters appends writers who use a same encoder to logger.
func WithCallerMarshalFunc ¶
func WithCallerMarshalFunc(callerMarshalFunc CallerMarshalFunc) Option
WithCallerMarshalFunc sets the CallerMarshalFunc for logger. If MetaCallerFieldName has been set by WithMetaKeys option (if WithMetaKeys is not appended, MetaCallerFieldName is set by default), CallerMarshalFunc will be invoked when printing logs.
func WithConfig ¶
func WithConfig(config config.LoggerConfig) Option
WithConfig sets the Logger's properties according to the provided configuration parameters. If the Enable field in the configuration is false, the logger level will be set Disabled. Normally, the WithDefault() option should be set before calling this option.
func WithConfigFile ¶
WithConfigFile loads the log configuration from the specified configuration file, sets the properties of the Logger according to the configuration parameters. If an error occurs while loading the configuration file, trigger a panic.
func WithConsolePrint ¶
WithConsolePrint sets whether enable printing to console.
func WithDefault ¶
func WithDefault() Option
WithDefault sets default configurations to logger. If you use this option, please put it at the top of all options, otherwise it may overwrite the modified configuration as the default.
func WithErrorMarshalFunc ¶
func WithErrorMarshalFunc(errorMarshalFunc ErrorMarshalFunc) Option
WithErrorMarshalFunc sets the ErrorMarshalFunc for logger. If Record.Err() has been invoked, ErrorMarshalFunc will be invoked when printing logs. The marshal result string will be used as the value of error key field.
func WithErrorStackMarshalFunc ¶
func WithErrorStackMarshalFunc(errorStackMarshalFunc ErrorStackMarshalFunc) Option
WithErrorStackMarshalFunc sets the ErrorStackMarshalFunc for logger.
func WithLabels ¶ added in v0.0.2
WithLabels sets the labels for the Logger using one or more strings. The labels are joined using the "|" separator and assigned to the Logger's label property.
func WithLevelFieldMarshalFunc ¶
func WithLevelFieldMarshalFunc(levelFieldMarshalFunc LevelFieldMarshalFunc) Option
WithLevelFieldMarshalFunc sets the LevelFieldMarshalFunc for logger. LevelFieldMarshalFunc will be invoked when printing logs, then the result string will be used as the value of level key field.
func WithMetaKeyColors ¶
WithMetaKeyColors sets an ANSI code of color for printing the meta key.
NOTICE: MetaKeyColors only useful for console printing if rainbow console enabled. Colors defined in globals.go
func WithMetaKeys ¶
WithMetaKeys sets meta key field names for each Record. Meta keys data will be placed at the front of the Record.
func WithRainbowConsole ¶
WithRainbowConsole sets whether enable rainbow printing on console. This option is useful only when ConsolePrint enabled.
func WithTimeFormat ¶
WithTimeFormat sets the format of time for logger printing.
type Record ¶
type Record interface {
WithLabels(label ...string) Record
WithLevel(lv level.Level) Record
WithDoneFunc(f func(msg string)) Record
WithCallerSkip(skip int) Record
UseIntDur() Record
Reset()
Discard() Record
Done()
Msg(msg string) Record
Msgf(format string, v ...interface{}) Record
Err(err error) Record
Str(key string, val string) Record
Strs(key string, vals ...string) Record
Stringer(key string, val fmt.Stringer) Record
Stringers(key string, vals ...fmt.Stringer) Record
Bytes(key string, val []byte) Record
Hex(key string, val []byte) Record
Int(key string, val int) Record
Ints(key string, vals ...int) Record
Int8(key string, val int8) Record
Int8s(key string, vals ...int8) Record
Int16(key string, val int16) Record
Int16s(key string, vals ...int16) Record
Int32(key string, val int32) Record
Int32s(key string, vals ...int32) Record
Int64(key string, val int64) Record
Int64s(key string, vals ...int64) Record
Uint(key string, val uint) Record
Uints(key string, vals ...uint) Record
Uint8(key string, val uint8) Record
Uint8s(key string, vals ...uint8) Record
Uint16(key string, val uint16) Record
Uint16s(key string, vals ...uint16) Record
Uint32(key string, val uint32) Record
Uint32s(key string, vals ...uint32) Record
Uint64(key string, val uint64) Record
Uint64s(key string, vals ...uint64) Record
Float32(key string, val float32) Record
Float32s(key string, vals ...float32) Record
Float64(key string, val float64) Record
Float64s(key string, vals ...float64) Record
Time(key string, format string, val time.Time) Record
Times(key string, format string, vals ...time.Time) Record
Dur(key string, unit time.Duration, val time.Duration) Record
Durs(key string, unit time.Duration, vals ...time.Duration) Record
Any(key string, i any) Record
IPAddr(key string, ip net.IP) Record
IPPrefix(key string, pfx net.IPNet) Record
MACAddr(key string, ha net.HardwareAddr) Record
}
type RecordPackerForWriter ¶
type RecordPackerForWriter struct {
// contains filtered or unexported fields
}
func (*RecordPackerForWriter) Any ¶ added in v0.0.2
func (j *RecordPackerForWriter) Any(key string, i any)
func (*RecordPackerForWriter) Bytes ¶
func (j *RecordPackerForWriter) Bytes(key string, val []byte)
func (*RecordPackerForWriter) CallerSkip ¶
func (j *RecordPackerForWriter) CallerSkip(skip int)
func (*RecordPackerForWriter) Done ¶
func (j *RecordPackerForWriter) Done()
func (*RecordPackerForWriter) Dur ¶
func (j *RecordPackerForWriter) Dur(key string, unit, val time.Duration)
func (*RecordPackerForWriter) Err ¶
func (j *RecordPackerForWriter) Err(err error)
func (*RecordPackerForWriter) Float32 ¶
func (j *RecordPackerForWriter) Float32(key string, val float32)
func (*RecordPackerForWriter) Float32s ¶
func (j *RecordPackerForWriter) Float32s(key string, vals ...float32)
func (*RecordPackerForWriter) Float64 ¶
func (j *RecordPackerForWriter) Float64(key string, val float64)
func (*RecordPackerForWriter) Float64s ¶
func (j *RecordPackerForWriter) Float64s(key string, vals ...float64)
func (*RecordPackerForWriter) Hex ¶
func (j *RecordPackerForWriter) Hex(key string, val []byte)
func (*RecordPackerForWriter) IPAddr ¶
func (j *RecordPackerForWriter) IPAddr(key string, ip net.IP)
func (*RecordPackerForWriter) IPPrefix ¶
func (j *RecordPackerForWriter) IPPrefix(key string, pfx net.IPNet)
func (*RecordPackerForWriter) Int ¶
func (j *RecordPackerForWriter) Int(key string, val int)
func (*RecordPackerForWriter) Int8 ¶
func (j *RecordPackerForWriter) Int8(key string, val int8)
func (*RecordPackerForWriter) Int8s ¶
func (j *RecordPackerForWriter) Int8s(key string, vals ...int8)
func (*RecordPackerForWriter) Int16 ¶
func (j *RecordPackerForWriter) Int16(key string, val int16)
func (*RecordPackerForWriter) Int16s ¶
func (j *RecordPackerForWriter) Int16s(key string, vals ...int16)
func (*RecordPackerForWriter) Int32 ¶
func (j *RecordPackerForWriter) Int32(key string, val int32)
func (*RecordPackerForWriter) Int32s ¶
func (j *RecordPackerForWriter) Int32s(key string, vals ...int32)
func (*RecordPackerForWriter) Int64 ¶
func (j *RecordPackerForWriter) Int64(key string, val int64)
func (*RecordPackerForWriter) Int64s ¶
func (j *RecordPackerForWriter) Int64s(key string, vals ...int64)
func (*RecordPackerForWriter) Ints ¶
func (j *RecordPackerForWriter) Ints(key string, vals ...int)
func (*RecordPackerForWriter) MACAddr ¶
func (j *RecordPackerForWriter) MACAddr(key string, ha net.HardwareAddr)
func (*RecordPackerForWriter) Msg ¶
func (j *RecordPackerForWriter) Msg(msg string)
func (*RecordPackerForWriter) Reset ¶
func (j *RecordPackerForWriter) Reset()
func (*RecordPackerForWriter) Str ¶
func (j *RecordPackerForWriter) Str(key, val string)
func (*RecordPackerForWriter) Stringer ¶
func (j *RecordPackerForWriter) Stringer(key string, val fmt.Stringer)
func (*RecordPackerForWriter) Stringers ¶
func (j *RecordPackerForWriter) Stringers(key string, vals ...fmt.Stringer)
func (*RecordPackerForWriter) Strs ¶
func (j *RecordPackerForWriter) Strs(key string, vals ...string)
func (*RecordPackerForWriter) Time ¶
func (j *RecordPackerForWriter) Time(key, fmt string, val time.Time)
func (*RecordPackerForWriter) Times ¶
func (j *RecordPackerForWriter) Times(key, fmt string, vals ...time.Time)
func (*RecordPackerForWriter) Uint ¶
func (j *RecordPackerForWriter) Uint(key string, val uint)
func (*RecordPackerForWriter) Uint8 ¶
func (j *RecordPackerForWriter) Uint8(key string, val uint8)
func (*RecordPackerForWriter) Uint8s ¶
func (j *RecordPackerForWriter) Uint8s(key string, vals ...uint8)
func (*RecordPackerForWriter) Uint16 ¶
func (j *RecordPackerForWriter) Uint16(key string, val uint16)
func (*RecordPackerForWriter) Uint16s ¶
func (j *RecordPackerForWriter) Uint16s(key string, vals ...uint16)
func (*RecordPackerForWriter) Uint32 ¶
func (j *RecordPackerForWriter) Uint32(key string, val uint32)
func (*RecordPackerForWriter) Uint32s ¶
func (j *RecordPackerForWriter) Uint32s(key string, vals ...uint32)
func (*RecordPackerForWriter) Uint64 ¶
func (j *RecordPackerForWriter) Uint64(key string, val uint64)
func (*RecordPackerForWriter) Uint64s ¶
func (j *RecordPackerForWriter) Uint64s(key string, vals ...uint64)
func (*RecordPackerForWriter) Uints ¶
func (j *RecordPackerForWriter) Uints(key string, vals ...uint)
type WriterEncoderPair ¶
type WriterEncoderPair struct {
// contains filtered or unexported fields
}
WriterEncoderPair wraps a writer and an encoder.

