...

Package log

import "github.com/cybertec-postgresql/pgwatch/v3/internal/log"
Overview
Index
Examples

Overview ▾

Index ▾

Constants
Variables
func WithLogger(ctx context.Context, logger LoggerIface) context.Context
func getColorByLevel(level logrus.Level) int
func getLogFileFormatter(opts CmdOpts) logrus.Formatter
func getLogFileWriter(opts CmdOpts) any
func trimFilename(s string) string
type BrokerHook
    func NewBrokerHook(ctx context.Context, level string) *BrokerHook
    func (hook *BrokerHook) AddSubscriber(msgCh MessageChanType)
    func (hook *BrokerHook) Fire(entry *logrus.Entry) error
    func (hook *BrokerHook) Levels() []logrus.Level
    func (hook *BrokerHook) RemoveSubscriber(msgCh MessageChanType)
    func (hook *BrokerHook) SetBrokerFormatter(formatter logrus.Formatter)
    func (hook *BrokerHook) poll(input <-chan *logrus.Entry)
    func (hook *BrokerHook) send(entry *logrus.Entry)
type CmdOpts
type Formatter
    func newFormatter(noColors bool) *Formatter
    func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error)
    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)
type LoggerHookerIface
    func Init(opts CmdOpts) LoggerHookerIface
type LoggerIface
    func GetLogger(ctx context.Context) LoggerIface
type MessageChanType
type MessageType
type PgxLogger
    func NewPgxLogger(l LoggerIface) *PgxLogger
    func (pgxlogger *PgxLogger) Log(ctx context.Context, level tracelog.LogLevel, msg string, data map[string]any)
type logger
type loggerKey

Examples

Formatter.Format (Default)

Package files

cmdopts.go formatter.go log.go log_broker_hook.go

Constants

const (
    colorRed     = 31
    colorBlue    = 36
    colorMagenta = 35
    colorGreen   = 32
)
const (
    disableColors = true
    enableColors  = false
)
const cacheLimit = 512
const highLoadLimit = 200 * time.Millisecond

Variables

FallbackLogger is an alias for the standard logger

var FallbackLogger = logrus.StandardLogger()
var defaultFormatter = &logrus.TextFormatter{DisableColors: true}

func WithLogger

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

func getColorByLevel(level logrus.Level) int

func getLogFileFormatter

func getLogFileFormatter(opts CmdOpts) logrus.Formatter

func getLogFileWriter

func getLogFileWriter(opts CmdOpts) any

func trimFilename

func trimFilename(s string) string

type BrokerHook

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

func NewBrokerHook(ctx context.Context, level string) *BrokerHook

NewBrokerHook creates a LogHook to be added to an instance of logger

func (*BrokerHook) AddSubscriber

func (hook *BrokerHook) AddSubscriber(msgCh MessageChanType)

AddSubscriber adds receiving channel to the subscription

func (*BrokerHook) Fire

func (hook *BrokerHook) Fire(entry *logrus.Entry) error

Fire adds logrus log message to the internal queue for processing

func (*BrokerHook) Levels

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

Levels returns the available logging levels

func (*BrokerHook) RemoveSubscriber

func (hook *BrokerHook) RemoveSubscriber(msgCh MessageChanType)

RemoveSubscriber deletes receiving channel from the subscription

func (*BrokerHook) SetBrokerFormatter

func (hook *BrokerHook) SetBrokerFormatter(formatter logrus.Formatter)

SetBrokerFormatter sets the format that will be used by hook.

func (*BrokerHook) poll

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 (*BrokerHook) send

func (hook *BrokerHook) send(entry *logrus.Entry)

send sends cached messages to the postgres server

type CmdOpts

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"`
}

type Formatter

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

func newFormatter(noColors bool) *Formatter

func (*Formatter) Format

func (f *Formatter) Format(entry *logrus.Entry) ([]byte, error)

Format an log entry

Example (Default)

Code:

l := logrus.New()
l.SetOutput(os.Stdout)
l.SetLevel(logrus.DebugLevel)
l.SetFormatter(&formatter.Formatter{
    NoColors:        true,
    TimestampFormat: "-",
})

l.Debug("test1")
l.Info("test2")
l.Warn("test3")
l.Error("test4")

Output:

- [DEBU] test1
- [INFO] test2
- [WARN] test3
- [ERRO] test4

func (*Formatter) writeCaller

func (f *Formatter) writeCaller(b *bytes.Buffer, entry *logrus.Entry)

func (*Formatter) writeField

func (f *Formatter) writeField(b *bytes.Buffer, entry *logrus.Entry, field string)

func (*Formatter) writeFields

func (f *Formatter) writeFields(b *bytes.Buffer, entry *logrus.Entry)

func (*Formatter) writeOrderedFields

func (f *Formatter) writeOrderedFields(b *bytes.Buffer, entry *logrus.Entry)

type LoggerHookerIface

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

func Init(opts CmdOpts) LoggerHookerIface

Init creates logging facilities for the application

type LoggerIface

LoggerIface is the interface used by all components

type LoggerIface logrus.FieldLogger

func GetLogger

func GetLogger(ctx context.Context) LoggerIface

GetLogger retrieves the current logger from the context. If no logger is available, the default logger is returned

type MessageChanType

MessageChanType represents the format of the message channel

type MessageChanType chan MessageType

type MessageType

MessageType represents the format of the message

type MessageType string

type PgxLogger

PgxLogger is the struct used to log using pgx postgres driver

type PgxLogger struct {
    l LoggerIface
}

func NewPgxLogger

func NewPgxLogger(l LoggerIface) *PgxLogger

NewPgxLogger returns a new instance of PgxLogger

func (*PgxLogger) Log

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

type logger struct {
    *logrus.Logger
    *BrokerHook
}

type loggerKey

type loggerKey struct{}