logger

package
v0.1.2 Latest Latest
Warning

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

Go to latest
Published: Apr 29, 2024 License: MPL-2.0 Imports: 19 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultCronLogger cronLoggerType

GcpLoggingSeverityMap contains the default zerolog.Level -> logging.Severity mappings.

Logger is the default zerolog.Logger in this package. It should be initialized with InitLoggerr, otherwise it defaults to zerolog's standard default logger

It is used in this package's convenience functions like Info(), Error()

Functions

func Debug

func Debug() *zerolog.Event

func Error

func Error() *zerolog.Event

func Fatal

func Fatal() *zerolog.Event

func Info

func Info() *zerolog.Event

func InitLogger

func InitLogger(ctx context.Context, config Config) (err error)

InitLogger initializes the loggers in accordance to the configuration passed, which initiates the default exported Logger

While the configuration can be passed into this function, this function is intended to help configure a logger quickly from YAML based configuration.

refer tho the repo root's example.config.yaml for reference.

func InitRotatedfileWriterRotation

func InitRotatedfileWriterRotation(ctx context.Context, config RotatedFilewriterConfig, destination ...*WriterStructWithDefaultDestination) (err error)

InitRotatedfileWriterRotation creates a new rotated file logger. Only one (optional) destination should be specified. If the (optional) destination is not specified, this rotates the packages' internal rotated file writer destination

This should be only called once

func LeveledWriter added in v0.0.7

func LeveledWriter(logger *zerolog.Logger, severity zerolog.Level) leveledWriter

LeveledWriter creates a io.Writer that writes to the log with the specified severity and the passed bytes as Msg

This makes it compatible with stdlib's Log Writer, while maintaining a severity level consistent with other logs

func NewCronLogger

func NewCronLogger(logger *zerolog.Logger) *cronLoggerType

NewCronLogger accepts a custom zerolog logger and adapts it to a cron.Logger (github.com/robfig/cron/v3)

The rationale of providing an adaptor to the excellent cron is to provide a levelled and structured logging to the cron, since the cron's logger Info and Error interface is more akin to non structured loggers. This provides automatic recognition of field key if the even indexed elements is string, helping transforming it to a map[string]interface{} passed to zerolog.Logger as Interface()

func NewGcpLoggingWriter

func NewGcpLoggingWriter(ctx context.Context, config GcpLoggingConfig, gcpLogOptions ...logging.LoggerOption) (writer zerolog.LevelWriter, err error)

func Panic

func Panic() *zerolog.Event

func Trace

func Trace() *zerolog.Event

func Warn

func Warn() *zerolog.Event

Types

type Config

type Config struct {
	Destinations      outputDestinationsSetting `yaml:"Destinations" env:"LOG_DESTINATIONS"`
	GcpLoggingConfig  GcpLoggingConfig          `yaml:"GcpLoggingConfig"`
	RotatedFileConfig RotatedFilewriterConfig   `yaml:"RotatedFileConfig"`
}

func (*Config) CreateLogger added in v0.1.0

func (config *Config) CreateLogger(ctx context.Context) (logger zerolog.Logger, err error)

CreateLogger initializes the loggers in accordance to config

type GcpLoggingConfig

type GcpLoggingConfig struct {
	ProjectId              string            `yaml:"ProjectId" env:"GCPLOGGING_PROJECTID"`
	LogId                  string            `yaml:"LogId" env:"GCPLOGGING_LOGID"`
	ServiceAccountFilePath string            `yaml:"ServiceAccountFilePath" env:"GOOGLE_APPLICATION_CREDENTIALS"`
	ServiceAccountJSON     string            `yaml:"ServiceAccountJSON" env:"GCPLOGGING_SERVICEACCOUNTJSON"`
	DefaultLabels          map[string]string `yaml:"DefaultLabels" env:"GCPLOGGING_DEFAULTLABELS"`
}

type GcpLoggingSeverityMapType

type GcpLoggingSeverityMapType map[zerolog.Level]logging.Severity

type GcpLoggingWriter

type GcpLoggingWriter struct {
	zerolog.LevelWriter
	// contains filtered or unexported fields
}

func (*GcpLoggingWriter) Write

func (c *GcpLoggingWriter) Write(p []byte) (int, error)

func (*GcpLoggingWriter) WriteLevel

func (c *GcpLoggingWriter) WriteLevel(level zerolog.Level, payload []byte) (int, error)

type RotatedFilewriterConfig

type RotatedFilewriterConfig struct {
	FolderPath          string `yaml:"FolderPath" env:"LOGROTATE_FOLDER_PATH"`
	RotateCronFrequency string `yaml:"RotateCronFrequency" env:"LOGROTATE_ROTATE_CRON_FREQUENCY"`
	FileNameFormat      string `yaml:"FileNameFormat" env:"LOGROTATE_FILE_NAME_FORMAT"`
	MaxLogFilesKept     int    `yaml:"MaxLogFilesKept" env:"LOGROTATE_MAX_LOG_FILES_KEPT"`
	MinLogFilesKept     int    `yaml:"MinLogFilesKept" env:"LOGROTATE_MIN_LOG_FILES_KEPT"`
	MaxTotalLogFileSize string `yaml:"MaxTotalLogFileSize" env:"LOGROTATE_MAX_TOTAL_LOG_FILE_SIZE"`
}

type WriterStructWithDefaultDestination

type WriterStructWithDefaultDestination struct {
	// when set, will write to the destination
	// otherwise, will write to the default package rotatedLogFile
	Destination io.Writer
	// contains filtered or unexported fields
}

WriterStructWithDefaultDestination provides a way to change the destination io.Writer while maintaining the pointer to the struct itself, allowing the log's destination writer to be changed without reinitiating the log itself.

Intended to be used with constructs like file rotation

func (*WriterStructWithDefaultDestination) SetOutput

func (w *WriterStructWithDefaultDestination) SetOutput(output io.Writer)

SetOutput changes the destination. If the destination is io.WriteCloser, it will be closed before reassignment

func (*WriterStructWithDefaultDestination) Write

Jump to

Keyboard shortcuts

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