Authorization Logging
Track and monitor authorization attempts with the auth-log package.
Authorization Logging For Audit Trail
The @repo/auth-log
package provides a simple yet powerful way to log and monitor authorization attempts in your application. It integrates seamlessly with the authorization packages to create an audit trail of access control decisions.
Overview
Authorization logging is a critical component of security monitoring and compliance. The @repo/auth-log
package allows you to:
- Track authorization decisions: Log whether access was granted or denied
- Record contextual information: Capture user, resource, and action details
- Customize logging destinations: Output to console or file
- Support compliance requirements: Maintain audit trails for regulatory needs
Installation
The package is included by default in the zopio
stack. If you need to install it separately:
Basic Usage
The auth-log package is designed to work seamlessly with the authorization packages:
Configuration
By default, logs are output to the console. You can configure the logging destination by setting the AUTH_LOG_TARGET
environment variable:
When logging to a file, entries are appended to ./logs/access.log
by default.
When using BetterStack, you’ll need to provide your source token via the BETTERSTACK_SOURCE_TOKEN
environment variable.
Log Format
Each log entry contains the following information:
timestamp
: When the authorization attempt occurredresource
: The resource being accessed (e.g., ‘article’, ‘user’)action
: The action being performed (e.g., ‘read’, ‘update’)context
: User context information (userId, role, tenantId)recordId
: Optional ID of the specific record being accessedfield
: Optional field name for field-level permissionscan
: Boolean indicating whether access was grantedreason
: Optional explanation for why access was denied
Integration with Authorization Packages
The auth-log package is automatically integrated with the auth-runner package, which means all authorization checks performed through the runner will be logged without additional configuration.
Available Adapters
Console Logger
The default adapter that outputs logs to the console:
File Logger
Writes logs to a file on disk:
BetterStack Logger
Sends logs to BetterStack’s logging service for centralized log management:
The BetterStack adapter enriches logs with additional metadata:
- Sets log level to “info” for allowed access and “warn” for denied access
- Creates a formatted message for better readability
- Adds a service name for easier filtering
Custom Adapters
You can create custom logging adapters by implementing the logger interface:
Best Practices
- Log all authorization attempts: Enable comprehensive logging for security audits
- Monitor denied access: Pay special attention to denied access attempts
- Rotate log files: Implement log rotation for file-based logging
- Protect sensitive information: Be careful not to log sensitive user data
- Review logs regularly: Set up a process to review authorization logs