log

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: 13 Imported by: 3

README

Log Utilities

This package enhances the original logrus, and supports to hook alert.

Configurations

Currently, there are several configurations available:

  • Set default log level (default value is info).
  • Set log levels for specific modules.
  • Force or disable colors in Console or output file.
  • Hook with alert.
  • Output to file with rotation.

Initialize

We recommend to initialize the log module from a configuration file or environment variables.

// Initialize logging by specifying configurations
log.MustInit(conf)

// or Initialize logging from configurations loaded by viper
log.MustInitFromViper()

Log with Module

Application could specify different log levels for different moduels. Please use pre-defined factory method to create a logger with specific module name and log level.

log.WithModule("rpc").Info("Info message")
log.WithModule("rpc").Debug("Debug message")

Hook with Alert

Developers can configure the alert hook to set up default notification channels for sending alert messages when warning or error logs occur. You can also customize notifications by specifying the target channel(s) through the @channel field in a Logrus entry.

// Send alert to the 'tgrobot' channel only.
logrus.WithField("@channel", "tgrobot").Warn("Some warning occurred")

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindFlags added in v0.2.1

func BindFlags(cmd *cobra.Command)

BindFlags binds logging relevant flags for specified command.

func MustInit

func MustInit(conf LoggingConfig)

MustInit sets up the logging system according to the provided LoggingConfig and log level. It configures the log level, adds an alert hook, sets a text formatter, and adapts the logger for Geth compatibility. In case of any error during initialization, this function will panic.

func MustInitFromViper

func MustInitFromViper()

MustInitFromViper initializes the logging system using configurations from viper.

Precondition:

  • Viper must be initialized with appropriate configurations before calling this function.

Panics:

  • This function will panic if it encounters any errors during initialization.

func MustInitWithCtx added in v0.2.1

func MustInitWithCtx(ctx context.Context, wg *sync.WaitGroup, conf LoggingConfig)

MustInitWithCtx performs the similiar initializations as `MustInit` with support for graceful shutdown by accepting a context and a wait group.

func MustInitWithCtxFromViper added in v0.2.1

func MustInitWithCtxFromViper(ctx context.Context, wg *sync.WaitGroup)

MustInitWithCtxFromViper performs the similar initializations as `MustInitFromViper` with support for graceful shutdown by accepting a context and a wait group.

Parameters:

  • ctx: The context for graceful shutdown handling.
  • wg: The wait group to track goroutines for shutdown synchronization.

func WithModule added in v0.7.6

func WithModule(module string) *logrus.Entry

WithModule returns a logger with module name and specific log level.

Note, the module name should not contain delimiters "=" and ",". Otherwise, the behavior is unknown.

Types

type LoggingConfig

type LoggingConfig struct {
	Level        string       `default:"info"` // log level in format of "level,module1=level1,module2=level2"
	ForceColor   bool         // helpful on windows
	DisableColor bool         // helpful to output logs in file
	AlertHook    hook.Config  // alert hooking configurations
	Output       OutputConfig // output configurations
}

LoggingConfig logging configuration such as log level etc.,

type OutputConfig added in v0.3.0

type OutputConfig struct {
	Type     string         `default:"stderr"` // Available output types are: stdout, stderr, file
	FilePath string         // Optional: File path for "file" type
	Rotation RotationConfig // Optional: Rotation settings for "file" type
}

OutputConfig represents the output configuration.

type RotationConfig added in v0.3.0

type RotationConfig struct {
	MaxSize    int  `default:"100"` // Maximum size of the log file in MB before rotation
	MaxBackups int  // Maximum number of backup files to retain (0 to keep all)
	MaxAge     int  `default:"30"`   // Maximum age of log files before deletion (e.g., 30 days)
	Compress   bool `default:"true"` // Whether to compress rotated log files
}

RotationConfig defines the rotation settings for log files.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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