logger

package module
v4.2.6 Latest Latest
Warning

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

Go to latest
Published: Jul 4, 2024 License: Apache-2.0 Imports: 13 Imported by: 9

README

go-logger

Test status Coverage Status Go Report Card GoDev GitHub release

Fast, lightweight, customizable and structured logger for Go.

Install

go get github.com/savsgio/go-logger/v4

Supported Go versions:

  • 1.22.x
  • 1.21.x,
  • 1.20.x
  • 1.19.x
  • 1.18.x
  • 1.17.x

Levels:

Level Code (constant) Value (str)
Print logger.PRINT
Panic logger.PANIC panic / PANIC
Fatal logger.FATAL fatal / FATAL
Error logger.ERROR error / ERROR
Warning logger.WARNING warning / WARNING
Info logger.INFO info / INFO
Debug logger.DEBUG debug / DEBUG
Trace logger.TRACE trace / TRACE

NOTE: The default level of standard logger is INFO.

Encoders:

  • Text
  • JSON
  • Custom (your own encoder).

NOTE: The default encoder of standard logger is text.

Contributing

Feel free to contribute it or fork me... 😉

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidLevel is the invalid level error.
	ErrInvalidLevel = errors.New("invalid level")

	// ErrEmptyHookLevels is the empty hook levels error.
	ErrEmptyHookLevels = errors.New("empty hook levels")
)

Functions

func AddHook added in v4.2.0

func AddHook(h Hook) error

AddHook registers the given hook to the standard logger.

func Debug

func Debug(msg ...interface{})

func Debugf

func Debugf(msg string, args ...interface{})

func Error

func Error(msg ...interface{})

func Errorf

func Errorf(msg string, args ...interface{})

func Fatal

func Fatal(msg ...interface{})

func Fatalf

func Fatalf(msg string, args ...interface{})

func Info

func Info(msg ...interface{})

func Infof

func Infof(msg string, args ...interface{})

func IsLevelEnabled

func IsLevelEnabled(level Level) bool

IsLevelEnabled checks if the given level is enabled on the standard logger.

func Panic added in v4.2.0

func Panic(msg ...interface{})

func Panicf added in v4.2.0

func Panicf(msg string, args ...interface{})

func Print

func Print(msg ...interface{})

func Printf

func Printf(msg string, args ...interface{})

func ReleaseBuffer

func ReleaseBuffer(b *Buffer)

ReleaseBuffer puts the given buffer in the pool after it is reset.

func SetEncoder

func SetEncoder(enc Encoder)

SetEncoder sets the encoder to the standard logger.

func SetFields

func SetFields(fields ...Field)

SetFields sets the fields to the standard logger.

func SetFlags

func SetFlags(flag Flag)

SetFlags sets the output flags to the standard logger.

func SetLevel

func SetLevel(level Level)

SetLevel sets the level to the standard logger.

func SetOutput

func SetOutput(output io.Writer)

SetOutput sets the output to the standard logger.

func Trace

func Trace(msg ...interface{})

func Tracef

func Tracef(msg string, args ...interface{})

func Warning

func Warning(msg ...interface{})

func Warningf

func Warningf(msg string, args ...interface{})

Types

type Buffer

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

Buffer provides the byte buffer used by encoders to encode the output.

func AcquireBuffer

func AcquireBuffer() *Buffer

AcquireBuffer returns a buffer instance from pool.

func NewBuffer

func NewBuffer() *Buffer

NewBuffer returns a new buffer.

func (*Buffer) Bytes

func (b *Buffer) Bytes() []byte

Bytes returns all the accumulated bytes.

func (*Buffer) Escape

func (b *Buffer) Escape(startAt int)

Escape escapes accumulated bytes since the given index.

func (*Buffer) Len

func (b *Buffer) Len() int

Len returns the size of the buffer.

func (*Buffer) Reset

func (b *Buffer) Reset()

Reset clears the buffer.

func (*Buffer) String

func (b *Buffer) String() string

String returns string representation.

func (*Buffer) Write

func (b *Buffer) Write(s []byte) (int, error)

Write writes the given bytes slice to the buffer.

func (*Buffer) WriteByte

func (b *Buffer) WriteByte(s byte) error

WriteByte writes the given byte to the buffer.

func (*Buffer) WriteDatetime

func (b *Buffer) WriteDatetime(now time.Time, layout string)

WriteDatetime writes the given time to the buffer formatted with the given layout.

