logging

package
v0.1.8 Latest Latest
Warning

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

Go to latest
Published: Jul 28, 2025 License: MIT Imports: 8 Imported by: 0

Documentation

Overview

Package logging provides a unified logging interface

Package logging provides a unified logging interface

Package logging provides a unified logging interface

Index

Constants

View Source
const (
	// LogEncodingConsole represents console encoding for logs
	LogEncodingConsole = "console"
	// LogEncodingJSON represents JSON encoding for logs
	LogEncodingJSON = "json"
	// EnvironmentDevelopment represents the development environment
	EnvironmentDevelopment = "development"
	// MaxPartsLength represents the maximum number of parts in a log message
	MaxPartsLength = 2
	// FieldPairSize represents the number of elements in a key-value pair
	FieldPairSize = 2
	// MaxStringLength represents the maximum length for string fields
	MaxStringLength = 1000
	// MaxPathLength represents the maximum length for path fields
	MaxPathLength = 500
	// MaxUserAgentLength represents the maximum length for user agent fields
	MaxUserAgentLength = 1000
	// UUIDLength represents the standard UUID length
	UUIDLength = 36
	// UUIDParts represents the number of parts in a UUID
	UUIDParts = 5
	// UUIDMinMaskLen represents the minimum length for UUID masking
	UUIDMinMaskLen = 8
	// UUIDMaskPrefixLen represents the prefix length for UUID masking
	UUIDMaskPrefixLen = 4
	// UUIDMaskSuffixLen represents the suffix length for UUID masking
	UUIDMaskSuffixLen = 4
)

Variables

This section is empty.

Functions

func CustomField added in v0.1.5

func CustomField(key string, value any, sanitizer func(any) string) zap.Field

CustomField creates a field with custom sanitization logic

func MaskedField added in v0.1.5

func MaskedField(key, value, mask string) zap.Field

MaskedField creates a field with custom masking applied to the value

func ObjectField added in v0.1.5

func ObjectField(key string, obj any) zap.Field

ObjectField creates a field with sanitized object data

func RequestID added in v0.1.5

func RequestID(key, value string) zap.Field

RequestID creates a field with validated request ID

func SafeString added in v0.1.5

func SafeString(key, value string) zap.Field

SafeString creates a field with a safe string value (no sanitization)

func Sanitized added in v0.1.5

func Sanitized(key, value string) zap.Field

Sanitized creates a field with sanitized string data

func SensitiveField added in v0.1.5

func SensitiveField(key string, value any) zap.Field

SensitiveField creates a field that automatically masks sensitive data

func TruncatedField added in v0.1.5

func TruncatedField(key, value string, maxLength int) zap.Field

TruncatedField creates a field with truncated value

Types

type Factory

type Factory struct {
	LogLevel string
	// contains filtered or unexported fields
}

Factory creates loggers based on configuration

func NewFactory

func NewFactory(cfg *FactoryConfig, sanitizer sanitization.ServiceInterface) (*Factory, error)

NewFactory creates a new logger factory with the given configuration

func (*Factory) CreateLogger

func (f *Factory) CreateLogger() (Logger, error)

CreateLogger creates a new logger instance with the application name.

func (*Factory) WithTestCore added in v0.1.5

func (f *Factory) WithTestCore(core zapcore.Core) *Factory

WithTestCore allows tests to inject a zapcore.Core for capturing logs

type FactoryConfig added in v0.1.5

type FactoryConfig struct {
	AppName          string
	Version          string
	Environment      string
	LogLevel         string
	OutputPaths      []string
	ErrorOutputPaths []string
	Fields           map[string]any
}

FactoryConfig holds configuration for logger factory

func (*FactoryConfig) Validate added in v0.1.5

func (cfg *FactoryConfig) Validate() error

Validate validates the factory configuration

type Field added in v0.1.5

type Field struct {
	Key   string
	Value any
	Type  FieldType
}

Field represents a structured log field with type information

func Bool

func Bool(key string, value bool) Field

Bool creates a boolean field

func Error

func Error(key string, err error) Field

Error creates an error field

func Float added in v0.1.5

func Float(key string, value float64) Field

Float creates a float field

func Int

func Int(key string, value int) Field

Int creates an integer field

func Int64

func Int64(key string, value int64) Field

