logger

package
v0.2.2 Latest Latest
Warning

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

Go to latest
Published: Aug 11, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Index

Constants

View Source
const DefaultFileHookLayout = "20060102_log.json"

Variables

View Source
var ErrInvalidConfig = errors.New("invalid config")

Functions

This section is empty.

Types

type Config

type Config struct {
	Level string `json:"level" yaml:"level"`
	File  struct {
		Path   string `json:"path"   yaml:"path"`
		Layout string `json:"layout" yaml:"layout"`
	} `json:"file" yaml:"file"`
	Discord discordbotrus.Config `json:"discord" yaml:"discord"`
}

Config represents the configuration structure for the logger. It includes settings for log level and file output configuration.

type Hook

type Hook interface {
	logrus.Hook
	Close() error
}

Hook includes logrus.Hook interface and describes Close method.

type Logger

type Logger struct {
	*logrus.Logger
	// contains filtered or unexported fields
}

Logger wraps logrus.Logger with additional configuration and methods.

func New

func New() *Logger

New creates and returns a new Logger instance with default JSON formatter. The returned logger has no output set by default (uses stderr).

func (*Logger) AddOutput

func (log *Logger) AddOutput(w io.Writer)

AddOutput adds additional output writer to the logger. This allows writing logs to multiple destinations simultaneously. The new writer will be used in addition to any existing outputs.

func (*Logger) Close

func (log *Logger) Close() error

Close implements the io.Closer interface for the Logger. Currently, it doesn't perform any cleanup but is provided for future compatibility.

func (*Logger) SetConfig

func (log *Logger) SetConfig(cfg *Config, options ...Option) error

SetConfig applies a new configuration to the Logger and configures all required outputs. It handles the following configuration aspects:

  • Core logger settings (log level)
  • File output configuration (if specified)
  • Discord hook setup (if configured)

Parameters:

  • cfg: Pointer to Config struct containing all logger settings. Must not be nil.
  • options: Optional variadic list of Option functions to modify logger behavior.

Returns:

  • error: Returns ErrInvalidConfig if cfg is nil or contains invalid settings. Returns file creation errors if file logging is configured. Returns Discord hook initialization errors if Discord logging is configured.

Usage:

err := logger.SetConfig(&Config{
    Level: "info",
    File: FileConfig{Path: "/var/log", Layout: "2006-01-02.log"},
})
if err != nil {
    // handle error
}

Notes:

  • This function is not concurrent-safe and should not be called while the logger is in use.
  • Replaces all previous configuration when called.
  • File outputs are created immediately if specified in config.
  • Discord hooks are initialized immediately if configured.

func (*Logger) Writer added in v0.2.1

func (log *Logger) Writer() io.Writer

Writer returns the current writer used by the logger. This can be used to redirect the logger's output or integrate with other systems.

type Option

type Option func(log *Logger)

func WithDiscordSession

func WithDiscordSession(session *discordgo.Session) Option

Jump to

Keyboard shortcuts

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