logging

package
v1.29.0 Latest Latest
Warning

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

Go to latest
Published: Dec 31, 2025 License: Apache-2.0 Imports: 10 Imported by: 0

README

Logging Package

The logging package provides advanced logging capabilities with automatic PII (Personally Identifiable Information) detection and sanitization.

Features

  • 🔒 Automatic PII Detection: Identifies and masks sensitive data patterns
  • 📊 Multiple Log Levels: Debug, Info, Warn, Error, Fatal
  • 🎯 Structured Logging: JSON output format support
  • 🔧 Configurable: Environment variables and programmatic configuration
  • 🚀 High Performance: Minimal overhead with cached regex patterns
  • 🎨 Customizable: Add your own PII patterns

Quick Example

package main

import (
    "github.com/patdeg/common/logging"
)

func main() {
    // Create a logger
    logger := logging.NewLogger()
    
    // Log with automatic PII sanitization
    logger.Info("User logged in: john@example.com")
    // Output: [INFO] User logged in: j***n@***.com
    
    // Add custom pattern
    logger.Sanitizer.AddCustomPattern("account", `\bACCT\d{8}\b`)
    
    // Enable JSON output
    logger.SetJSONOutput(true)
}

Files

  • safe.go - Core logger implementation with PII-safe logging
  • sanitizer.go - PII detection and masking engine
  • README.md - This file

See Also

Documentation

Overview

Package logging provides PII-safe logging functionality for applications. It includes automatic sanitization of personally identifiable information (PII) and structured logging capabilities.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Debug

func Debug(format string, v ...interface{})

Debug logs a debug message with PII sanitization

func Error

func Error(format string, v ...interface{})

Error logs an error message with PII sanitization

func Fatal

func Fatal(format string, v ...interface{})

Fatal logs a fatal message and exits the program

func Info

func Info(format string, v ...interface{})

Info logs an informational message with PII sanitization

func SetJSONOutput

func SetJSONOutput(enabled bool)

SetJSONOutput enables or disables JSON output for the default logger

func SetLevel

func SetLevel(level LogLevel)

SetLevel sets the minimum log level for the default logger

func SetPrefix

func SetPrefix(prefix string)

SetPrefix sets a prefix for all log messages from the default logger

func Warn

func Warn(format string, v ...interface{})

Warn logs a warning message with PII sanitization

Types

type LogEntry

type LogEntry struct {
	Timestamp string                 `json:"timestamp"`
	Level     string                 `json:"level"`
	Message   string                 `json:"message"`
	Source    string                 `json:"source,omitempty"`
	Metadata  map[string]interface{} `json:"metadata,omitempty"`
}

LogEntry represents a structured log entry

type LogLevel

type LogLevel int

LogLevel represents the severity level of a log message

const (
	// DebugLevel logs are typically only enabled in development
	DebugLevel LogLevel = iota
	// InfoLevel logs are informational messages
	InfoLevel
	// WarnLevel logs are warning messages
	WarnLevel
	// ErrorLevel logs are error messages
	ErrorLevel
	// FatalLevel logs are fatal messages that will cause the program to exit
	FatalLevel
)

type LogSanitizer

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

LogSanitizer sanitizes log messages to remove PII

func NewLogSanitizer

func NewLogSanitizer() *LogSanitizer

NewLogSanitizer creates a new log sanitizer with default settings

func (*LogSanitizer) AddCustomPattern

func (ls *LogSanitizer) AddCustomPattern(name string, pattern string) error

AddCustomPattern adds a custom pattern for sanitization

func (*LogSanitizer) Sanitize

func (ls *LogSanitizer) Sanitize(message string) string

Sanitize removes or masks PII from a log message

func (*LogSanitizer) SanitizeStruct

func (ls *LogSanitizer) SanitizeStruct(data interface{}) map[string]interface{}

SanitizeStruct sanitizes a struct by masking PII in string fields

func (*LogSanitizer) SetEmailMasking

func (ls *LogSanitizer) SetEmailMasking(enabled bool)

SetEmailMasking enables or disables email masking

func (*LogSanitizer) SetIPMasking

func (ls *LogSanitizer) SetIPMasking(enabled bool)

SetIPMasking enables or disables IP address masking

type Logger

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

Logger provides PII-safe logging functionality

var (
	// DefaultLogger is the default logger instance
	DefaultLogger *Logger
)

func NewLogger

func NewLogger() *Logger

NewLogger creates a new logger instance

func (*Logger) Debug

func (l *Logger) Debug(format string, v ...interface{})

Debug logs a debug message with PII sanitization

func (*Logger) Error

func (l *Logger) Error(format string, v ...interface{})

Error logs an error message with PII sanitization

func (*Logger) Fatal

func (l *Logger) Fatal(format string, v ...interface{})

Fatal logs a fatal message and exits the program

func (*Logger) Info

func (l *Logger) Info(format string, v ...interface{})

Info logs an informational message with PII sanitization

func (*Logger) SetJSONOutput

func (l *Logger) SetJSONOutput(enabled bool)

SetJSONOutput enables or disables JSON output format

func (*Logger) SetLevel

func (l *Logger) SetLevel(level LogLevel)

SetLevel sets the minimum log level

func (*Logger) SetPrefix

func (l *Logger) SetPrefix(prefix string)

SetPrefix sets a prefix for all log messages

func (*Logger) Warn

func (l *Logger) Warn(format string, v ...interface{})

Warn logs a warning message with PII sanitization

Jump to

Keyboard shortcuts

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