Overview
Salt’s audit logging system provides comprehensive tracking of user actions with support for custom metadata, actor extraction, and multiple storage backends including PostgreSQL.Core Concepts
- Actor: The user or service performing the action
- Action: A string identifier for the operation being performed
- Data: Arbitrary data associated with the action
- Metadata: Additional context information (e.g., IP address, user agent)
- Timestamp: When the action occurred
Service
Creating an Audit Service
opts- Variable number ofAuditOptionfunctions to configure the service
*Service instance
Example: Basic Setup
Logging Actions
Log Method
ctx- Context containing actor and metadata informationaction- String identifier for the action (e.g., “user.login”, “resource.delete”)data- Any data to associate with the action
Example: Logging User Actions
Context Management
WithActor
Adds actor information to the context.ctx- Parent contextactor- Actor identifier (e.g., user ID, service name)
WithMetadata
Adds or appends metadata to the context.ctx- Parent contextmd- Metadata map to add
Example: Adding Context Information
Configuration Options
WithRepository
Configures the storage backend for audit logs.WithMetadataExtractor
Configures automatic metadata extraction from context.WithActorExtractor
Configures custom actor extraction logic.Data Models
Log Structure
Fromaudit/model.go:5-11:
Example Log Entry
PostgreSQL Repository
NewPostgresRepository
db- PostgreSQL database connection
*PostgresRepository instance
Database Schema
Fromrepositories/postgres.go:36-48:
Example: Full PostgreSQL Setup
Advanced Usage
Middleware Pattern
Bulk Logging Pattern
Action Naming Conventions
Follow a consistent naming pattern for actions:Querying Audit Logs
Best Practices
- Consistent Action Names: Use a hierarchical naming scheme (e.g.,
resource.action) - Include Context: Always add relevant metadata for debugging and compliance
- Async Logging: Consider async logging for high-throughput applications
- Data Retention: Implement log retention policies based on compliance requirements
- PII Handling: Be careful not to log sensitive personal information
- Error Handling: Log audit failures separately to ensure visibility
Performance Considerations
- Indexing: The PostgreSQL repository creates indexes on
timestamp,action, andactor - Batch Writes: Consider batching writes for high-volume scenarios
- Partitioning: Use table partitioning for large audit log tables
- Archiving: Implement archiving strategies for historical logs
References
- Source:
~/workspace/source/auth/audit/audit.go - Model:
~/workspace/source/auth/audit/model.go - PostgreSQL Repository:
~/workspace/source/auth/audit/repositories/postgres.go