logger

package
v0.0.0-...-a7c217f Latest Latest
Warning

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

Go to latest
Published: Apr 30, 2024 License: AGPL-3.0 Imports: 13 Imported by: 36

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrInvalidDomainFormat = errors.New("invalid domain format")
)
View Source
var (
	ErrInvalidLevel = errors.New("not a valid logging Level")
)

Functions

func AddDebugDomain

func AddDebugDomain(domain string, ttl time.Duration) error

AddDebugDomain adds the specified domain to the debug list.

func DebugExpiration

func DebugExpiration(domain string) *time.Time

DebugExpiration returns the expiration date for the debug mode for the instance logger of the given domain (or nil if the debug mode is not activated).

func Init

func Init(opt Options) error

Init initializes the logger module with the specified options.

It also setup the global logger for go-redis. Thoses are at Info level.

func RemoveDebugDomain

func RemoveDebugDomain(domain string) error

RemoveDebugDomain removes the specified domain from the debug list.

func SyslogHook

func SyslogHook() (logrus.Hook, error)

SyslogHook return a logrus.Hook sending all the logs to a local syslog server via a socket.

Types

type Debugger

type Debugger interface {
	AddDomain(domain string, ttl time.Duration) error
	RemoveDomain(domain string) error
	ExpiresAt(domain string) *time.Time
}

Debugger manage the list of domains with the debug mode.

Once you call `AddDomain` all Debug logs containing the corresponding `domain` field (setup with `WithDomain`) will be printed even if the global logger is setup with a higher level (like 'info').

type Entry

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

Entry is the struct on which we can call the Debug, Info, Warn, Error methods with the structured data accumulated.

func WithDomain

func WithDomain(domain string) *Entry

WithDomain returns a logger with the specified domain field.

func WithNamespace

func WithNamespace(nspace string) *Entry

WithNamespace returns a logger with the specified nspace field.

func (*Entry) AddHook

func (e *Entry) AddHook(hook logrus.Hook)

AddHook adds a hook on a logger.

func (*Entry) Debug

func (e *Entry) Debug(msg string)

func (*Entry) Debugf

func (e *Entry) Debugf(format string, args ...interface{})

func (*Entry) Error

func (e *Entry) Error(msg string)

func (*Entry) Errorf

func (e *Entry) Errorf(format string, args ...interface{})

func (*Entry) Info

func (e *Entry) Info(msg string)

func (*Entry) Infof

func (e *Entry) Infof(format string, args ...interface{})

func (*Entry) IsDebug

func (e *Entry) IsDebug() bool

IsDebug returns whether or not the debug mode is activated.

func (*Entry) Log

func (e *Entry) Log(level Level, msg string)

func (*Entry) Warn

func (e *Entry) Warn(msg string)

func (*Entry) Warnf

func (e *Entry) Warnf(format string, args ...interface{})

func (*Entry) WithDomain

func (e *Entry) WithDomain(domain string) Logger

WithDomain add a domain field.

func (*Entry) WithField

func (e *Entry) WithField(key string, value interface{}) Logger

WithField adds a single field to the Entry.

func (*Entry) WithFields

func (e *Entry) WithFields(fields Fields) Logger

WithFields adds a map of fields to the Entry.

func (*Entry) WithNamespace

func (e *Entry) WithNamespace(nspace string) *Entry

WithNamespace adds a namespace (nspace field).

func (*Entry) WithTime

func (e *Entry) WithTime(t time.Time) Logger

WithTime overrides the Entry's time

func (*Entry) Writer

func (e *Entry) Writer() *io.PipeWriter

type Fields

type Fields map[string]interface{}

Fields type, used to pass to [Logger.WithFields].

type Level

type Level uint8

Level type

const (

	// ErrorLevel logs important errors when failure append.
	ErrorLevel Level

	// WarnLevel logs non critical entries that deserve some intention.
	WarnLevel

	// InfoLevel log general operational entries about what's going on inside
	// the application.
	InfoLevel

	// DebugLevel logs is only enabled when debugging is setup. It can be
	// very verbose logging and should be activated only on a limited period.
	DebugLevel
)

These are the different logging levels.

func ParseLevel

func ParseLevel(lvl string) (Level, error)

ParseLevel takes a string level and returns the log level constant.

func (Level) MarshalText

func (level Level) MarshalText() ([]byte, error)

MarshalText implements encoding.TextMarshaler.

func (Level) String

func (level Level) String() string

String converts the Level to a string. E.g. LevelDebug becomes "debug".

func (*Level) UnmarshalText

func (level *Level) UnmarshalText(text []byte) error

UnmarshalText implements encoding.TextUnmarshaler.

type Logger

type Logger interface {
	Debugf(format string, args ...interface{})
	Infof(format string, args ...interface{})
	Warnf(format string, args ...interface{})
	Errorf(format string, args ...interface{})

	Debug(msg string)
	Info(msg string)
	Warn(msg string)
	Error(msg string)

	// Generic field operations
	WithField(fn string, fv interface{}) Logger
	WithFields(fields Fields) Logger

	// Business specific field operations.
	WithTime(t time.Time) Logger
	WithDomain(s string) Logger

	Log(level Level, msg string)
}

Logger allows to emits logs to the divers log systems.

type MemDebugger

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

MemDebugger is a in-memory based Debugger implementation.

This implem is local only and is not suited for any multi-instance setup.

func NewMemDebugger

func NewMemDebugger() *MemDebugger

NewMemDebugger instantiate a new MemDebugger.

func (*MemDebugger) AddDomain

func (m *MemDebugger) AddDomain(domain string, ttl time.Duration) error

AddDomain adds the specified domain to the debug list.

func (*MemDebugger) ExpiresAt

func (m *MemDebugger) ExpiresAt(domain string) *time.Time

ExpiresAt returns the expiration time for this domain.

If this domain is not in debug mode, it returns `nil`.

func (*MemDebugger) RemoveDomain

func (m *MemDebugger) RemoveDomain(domain string) error

RemoveDomain removes the specified domain from the debug list.

type Options

type Options struct {
	Hooks  []logrus.Hook
	Output io.Writer
	Level  string
	Redis  redis.UniversalClient
}

Options contains the configuration values of the logger system

type RedisDebugger

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

RedisDebugger is a redis based Debugger implementation.

It use redis to synchronize all the instances between them. This implementation is safe to use in a multi instance setup.

Technically speaking this is a wrapper around a MemDebugger using redis pub/sub and store for syncing the instances between them and restoring the domain list at startup.

func NewRedisDebugger

func NewRedisDebugger(client redis.UniversalClient) (*RedisDebugger, error)

NewRedisDebugger instantiates a new RedisDebugger, bootstraps the service with the state saved in redis and starts the subscription to the change channel.

func (*RedisDebugger) AddDomain

func (r *RedisDebugger) AddDomain(domain string, ttl time.Duration) error

AddDomain adds the specified domain to the debug list.

func (*RedisDebugger) Close

func (r *RedisDebugger) Close() error

Close the redis client and the subscription channel.

func (*RedisDebugger) ExpiresAt

func (r *RedisDebugger) ExpiresAt(domain string) *time.Time

func (*RedisDebugger) RemoveDomain

func (r *RedisDebugger) RemoveDomain(domain string) error

RemoveDomain removes the specified domain from the debug list.

Jump to

Keyboard shortcuts

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