logger

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Nov 11, 2025 License: Apache-2.0 Imports: 11 Imported by: 0

Documentation

Overview

Package logger is a specific logging library on top of slog with additional goodness

Index

Constants

View Source
const (
	// DefaultVerboseFormat is the default format for verbose (DEBUG to stdout) output
	DefaultVerboseFormat = "."

	// DefaultFriendlyFormat is the default format for all CLI friendly output (INFO and above to stdout)
	DefaultFriendlyFormat = `"[\(.level)] - \(.ts) - \(.operation) - \(.message) - \(.attributes)"`
)

Default output formats

View Source
const (
	SyslogFacilityKern     = "kern"
	SyslogFacilityUser     = "user"
	SyslogFacilityMail     = "mail"
	SyslogFacilityDaemon   = "daemon"
	SyslogFacilityAuth     = "auth"
	SyslogFacilitySyslog   = "syslog"
	SyslogFacilityNews     = "news"
	SyslogFacilityUucp     = "uucp"
	SyslogFacilityCron     = "cron"
	SyslogFacilityAuthpriv = "authpriv"
	SyslogFacilityFtp      = "ftp"
	SyslogFacilityLocal0   = "local0"
	SyslogFacilityLocal1   = "local1"
	SyslogFacilityLocal2   = "local2"
	SyslogFacilityLocal3   = "local3"
	SyslogFacilityLocal4   = "local4"
	SyslogFacilityLocal5   = "local5"
	SyslogFacilityLocal6   = "local6"
	SyslogFacilityLocal7   = "local7"
)
View Source
const (
	BusinessType    = "Business"
	SecurityType    = "Security"
	PerformanceType = "Performance"
)

All the different Log Types

View Source
const (
	CORRELATION_ID ctxKey = "correlationid"
	TYPE           ctxKey = "type"
	APPLICATION    ctxKey = "application"
	OPERATION      ctxKey = "operation"
)

Contract fields expected in the Context to be available for logging purposes

View Source
const RFC3339NanoMC = "2006-01-02T15:04:05.999Z0700"

RFC3339NanoMC is the desired timestamp output format

Variables

ALLOWED_TYPES are the allowed values for TYPE

View Source
var REQUIRED_FIELDS = []ctxKey{
	TYPE,
	APPLICATION,
	OPERATION,
}

REQUIRED_FIELDS are the fields checked against when MangoConfig.Strict is set

Functions

func ToMap

func ToMap(attrs []slog.Attr) map[string]interface{}

Helper function to convert []slog.Attr to a map[string]interface{}

Types

type CliConfig

type CliConfig struct {
	// Enabled allows stdout/stderr printouts
	Enabled bool `yaml:"enabled" json:"enabled"`

	// Friendly enables a human friendly output to stdout/stderr
	// When false it outputs json format as in file output
	Friendly bool `yaml:"friendly" json:"friendly"`

	// FriendlyFormat of the output in normal scenarios and if Friendly enabled
	// Defaults to DefaultFriendlyFormat applied to all log statements info+
	FriendlyFormat string `yaml:"friendly-format" json:"friendlyFormat"`

	// Verbose Enable debug to come out to std out following the VerboseFormat
	Verbose bool `yaml:"verbose" json:"verbose"`

	// VerboseFormat of the DEBUG statements output in verbose mode
	// Defaults to print the whole json object of logger.StructuredLog (using DefaultVerboseFormat)
	VerboseFormat string `yaml:"verbose-format" json:"verboseFormat"`
}

type CorrelationIdConfig

type CorrelationIdConfig struct {
	// Strict enforces CorrelationId as part of the REQUIRED_FIELDS to be present in each log context
	Strict bool `yaml:"strict" json:"strict"`

	// AutoGenerate will generate a correlationId if missing from context
	// This will NOT be generated BEFORE REQUIRED_FIELDS restriction, therefore if correlationId is missing in a strict setup, it will fail regardless of auto-generate flag
	AutoGenerate bool `yaml:"auto-generate" json:"autoGenerate"`
}

CorrelationIdConfig defines the configuration of correlationId across mangologger

type FileOutputConfig

