const ( colorRed = 31 colorBlue = 36 colorMagenta = 35 colorGreen = 32 )
const ( disableColors = true enableColors = false )
const cacheLimit = 512
const highLoadLimit = 200 * time.Millisecond
FallbackLogger is an alias for the standard logger
var FallbackLogger = logrus.StandardLogger()
var defaultFormatter = &logrus.TextFormatter{DisableColors: true}
func WithLogger(ctx context.Context, logger LoggerIface) context.Context
WithLogger returns a new context with the provided logger. Use in combination with logger.WithField(s) for great effect
func getColorByLevel(level logrus.Level) int
func getLogFileFormatter(opts CmdOpts) logrus.Formatter
func getLogFileWriter(opts CmdOpts) any
func trimFilename(s string) string
BrokerHook is the implementation of the logrus hook for publicating logs to subscribers
type BrokerHook struct { highLoadTimeout time.Duration // wait this amount of time before skip log entry subscribers []MessageChanType // input chan *logrus.Entry ctx context.Context lastError chan error level string mu *sync.Mutex formatter logrus.Formatter }
func NewBrokerHook(ctx context.Context, level string) *BrokerHook
NewBrokerHook creates a LogHook to be added to an instance of logger
func (hook *BrokerHook) AddSubscriber(msgCh MessageChanType)
AddSubscriber adds receiving channel to the subscription
func (hook *BrokerHook) Fire(entry *logrus.Entry) error
Fire adds logrus log message to the internal queue for processing
func (hook *BrokerHook) Levels() []logrus.Level
Levels returns the available logging levels
func (hook *BrokerHook) RemoveSubscriber(msgCh MessageChanType)
RemoveSubscriber deletes receiving channel from the subscription
func (hook *BrokerHook) SetBrokerFormatter(formatter logrus.Formatter)
SetBrokerFormatter sets the format that will be used by hook.
func (hook *BrokerHook) poll(input <-chan *logrus.Entry)
poll checks for incoming messages and caches them internally until either a maximum amount is reached, or a timeout occurs.
func (hook *BrokerHook) send(entry *logrus.Entry)
send sends cached messages to the postgres server
CmdOpts specifies the logging command-line options
type CmdOpts struct { LogLevel string `short:"v" long:"log-level" mapstructure:"log-level" description:"Verbosity level for stdout and log file" choice:"debug" choice:"info" choice:"error" default:"info"` LogFile string `long:"log-file" mapstructure:"log-file" description:"File name to store logs"` LogFileFormat string `long:"log-file-format" mapstructure:"log-file-format" description:"Format of file logs" choice:"json" choice:"text" default:"json"` LogFileRotate bool `long:"log-file-rotate" mapstructure:"log-file-rotate" description:"Rotate log files"` LogFileSize int `long:"log-file-size" mapstructure:"log-file-size" description:"Maximum size in MB of the log file before it gets rotated" default:"100"` LogFileAge int `long:"log-file-age" mapstructure:"log-file-age" description:"Number of days to retain old log files, 0 means forever" default:"0"` LogFileNumber int `long:"log-file-number" mapstructure:"log-file-number" description:"Maximum number of old log files to retain, 0 to retain all" default:"0"` }
Formatter - logrus formatter, implements logrus.Formatter Forked from "github.com/antonfisher/nested-logrus-formatter"
type Formatter struct { // FieldsOrder - default: fields sorted alphabetically FieldsOrder []string // TimestampFormat - default: time.StampMilli = "Jan _2 15:04:05.000" TimestampFormat string // HideKeys - show [fieldValue] instead of [fieldKey:fieldValue] HideKeys bool // NoColors - disable colors NoColors bool // NoFieldsColors - apply colors only to the level, default is level + fields NoFieldsColors bool // NoFieldsSpace - no space between fields NoFieldsSpace bool // ShowFullLevel - show a full level [WARNING] instead of [WARN] ShowFullLevel bool // NoUppercaseLevel - no upper case for level value NoUppercaseLevel bool // TrimMessages - trim whitespaces on messages TrimMessages bool // CallerFirst - print caller info first CallerFirst bool // CustomCallerFormatter - set custom formatter for caller info CustomCallerFormatter func(*runtime.Frame) string }
func newFormatter(noColors bool) *Formatter
func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error)
Format an log entry
▹ Example (Default)
func (f *Formatter) writeCaller(b *bytes.Buffer, entry *logrus.Entry)
func (f *Formatter) writeField(b *bytes.Buffer, entry *logrus.Entry, field string)
func (f *Formatter) writeFields(b *bytes.Buffer, entry *logrus.Entry)
func (f *Formatter) writeOrderedFields(b *bytes.Buffer, entry *logrus.Entry)
LoggerHookerIface adds AddHook method to LoggerIface for database logging hook
type LoggerHookerIface interface { LoggerIface AddHook(hook logrus.Hook) AddSubscriber(msgCh MessageChanType) RemoveSubscriber(msgCh MessageChanType) }
func Init(opts CmdOpts) LoggerHookerIface
Init creates logging facilities for the application
LoggerIface is the interface used by all components
type LoggerIface logrus.FieldLogger
func GetLogger(ctx context.Context) LoggerIface
GetLogger retrieves the current logger from the context. If no logger is available, the default logger is returned
MessageChanType represents the format of the message channel
type MessageChanType chan MessageType
MessageType represents the format of the message
type MessageType string
PgxLogger is the struct used to log using pgx postgres driver
type PgxLogger struct { l LoggerIface }
func NewPgxLogger(l LoggerIface) *PgxLogger
NewPgxLogger returns a new instance of PgxLogger
func (pgxlogger *PgxLogger) Log(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]any)
Log transforms logging calls from pgx to logrus
type logger struct { *logrus.Logger *BrokerHook }
type loggerKey struct{}