func (*Buffer) WriteFileCaller

func (b *Buffer) WriteFileCaller(f runtime.Frame, short bool)

WriteFileCaller writes the file caller to the buffer.

func (*Buffer) WriteInterface

func (b *Buffer) WriteInterface(value interface{})

WriteInterface writes an interface value to the buffer.

func (*Buffer) WriteNewLine

func (b *Buffer) WriteNewLine()

WriteNewLine writes a new line to the buffer if it's needed.

func (*Buffer) WriteString

func (b *Buffer) WriteString(s string) (int, error)

WriteString writes the given string to the buffer.

func (*Buffer) WriteTimestamp

func (b *Buffer) WriteTimestamp(now time.Time, format TimestampFormat)

WriteTimestamp writes the timestamp to the buffer from the given time.

func (*Buffer) WriteTo

func (b *Buffer) WriteTo(w io.Writer) (int64, error)

WriteTo implements io.WriterTo.

type Config

type Config struct {
	Fields    []Field
	Datetime  bool
	Timestamp bool
	UTC       bool
	Shortfile bool
	Longfile  bool
	Function  bool
	// contains filtered or unexported fields
}

Config is the logger configuration.

func (Config) Copy

func (e Config) Copy() Config

Copy returns a copy of the config.

type Encoder

type Encoder interface {
	Copy() Encoder
	FieldsEncoded() string
	SetFieldsEncoded(fieldsEncoded string)
	Configure(cfg Config)
	Encode(b *Buffer, e Entry) error
}

Encoder represents the encoders contract.

type EncoderBase

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

EncoderBase is the base of encoders.

func (*EncoderBase) Copy

func (enc *EncoderBase) Copy() *EncoderBase

Copy returns a copy of the encoder base.

func (*EncoderBase) FieldsEncoded

func (enc *EncoderBase) FieldsEncoded() string

FieldsEncoded returns the encoded fields.

func (*EncoderBase) SetFieldsEncoded

func (enc *EncoderBase) SetFieldsEncoded(fieldsEncoded string)

SetFieldsEncoded sets the fields enconded.

type EncoderJSON

type EncoderJSON struct {
	EncoderBase
	// contains filtered or unexported fields
}

EncoderJSON is the json encoder.

func NewEncoderJSON

func NewEncoderJSON(cfg EncoderJSONConfig) *EncoderJSON

NewEncoderJSON creates a new json encoder.

func (*EncoderJSON) Configure

func (enc *EncoderJSON) Configure(cfg Config)

Configure configures then encoder.

- Encondes and sets the fields.

func (*EncoderJSON) Copy

func (enc *EncoderJSON) Copy() Encoder

Copy returns a copy of the json encoder.

func (*EncoderJSON) Encode

func (enc *EncoderJSON) Encode(buf *Buffer, e Entry) error

Encode encodes the given entry to the buffer.

type EncoderJSONConfig

type EncoderJSONConfig struct {
	FieldMap EnconderJSONFieldMap

	// Default: time.RFC3339
	DatetimeLayout string

	// Default: TimestampFormatSeconds
	TimestampFormat TimestampFormat
}

EncoderJSONConfig is the configuration of json encoder.

type EncoderText

type EncoderText struct {
	EncoderBase
	// contains filtered or unexported fields
}

EncoderText is the text enconder.

func NewEncoderText

func NewEncoderText(cfg EncoderTextConfig) *EncoderText

NewEncoderText creates a new text encoder.

func (*EncoderText) Configure

func (enc *EncoderText) Configure(cfg Config)

Configure configures then encoder.

- Encondes and sets the fields.

func (*EncoderText) Copy

func (enc *EncoderText) Copy() Encoder

Copy returns a copy of the text encoder.

func (*EncoderText) Encode

func (enc *EncoderText) Encode(buf *Buffer, e Entry) error

Encode encodes the given entry to the buffer.

type EncoderTextConfig

type EncoderTextConfig struct {
	// Default: -
	Separator string

	// Default: time.RFC3339
	DatetimeLayout string

	// Default: TimestampFormatSeconds
	TimestampFormat TimestampFormat
}

EncoderTextConfig is the configuration of text encoder.

type EnconderJSONFieldMap

type EnconderJSONFieldMap struct {
	// Default: datetime
	DatetimeKey string

	// Default: timestamp
	TimestampKey string

	// Default: level
	LevelKey string

	// Default: file
	FileKey string

	// Default: func
	FunctionKey string

	// Default: message
	MessageKey string
}