type FileOutputConfig struct {
	// Enabled switches on printing out to file
	Enabled bool `yaml:"enabled" json:"enabled"`

	// Debug allows debug printout to file
	Debug bool `yaml:"debug" json:"debug"`

	// Path is the log file name - It uses <processname>-lumberjack.log in os.TempDir() if empty.
	Path string `yaml:"path" json:"path"`

	// MaxSize in MB before rotating - It defaults to 100 megabytes
	MaxSize int `yaml:"max-size" json:"maxSize"`

	// MaxBackups is the number of old log files to keep - The default is to retain all old log files
	MaxBackups int `yaml:"max-backups" json:"maxBackups"`

	// MaxAge is the number of days to keep old log files - The default is not to remove old log files based on age
	MaxAge int `yaml:"max-age" json:"maxAge"`

	// Compress old log files - The default is not to perform compression
	Compress bool `yaml:"compress" json:"compress"`
}

type LogConfig

type LogConfig struct {
	// MangoConfig is the mango configuration node
	MangoConfig *MangoConfig `yaml:"mango" json:"mango"`

	// Out is the node holding configuration about the file output
	Out *OutConfig `yaml:"out" json:"out"`
}

LogConfig is the main configuration struct for Mango logging

type MangoConfig

type MangoConfig struct {
	// Strict Will enforce the REQUIRED_FIELDS to be present in each log context
	Strict bool `yaml:"strict" json:"strict"`

	// CorrelationId configuration
	CorrelationId *CorrelationIdConfig `yaml:"correlation-id" json:"correlationId"`
}

type MangoLogger

type MangoLogger struct {
	Config    *LogConfig
	LogWriter *lumberjack.Logger
	// contains filtered or unexported fields
}

func NewMangoLogger

func NewMangoLogger(config *LogConfig) *MangoLogger

func (MangoLogger) Enabled

func (sl MangoLogger) Enabled(context context.Context, level slog.Level) bool

func (MangoLogger) Handle

func (sl MangoLogger) Handle(context context.Context, record slog.Record) error

func (MangoLogger) WithAttrs

func (sl MangoLogger) WithAttrs(attrs []slog.Attr) slog.Handler

func (MangoLogger) WithGroup

func (sl MangoLogger) WithGroup(name string) slog.Handler

type OutConfig

type OutConfig struct {
	// Overall output enable - killer switch to all output of mangologger
	Enabled bool `yaml:"enabled" json:"enabled"`

	// File output configuration
	File *FileOutputConfig `yaml:"file" json:"file"`

	// Cli configuration node for CLI output options
	Cli *CliConfig `yaml:"cli" json:"cli"`

	// Syslog configuration node for Syslog output options
	Syslog *SyslogConfig `yaml:"syslog" json:"syslog"`
}

OutConfig provides a structure for defining the configuration of all the logging output

type StructuredLog

type StructuredLog struct {
	// Timestamp of the log entry
	Timestamp string `json:"ts"`

	// Type of the log entry. One of: Security, Business, or Performance
	Type string `json:"type"`

	// Application of which this log entry belongs. Should be corresponding with TAT
	Application string `json:"application"`

	// Operation is synonymous with the application/system's function or method.
	// Consider such examples: search, create, health, user_registration, checkout, token_issueance, case-status, etc.
	Operation string `json:"operation"`

	// Correlationid from the caller or self generated allowing to relate different systems around one
	Correlationid string `json:"correlationid"`

	// LogId is a unique identifier for each log entry - Helps in referring to logs when searching
	LogId string `json:"logId"`

	// Level of the log entry (slog.Debug, slog.Info, slog.Warn, slog.Error)
	Level slog.Level `json:"level"`

	// Message is the actual message of the log entry
	Message any `json:"message"`

	// Attributes set with slog or on the logger
	Attributes map[string]interface{} `json:"attributes"`
}

StructuredLog is the structure of every log entry (output)

type SyslogConfig

type SyslogConfig struct {
	// Facility refers to the syslog facility of a given log
	Facility SyslogFacility `yaml:"facility" json:"facility"`
	// contains filtered or unexported fields
}

type SyslogFacility

type SyslogFacility string

Jump to

Keyboard shortcuts

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