hook

package
v0.7.11 Latest Latest
Warning

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

Go to latest
Published: Sep 17, 2026 License: MIT Imports: 11 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var ErrAsyncQueueFull = errors.New("async hook queue is full")

ErrAsyncQueueFull is the error returned when the async queue is full.

Functions

func AddAlertHook

func AddAlertHook(ctx context.Context, wg *sync.WaitGroup, conf Config) error

AddAlertHook attaches a custom logrus Hook for generating alert notifications based on configured levels and channels. It supports both synchronous and asynchronous operation modes, with optional graceful shutdown integration.

Types

type AlertHook

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

AlertHook logrus hooks to send specified level logs as text message for alerting.

func NewAlertHook

func NewAlertHook(lvls []logrus.Level, chs []alert.Channel, timeout time.Duration) *AlertHook

NewAlertHook constructor to new AlertHook instance.

func (*AlertHook) Fire

func (hook *AlertHook) Fire(logEntry *logrus.Entry) (err error)

func (*AlertHook) Levels

func (hook *AlertHook) Levels() []logrus.Level

implements `logrus.Hook` interface methods.

type AsyncHook

type AsyncHook struct {
	logrus.Hook // Embedded logrus hook.
	AsyncOption // Embedded options.
	// contains filtered or unexported fields
}

AsyncHook is a logrus hook that processes log entries asynchronously.

func NewAsyncHook

func NewAsyncHook(hook logrus.Hook, opts AsyncOption) *AsyncHook

NewAsyncHook initializes and starts a standard AsyncHook without graceful shutdown handling. Use this when you don't require integration with a graceful shutdown mechanism.

func NewAsyncHookWithCtx

func NewAsyncHookWithCtx(
	ctx context.Context, wg *sync.WaitGroup, hook logrus.Hook, opts AsyncOption) *AsyncHook

NewAsyncHookWithCtx initializes and starts a new AsyncHook instance that integrates with graceful shutdown handling. It's designed to work harmoniously with the application's shutdown process to ensure no logs are lost during shutdown.

func (*AsyncHook) Fire

func (h *AsyncHook) Fire(entry *logrus.Entry) error

Fire implements the logrus.Hook interface, which enqueues log entries for async processing or handles them synchronously if necessary.

type AsyncOption

type AsyncOption struct {
	NumWorkers  int           // Number of worker goroutines. If set to 0 (default), async mode is disabled.
	QueueSize   int           `default:"60"` // Maximum number of queued jobs.
	StopTimeout time.Duration `default:"5s"` // Timeout duration before forced exit of async processing.
}

AsyncOption defines configuration options for the AsyncHook.

type Config

type Config struct {
	// Level is the minimum logrus level at which alerts will be triggered.
	Level string `default:"warn"`

	// Channels lists the default alert notification channels to use.
	Channels []string

	// Maximum request timeout allowed to send alert.
	SendTimeout time.Duration `default:"3s"`

	// Async configures the behavior of the asynchronous worker for handling log alerts.
	Async AsyncOption
}

Jump to

Keyboard shortcuts

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