logrus

package module
v0.0.15 Latest Latest
Warning

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

Go to latest
Published: May 11, 2026 License: MIT Imports: 10 Imported by: 0

README ΒΆ

Compogo Logrus πŸͺ΅

Compogo Logrus β€” это готовая интСграция logrus с Ρ„Ρ€Π΅ΠΉΠΌΠ²ΠΎΡ€ΠΊΠΎΠΌ Compogo. ДобавляСтся ΠΎΠ΄Π½ΠΎΠΉ строкой ΠΈ автоматичСски ΠΏΠΎΠ»ΡƒΡ‡Π°Π΅Ρ‚ имя прилоТСния ΠΈΠ· ΠΊΠΎΠ½Ρ„ΠΈΠ³Π°, настраиваСтся Ρ‡Π΅Ρ€Π΅Π· Ρ„Π»Π°Π³ΠΈ ΠΈ ΠΏΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΈΠ²Π°Π΅Ρ‚ Π΄ΠΎΡ‡Π΅Ρ€Π½ΠΈΠ΅ Π»ΠΎΠ³Π³Π΅Ρ€Ρ‹.

πŸš€ Установка

go get github.com/Compogo/logrus
πŸ“¦ ИспользованиС
package main

import (
    "github.com/Compogo/compogo"
    "github.com/Compogo/logrus"
    "github.com/Compogo/myapp/service"
)

func main() {
    app := compogo.NewApp("myapp",
        compogo.WithOsSignalCloser(),
        logrus.WithLogrus(),                    // ← ΠΎΠ΄Π½Π° строка
        compogo.WithComponents(
            service.Component,
        ),
    )

    if err := app.Serve(); err != nil {
        panic(err)
    }
}

✨ ВозмоТности

🎯 АвтоматичСский прСфикс с ΠΈΠΌΠ΅Π½Π΅ΠΌ прилоТСния

ВсС сообщСния автоматичСски ΠΏΠΎΠ»ΡƒΡ‡Π°ΡŽΡ‚ прСфикс [appname]. Π”ΠΎΡ‡Π΅Ρ€Π½ΠΈΠ΅ Π»ΠΎΠ³Π³Π΅Ρ€Ρ‹ Π΄ΠΎΠ±Π°Π²Π»ΡΡŽΡ‚ свой:

type Service struct {
    log logger.Logger
}

func NewService(log logger.Logger) *Service {
    child := log.GetLogger("service")
    child.Info("starting") // [myapp] [service] starting
}
🎚️ Π£Ρ€ΠΎΠ²Π½ΠΈ логирования

Π£Ρ€ΠΎΠ²Π΅Π½ΡŒ задаётся Ρ‡Π΅Ρ€Π΅Π· Ρ„Π»Π°Π³ --logger.level:

./myapp --logger.level=debug

ДоступныС ΡƒΡ€ΠΎΠ²Π½ΠΈ: panic, error, warn, info, debug.

πŸ”§ Π Π°Π·Π΄Π΅Π»Π΅Π½ΠΈΠ΅ stdout/stderr
  • Info, Debug, Print β†’ stdout
  • Warn, Error, Panic β†’ stderr
πŸͺ ΠŸΠΎΠ΄Π΄Π΅Ρ€ΠΆΠΊΠ° Ρ…ΡƒΠΊΠΎΠ²
decorator.AddHook(sentry.NewHook(...))
βš™οΈ Как это Ρ€Π°Π±ΠΎΡ‚Π°Π΅Ρ‚
  1. Init β€” рСгистрируСт ΠΊΠΎΠ½Ρ„ΠΈΠ³ ΠΈ Π΄Π΅ΠΊΠΎΡ€Π°Ρ‚ΠΎΡ€ Π² DI
  2. BindFlags β€” добавляСт Ρ„Π»Π°Π³ --logger.level
  3. PreRun β€” Π·Π°Π³Ρ€ΡƒΠΆΠ°Π΅Ρ‚ ΠΊΠΎΠ½Ρ„ΠΈΠ³, подставляСт имя прилоТСния, устанавливаСт ΡƒΡ€ΠΎΠ²Π΅Π½ΡŒ

Documentation ΒΆ

Index ΒΆ

Constants ΒΆ

This section is empty.

Variables ΒΆ

This section is empty.

Functions ΒΆ

func WithLogrus ΒΆ

func WithLogrus() compogo.Option

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 Logger ΒΆ added in v0.0.14

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

Logger 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 NewLogger ΒΆ added in v0.0.14

func NewLogger() *Logger

NewLogger creates a new Logger 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 (*Logger) AddHook ΒΆ added in v0.0.14

func (logger *Logger) AddHook(hook logrus.Hook)

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 (*Logger) Debug ΒΆ added in v0.0.14

func (logger *Logger) 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 (*Logger) Debugf ΒΆ added in v0.0.14

func (logger *Logger) Debugf(s string, i ...interface{})

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 (*Logger) Error ΒΆ added in v0.0.14

func (logger *Logger) 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 (*Logger) Errorf ΒΆ added in v0.0.14

func (logger *Logger) Errorf(s string, i ...interface{})

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 (*Logger) GetLogger ΒΆ added in v0.0.14

func (logger *Logger) GetLogger(name string) logger.Logger

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 (*Logger) GetStdErr ΒΆ added in v0.0.14

func (logger *Logger) GetStdErr() *logrus.Logger

GetStdErr returns the underlying logrus.Logger used for error output. This can be used for advanced configuration or adding hooks.

func (*Logger) GetStdOut ΒΆ added in v0.0.14

func (logger *Logger) GetStdOut() *logrus.Logger

GetStdOut returns the underlying logrus.Logger used for standard output. This can be used for advanced configuration or adding hooks.

func (*Logger) Info ΒΆ added in v0.0.14

func (logger *Logger) 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 (*Logger) Infof ΒΆ added in v0.0.14

func (logger *Logger) Infof(s string, i ...interface{})

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 (*Logger) Panic ΒΆ added in v0.0.14

func (logger *Logger) 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 (*Logger) Panicf ΒΆ added in v0.0.14

func (logger *Logger) Panicf(s string, i ...interface{})

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 (*Logger) Print ΒΆ added in v0.0.14

func (logger *Logger) 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 (*Logger) Printf ΒΆ added in v0.0.14

func (logger *Logger) Printf(s string, i ...interface{})

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 (*Logger) SetLevel ΒΆ added in v0.0.14

func (logger *Logger) SetLevel(level logger.Level) error

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.

func (*Logger) Warn ΒΆ added in v0.0.14

func (logger *Logger) Warn(i ...interface{})

Warn logs a message at Warn level. The application name is prepended to the arguments. If this is a child logger, the call is delegated to the parent.

func (*Logger) Warnf ΒΆ added in v0.0.14

func (logger *Logger) Warnf(s string, i ...interface{})

Warnf logs a formatted message at Warn level. The message is prefixed with the application name in brackets. If this is a child logger, the call is delegated to the parent.

Directories ΒΆ

Path Synopsis
infrastructure

Jump to

Keyboard shortcuts

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