EnconderJSONFieldMap defines name of keys.

type Entry

type Entry struct {
	Config     Config
	Time       time.Time
	Level      Level
	Caller     runtime.Frame
	Message    string
	RawMessage string
	Args       []interface{}
}

Entry collects all the information for the output.

type Field

type Field struct {
	Key   string
	Value interface{}
}

Field type.

type Flag

type Flag int

Flag type.

const (
	Ldatetime Flag = 1 << iota
	Ltimestamp
	LUTC
	Llongfile
	Lshortfile
	Lfunction

	LstdFlags = Ldatetime
)

Logger flags.

type Hook

type Hook interface {
	// Levels returns the levels at which the hook fires.
	Levels() []Level

	// Fire is hook function.
	//
	// NOTE: The returned error will be written to `os.Stderr`.
	Fire(e Entry) error
}

Hook represents a extended functionality that will be fired when logging.

NOTE: This is not run concurrently, so be quite with locks.

type Level

type Level int

Level type.

const (
	PRINT Level
	PANIC
	FATAL
	ERROR
	WARNING
	INFO
	DEBUG
	TRACE
)

Logger levels.

func ParseLevel

func ParseLevel(levelStr string) (level Level, err error)

ParseLevel returns the Level constant from the given level string.

func (Level) String

func (l Level) String() string

Strings returns the string representation of the level.

type Logger

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

Logger type.

func New

func New(level Level, output io.Writer, fields ...Field) *Logger

New creates a new Logger.

func WithFields

func WithFields(fields ...Field) *Logger

WithFields returns a copy of the standard logger with the given fields.

func (*Logger) AddHook

func (l *Logger) AddHook(h Hook) error

AddHook registers the given hook to the logger.

func (*Logger) Debug

func (l *Logger) Debug(msg ...interface{})

func (*Logger) Debugf

func (l *Logger) Debugf(msg string, args ...interface{})

func (*Logger) Error

func (l *Logger) Error(msg ...interface{})

func (*Logger) Errorf

func (l *Logger) Errorf(msg string, args ...interface{})

func (*Logger) Fatal

func (l *Logger) Fatal(msg ...interface{})

func (*Logger) Fatalf

func (l *Logger) Fatalf(msg string, args ...interface{})

func (*Logger) Info

func (l *Logger) Info(msg ...interface{})

func (*Logger) Infof

func (l *Logger) Infof(msg string, args ...interface{})

func (*Logger) IsLevelEnabled

func (l *Logger) IsLevelEnabled(level Level) bool

IsLevelEnabled checks if the given level is enabled on the logger.

func (*Logger) Panic added in v4.2.0

func (l *Logger) Panic(msg ...interface{})

func (*Logger) Panicf added in v4.2.0

func (l *Logger) Panicf(msg string, args ...interface{})

func (*Logger) Print

func (l *Logger) Print(msg ...interface{})

func (*Logger) Printf

func (l *Logger) Printf(msg string, args ...interface{})

func (*Logger) SetEncoder

func (l *Logger) SetEncoder(enc Encoder)

SetEncoder sets the logger encoder.

func (*Logger) SetFields

func (l *Logger) SetFields(fields ...Field)

SetFields sets the logger fields.

func (*Logger) SetFlags

func (l *Logger) SetFlags(flag Flag)

SetFlags sets the logger output flags.

func (*Logger) SetLevel

func (l *Logger) SetLevel(level Level)

SetLevel sets the logger level.

func (*Logger) SetOutput

func (l *Logger) SetOutput(output io.Writer)

SetOutput sets the logger output.

func (*Logger) Trace

func (l *Logger) Trace(msg ...interface{})

func (*Logger) Tracef

func (l *Logger) Tracef(msg string, args ...interface{})

func (*Logger) Warning

func (l *Logger) Warning(msg ...interface{})

func (*Logger) Warningf

func (l *Logger) Warningf(msg string, args ...interface{})

func (*Logger) WithFields

func (l *Logger) WithFields(fields ...Field) *Logger

WithFields returns a logger copy with the given fields.

type TimestampFormat

type TimestampFormat int

TimestampFormat type.

const (
	TimestampFormatSeconds TimestampFormat = iota + 1
	TimestampFormatNanoseconds
)

Logger timestamp formats.

Jump to

Keyboard shortcuts

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