Documentation
ΒΆ
Index ΒΆ
- Constants
- Variables
- func WithLogrus() compogo.Option
- type Config
- type Decorator
- func (logger *Decorator) AddHook(hook logrus.Hook)
- func (logger *Decorator) Debug(i ...interface{})
- func (logger *Decorator) Debugf(s string, i ...interface{})
- func (logger *Decorator) Error(i ...interface{})
- func (logger *Decorator) Errorf(s string, i ...interface{})
- func (logger *Decorator) GetLogger(name string) logger.Logger
- func (logger *Decorator) GetStdErr() *logrus.Logger
- func (logger *Decorator) GetStdOut() *logrus.Logger
- func (logger *Decorator) Info(i ...interface{})
- func (logger *Decorator) Infof(s string, i ...interface{})
- func (logger *Decorator) Panic(i ...interface{})
- func (logger *Decorator) Panicf(s string, i ...interface{})
- func (logger *Decorator) Print(i ...interface{})
- func (logger *Decorator) Printf(s string, i ...interface{})
- func (logger *Decorator) SetLevel(level logger.Level) error
- func (logger *Decorator) Warn(i ...interface{})
- func (logger *Decorator) Warnf(s string, i ...interface{})
Constants ΒΆ
const ( // LevelNameFieldName defines the command-line flag name for setting the log level. // Example: --logger.level=debug LevelNameFieldName = "logger.level" )
Variables ΒΆ
var ( // LevelNameDefault defines the default log level as a string. // It corresponds to logger.Error (lowercase "error"). LevelNameDefault = strings.ToLower(logger.Error.String()) // LoggerLevelToLogrusLevel maps Compogo's logger.Level to logrus.Level. // This ensures type-safe conversion between the two level systems. LoggerLevelToLogrusLevel = linker.NewLinker[logger.Level, logrus.Level]( linker.NewLink(logger.Panic, logrus.PanicLevel), linker.NewLink(logger.Error, logrus.ErrorLevel), linker.NewLink(logger.Warn, logrus.WarnLevel), linker.NewLink(logger.Info, logrus.InfoLevel), linker.NewLink(logger.Debug, logrus.DebugLevel), ) )
Functions ΒΆ
func WithLogrus ΒΆ
WithLogrus returns a Compogo option that integrates logrus as the application's logging system. It automatically:
- Registers the logger in the DI container
- Adds command-line flags for log level configuration
- Sets up the log level from configuration
- Injects the application name into the logger
Usage:
app := compogo.NewApp("myapp",
logrus.WithLogrus(),
// other options...
)
Types ΒΆ
type Config ΒΆ
type Config struct {
// LevelName is the string representation of the log level (e.g., "info", "debug").
// It is populated from the command-line flag.
LevelName string
// Level is the parsed logger.Level enum value, converted from LevelName.
Level logger.Level
}
Config holds the logger configuration that can be set via command-line flags or configuration files. It includes the log level as both string and parsed enum.
func Configuration ΒΆ
func Configuration(config *Config, configurator configurator.Configurator) (*Config, error)
Configuration applies configuration values to the Config struct. It reads from the configurator, sets defaults, and validates the log level. Returns an error if the level name cannot be parsed into a valid logger.Level.
The function is designed to be used with container.Invoke in the PreRun phase.
type Decorator ΒΆ
type Decorator struct {
// contains filtered or unexported fields
}
Decorator wraps a logrus.Logger to implement the logger.Logger interface. It adds application name prefixing to all log messages and supports creating child loggers for sub-components.
The decorator maintains separate stdout and stderr loggers, allowing different outputs for different log levels if needed.
func NewDecorator ΒΆ
func NewDecorator() *Decorator
NewDecorator creates a new Decorator instance with default logrus loggers. Both stdout and stderr loggers are initialized with default settings. The actual log level will be set later via SetLevel.
func (*Decorator) AddHook ΒΆ
AddHook adds a logrus hook to both stdout and stderr loggers. This enables integration with external logging systems like Sentry, file logging, or custom formatters.
func (*Decorator) Debug ΒΆ
func (logger *Decorator) Debug(i ...interface{})
Debug logs a message at Debug level. The application name is prepended to the arguments. If this is a child logger, the call is delegated to the parent.
func (*Decorator) Debugf ΒΆ
Debugf logs a formatted message at Debug level. The message is prefixed with the application name in brackets. If this is a child logger, the call is delegated to the parent.
func (*Decorator) Error ΒΆ
func (logger *Decorator) Error(i ...interface{})
Error logs a message at Error level. The application name is prepended to the arguments. If this is a child logger, the call is delegated to the parent.
func (*Decorator) Errorf ΒΆ
Errorf logs a formatted message at Error level. The message is prefixed with the application name in brackets. If this is a child logger, the call is delegated to the parent.
func (*Decorator) GetLogger ΒΆ
GetLogger creates a child logger with the given name. The child logger will prefix all messages with its own name in addition to the parent's name, creating a chain like: [app] [child] message
This is useful for sub-components that need their own log identity while still being part of the main application.
func (*Decorator) GetStdErr ΒΆ
GetStdErr returns the underlying logrus.Logger used for error output. This can be used for advanced configuration or adding hooks.
func (*Decorator) GetStdOut ΒΆ
GetStdOut returns the underlying logrus.Logger used for standard output. This can be used for advanced configuration or adding hooks.
func (*Decorator) Info ΒΆ
func (logger *Decorator) Info(i ...interface{})
Info logs a message at Info level. The application name is prepended to the arguments. If this is a child logger, the call is delegated to the parent.
func (*Decorator) Infof ΒΆ
Infof logs a formatted message at Info level. The message is prefixed with the application name in brackets. If this is a child logger, the call is delegated to the parent.
func (*Decorator) Panic ΒΆ
func (logger *Decorator) Panic(i ...interface{})
Panic logs a message at Panic level and then panics. The application name is prepended to the arguments. If this is a child logger, the call is delegated to the parent.
func (*Decorator) Panicf ΒΆ
Panicf logs a formatted message at Panic level and then panics. The message is prefixed with the application name in brackets. If this is a child logger, the call is delegated to the parent.
func (*Decorator) Print ΒΆ
func (logger *Decorator) Print(i ...interface{})
Print logs a message at Info level (for compatibility). The application name is prepended to the arguments. If this is a child logger, the call is delegated to the parent.
func (*Decorator) Printf ΒΆ
Printf logs a formatted message at Info level (for compatibility). The message is prefixed with the application name in brackets. If this is a child logger, the call is delegated to the parent.
func (*Decorator) SetLevel ΒΆ
SetLevel changes the logging level for both stdout and stderr loggers. The level is converted from Compogo's logger.Level to logrus.Level using the LoggerLevelToLogrusLevel mapper. Returns an error if the level conversion fails.