logger

package
v0.4.5 Latest Latest
Warning

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

Go to latest
Published: Feb 4, 2025 License: MIT Imports: 10 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var DefaultConfig = Config{
	Level:       "info",
	OutputPaths: []string{"stdout"},
	ErrorOutputPaths: []string{
		"stderr",
	},
	Development:  false,
	LogToConsole: false,
	Sampling: SamplingConfig{
		Initial:    100,
		Thereafter: 100,
	},
	EncodingConfig: EncodingConfig{
		TimeKey:         "time",
		LevelKey:        "level",
		NameKey:         "logger",
		CallerKey:       "caller",
		MessageKey:      "msg",
		StacktraceKey:   "stacktrace",
		LineEnding:      "\n",
		LevelEncoder:    "lowercase",
		TimeEncoder:     "iso8601",
		DurationEncoder: "string",
		CallerEncoder:   "short",
	},
	LogRotation: LogRotationConfig{
		Enabled:    true,
		MaxSizeMB:  100,
		MaxBackups: 7,
		MaxAgeDays: 30,
		Compress:   true,
	},
	Sanitization: SanitizationConfig{
		SensitiveFields: []string{
			"password",
			"token",
			"access_token",
			"refresh_token",
		},
		Mask: "****",
	},
}

Provides fallback logging settings for any logger not specified in log.config.json.

Functions

func Init

func Init(configPaths []string, manager *LoggerManager) error

Init initializes the loggers based on multiple configuration files. It should be called once at the start of the application.

Types

type AsyncCore

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

wraps a zapcore.Core and handles asynchronous, batched logging.

func NewAsyncCore

func NewAsyncCore(core zapcore.Core, bufferSize, batchSize int, flushInterval time.Duration) *AsyncCore

initializes a new AsyncCore with batching and tracking. bufferSize: size of the buffered channel batchSize: number of log entries per batch flushInterval: maximum time to wait before flushing a batch

func (*AsyncCore) Check

func (ac *AsyncCore) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry

enqueues the entry if enabled.

func (*AsyncCore) Enabled

func (ac *AsyncCore) Enabled(level zapcore.Level) bool

func (*AsyncCore) Sync

func (ac *AsyncCore) Sync() error

flushes all buffered log entries and syncs the underlying core.

func (*AsyncCore) With

func (ac *AsyncCore) With(fields []zapcore.Field) zapcore.Core

func (*AsyncCore) Write

func (ac *AsyncCore) Write(entry zapcore.Entry, fields []zapcore.Field) error

enqueues the log entry along with its fields.

type Config

type Config struct {
	Level            string             `json:"level"`
	OutputPaths      []string           `json:"outputPaths"`
	ErrorOutputPaths []string           `json:"errorOutputPaths"`
	Development      bool               `json:"development"`
	LogToConsole     bool               `json:"logToConsole"`
	Sampling         SamplingConfig     `json:"sampling"`
	EncodingConfig   EncodingConfig     `json:"encodingConfig"`
	LogRotation      LogRotationConfig  `json:"logRotation"`
	Sanitization     SanitizationConfig `json:"sanitization"`
}

type EncodingConfig

type EncodingConfig struct {
	TimeKey         string `json:"timeKey"`
	LevelKey        string `json:"levelKey"`
	NameKey         string `json:"nameKey"`
	CallerKey       string `json:"callerKey"`
	MessageKey      string `json:"messageKey"`
	StacktraceKey   string `json:"stacktraceKey"`
	LineEnding      string `json:"lineEnding"`
	LevelEncoder    string `json:"levelEncoder"`
	TimeEncoder     string `json:"timeEncoder"`
	DurationEncoder string `json:"durationEncoder"`
	CallerEncoder   string `json:"callerEncoder"`
}

type LogEntry

type LogEntry struct {
	Entry  zapcore.Entry
	Fields []zapcore.Field
}

type LogRotationConfig

type LogRotationConfig struct {
	Enabled    bool `json:"enabled"`
	MaxSizeMB  int  `json:"maxSizeMB"`
	MaxBackups int  `json:"maxBackups"`
	MaxAgeDays int  `json:"maxAgeDays"`
	Compress   bool `json:"compress"`
}

type LoggerManager

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

func NewLoggerManager

func NewLoggerManager(logsConfigPaths []string) (*LoggerManager, error)

func (*LoggerManager) AddLogger

func (lm *LoggerManager) AddLogger(name string, logger *zap.Logger) error

adds a new logger to the manager. Returns error if a logger with the same name already exists.

func (*LoggerManager) GetAllLoggers

func (lm *LoggerManager) GetAllLoggers() map[string]*zap.Logger

Returns a copy of the map containing all loggers.

func (*LoggerManager) GetLogger

func (lm *LoggerManager) GetLogger(name string) (*zap.Logger, error)

Retrieves a logger by name. Returns error if logger doesn't exist.

func (*LoggerManager) RemoveLogger

func (lm *LoggerManager) RemoveLogger(name string) error

Removes a logger from the manager. Returns error if logger doesn't exist.

func (*LoggerManager) Sync

func (lm *LoggerManager) Sync() error

Sync flushes all loggers managed by LoggerManager. Returns a multi-error containing all sync errors encountered.

type SamplingConfig

type SamplingConfig struct {
	Initial    int `json:"initial"`
	Thereafter int `json:"thereafter"`
}

type SanitizationConfig

type SanitizationConfig struct {
	SensitiveFields []string `json:"sensitiveFields"`
	Mask            string   `json:"mask"`
}

SanitizationConfig configures sensitive field sanitization.

type SanitizerCore

type SanitizerCore struct {
	zapcore.Core
	// contains filtered or unexported fields
}

wraps a zapcore.Core and sanitizes log entries.

func NewSanitizerCore

func NewSanitizerCore(core zapcore.Core, sensitiveFields []string, mask string) *SanitizerCore

func (*SanitizerCore) Check

func (s *SanitizerCore) Check(entry zapcore.Entry, checkedEntry *zapcore.CheckedEntry) *zapcore.CheckedEntry

determines whether the supplied entry should be logged.

func (*SanitizerCore) Sync

func (s *SanitizerCore) Sync() error

flushe buffered logs (if any).

func (*SanitizerCore) With

func (s *SanitizerCore) With(fields []zapcore.Field) zapcore.Core

adds structured context to the core.

func (*SanitizerCore) Write

func (s *SanitizerCore) Write(entry zapcore.Entry, fields []zapcore.Field) error

type ZapWriter

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

adapter implements io.Writer and writes to Zap logger.

func NewZapWriter

func NewZapWriter(logger *zap.Logger, level zapcore.Level, prefix string) *ZapWriter

- logger: the Zap structured logger. - level: the log level at which messages should be logged. - prefix: the prefix to include as a separate field (optional).

func (*ZapWriter) Write

func (w *ZapWriter) Write(p []byte) (n int, err error)

Write implements the io.Writer interface.

Jump to

Keyboard shortcuts

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