Skip to content

Security Filtering

DD has built-in sensitive-data filtering that automatically detects and redacts passwords, keys, tokens, and other sensitive information in logs.

SensitiveDataFilter

Regex-based sensitive-data filter supporting dynamic patterns and caching.

Creation

FunctionSignatureDescription
NewSensitiveDataFilter() *SensitiveDataFilterFull pattern set
NewEmptySensitiveDataFilter() *SensitiveDataFilterEmpty filter
NewCustomSensitiveDataFilter(patterns ...string) (*SensitiveDataFilter, error)Custom patterns

Methods

MethodSignatureDescription
AddPattern(pattern string) errorAdd a regex pattern
AddPatterns(patterns ...string) errorAdd patterns in batch
ClearPatterns()Clear all patterns
PatternCount() intPattern count
Enable()Enable filtering
Disable()Disable filtering
IsEnabled() boolWhether enabled
Filter(input string) stringFilter a string
FilterFieldValue(key string, value any) anyFilter a single field value
FilterValueRecursive(key string, value any) anyRecursively filter nested structures
GetFilterStats() FilterStatsGet filtering statistics
ActiveGoroutineCount() int32Active filter goroutines
WaitForGoroutines(timeout time.Duration) boolWait for filter goroutines to finish
Close() boolClose the filter and release caches

Custom Patterns

go
filter, _ := dd.NewCustomSensitiveDataFilter(
    `(?i)password\s*[:=]\s*\S+`,     // Password
    `(?i)api[_-]?key\s*[:=]\s*\S+`,  // API Key
    `\b\d{16,19}\b`,                  // Credit card number
)

SecurityConfig

Security configuration struct controlling filter behavior and security level.

go
type SecurityConfig struct {
    MaxMessageSize  int                       // Message size cap in bytes (0 means no limit; preset configs default to 5MB)
    MaxWriters      int                       // Max writer count (preset configs default to 100)
    SensitiveFilter *SensitiveDataFilter      // Sensitive-data filter
    RateLimitConfig *internal.RateLimitConfig // Rate-limit config (internal type; nil disables rate limiting; preset configs do not populate this field)
}

About RateLimitConfig

RateLimitConfig controls log rate limiting, used to prevent log flooding (DoS) and keep the system stable under high load. The field is an internal type (*internal.RateLimitConfig) and cannot be constructed directly. All preset configs (DefaultSecurityConfig, DefaultSecureConfig, SecurityConfigForLevel, etc.) do not populate this field, i.e. rate limiting is disabled by default; the Logger initializes a rate limiter only when it is explicitly set. To disable rate limiting, set it to nil.

FilterStats

Filter statistics struct, used for monitoring and observability.

go
type FilterStats struct {
    ActiveGoroutines  int32         // Currently active filter goroutines
    PatternCount      int32         // Number of registered sensitive-data patterns
    SemaphoreCapacity int           // Max concurrent filter operations
    MaxInputLength    int           // Input length truncation threshold
    Enabled           bool          // Whether filtering is enabled
    TotalFiltered     int64         // Total filter operations
    TotalRedactions   int64         // Total redactions
    TotalTimeouts     int64         // Total timeouts
    AverageLatency    time.Duration // Average filter latency
    CacheHits         int64         // Cache hits
    CacheMiss         int64         // Cache misses
}

SecurityLevel

Security level enum, used with SecurityConfigForLevel to quickly obtain preset configurations.

go
type SecurityLevel int

Implements a String() method returning a readable level name.

ConstantDescription
SecurityLevelDevelopmentDevelopment (no sensitive filtering, no rate limiting)
SecurityLevelBasicBasic filtering (passwords, tokens, API keys, credit cards, SSNs, phones, SWIFT/CVV, etc. — about 40 common sensitive-data categories)
SecurityLevelStandardStandard filtering (recommended for production)
SecurityLevelStrictStrict filtering (PII / financial-data environments)
SecurityLevelParanoidMaximum filtering (high-risk environments)

Preset Configurations

FunctionDescriptionUse Case
DefaultSecurityConfig()Basic sensitive-data filteringProduction (recommended)
DefaultSecureConfig()Complete sensitive-data filteringHigh-security needs
HealthcareConfig()HIPAA complianceMedical industry
FinancialConfig()PCI-DSS complianceFinancial industry
GovernmentConfig()Government standardPublic sector

Configuration by Level

go
func SecurityConfigForLevel(level SecurityLevel) *SecurityConfig
LevelConstantDescription
DevelopmentSecurityLevelDevelopmentDevelopment, most permissive
BasicSecurityLevelBasicBasic filtering
StandardSecurityLevelStandardStandard filtering
StrictSecurityLevelStrictStrict filtering
ParanoidSecurityLevelParanoidMaximum filtering

Clone

go
func (c *SecurityConfig) Clone() *SecurityConfig

Creates a deep copy of the security configuration.

Usage

Configuring via Config

go
// DefaultConfig already embeds DefaultSecurityConfig(); usually no need to assign explicitly
cfg := dd.DefaultConfig()
logger, _ := dd.New(cfg)

// To replace with a higher security-level config, override explicitly
// cfg.Security = dd.DefaultSecureConfig()

Runtime Modification

go
// Update the security config
logger.SetSecurityConfig(dd.DefaultSecureConfig())

// Read the current config
sec := logger.GetSecurityConfig()

Filtering Nested Structures

go
filter := dd.NewSensitiveDataFilter()

// String filtering
filtered := filter.Filter("password=s3cr3t")
// -> "password=[REDACTED]"

// Nested structures (auto-recursive, supports cycle detection)
data := map[string]any{
    "user": map[string]any{
        "name":     "admin",
        "password": "s3cr3t",
        "token":    "eyJhbGciOi...",
    },
}
filteredData := filter.FilterValueRecursive("data", data)

Monitoring Filter Statistics

go
filter := dd.NewSensitiveDataFilter()
// ... use the filter ...
stats := filter.GetFilterStats()
fmt.Printf("Total filtered: %d, redactions: %d, avg latency: %v\n",
    stats.TotalFiltered, stats.TotalRedactions, stats.AverageLatency)

Next Steps