Int64 creates an int64 field

func Object added in v0.1.5

func Object(key string, obj any) Field

Object creates an object field for complex data

func Path added in v0.1.5

func Path(key, value string) Field

Path creates a path field with validation

func Sensitive added in v0.1.5

func Sensitive(key string, value any) Field

Sensitive creates a sensitive field that will be masked

func String

func String(key, value string) Field

String creates a string field

func UUID added in v0.1.5

func UUID(key, value string) Field

UUID creates a UUID field with automatic masking

func UserAgent added in v0.1.5

func UserAgent(key, value string) Field

UserAgent creates a user agent field with sanitization

func (Field) ToZapField added in v0.1.5

func (f Field) ToZapField() zap.Field

ToZapField converts a Field to a zap.Field

type FieldType added in v0.1.5

type FieldType int

FieldType represents the type of a field

const (
	StringFieldType FieldType = iota
	IntFieldType
	FloatFieldType
	BoolFieldType
	ErrorFieldType
	ObjectFieldType
	UUIDFieldType
	PathFieldType
	UserAgentFieldType
	SensitiveFieldType
	// InvalidPathMessage is the message returned for invalid paths
	InvalidPathMessage = "[invalid path]"
	// UUIDDashCount is the number of dashes in a standard UUID
	UUIDDashCount = 4
)

type LogLevel added in v0.1.5

type LogLevel string

LogLevel represents the severity of a log message

const (
	// LogLevelDebug represents debug level logging
	LogLevelDebug LogLevel = "debug"
	// LogLevelInfo represents info level logging
	LogLevelInfo LogLevel = "info"
	// LogLevelWarn represents warning level logging
	LogLevelWarn LogLevel = "warn"
	// LogLevelError represents error level logging
	LogLevelError LogLevel = "error"
	// LogLevelFatal represents fatal level logging
	LogLevelFatal LogLevel = "fatal"
)

type Logger

type Logger interface {
	Debug(msg string, fields ...any)
	Info(msg string, fields ...any)
	Warn(msg string, fields ...any)
	Error(msg string, fields ...any)
	Fatal(msg string, fields ...any)
	With(fields ...any) Logger
	WithComponent(component string) Logger
	WithOperation(operation string) Logger
	WithRequestID(requestID string) Logger
	WithUserID(userID string) Logger
	WithError(err error) Logger
	WithFields(fields map[string]any) Logger
	// New Field-based API methods
	WithFieldsStructured(fields ...Field) Logger
	DebugWithFields(msg string, fields ...Field)
	InfoWithFields(msg string, fields ...Field)
	WarnWithFields(msg string, fields ...Field)
	ErrorWithFields(msg string, fields ...Field)
	FatalWithFields(msg string, fields ...Field)
	SanitizeField(key string, value any) string
}

Logger interface defines the logging contract

type Sanitizer added in v0.1.5

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

Sanitizer provides simplified field sanitization

func NewSanitizer added in v0.1.5

func NewSanitizer() *Sanitizer

NewSanitizer creates a new sanitizer instance

func (*Sanitizer) ClearCache added in v0.1.5

func (s *Sanitizer) ClearCache()

ClearCache clears the sanitization cache

func (*Sanitizer) GetCacheSize added in v0.1.5

func (s *Sanitizer) GetCacheSize() int

GetCacheSize returns the current cache size

func (*Sanitizer) SanitizeField added in v0.1.5

func (s *Sanitizer) SanitizeField(key string, value any) string

SanitizeField sanitizes a field value based on its key and type

type SensitiveObject added in v0.1.5

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

SensitiveObject creates a custom field that implements zapcore.ObjectMarshaler for complex objects that need sensitive data masking

func NewSensitiveObject added in v0.1.5

func NewSensitiveObject(key string, value any) SensitiveObject

NewSensitiveObject creates a new sensitive object field

func (SensitiveObject) Field added in v0.1.5

func (s SensitiveObject) Field() zap.Field

Field returns the SensitiveObject as a zap.Field

func (SensitiveObject) MarshalLogObject added in v0.1.5

func (s SensitiveObject) MarshalLogObject(enc zapcore.ObjectEncoder) error

MarshalLogObject implements zapcore.ObjectMarshaler

Jump to

Keyboard shortcuts

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