Audit Logs
Comprehensive audit trail system in TMA Cloud.
Overview
Queue-based audit logging system using PostgreSQL and pg-boss. Tracks all critical user actions and system events asynchronously.
Configuration
Environment Variables
AUDIT_WORKER_CONCURRENCY=5 # Concurrent events processed
AUDIT_JOB_TTL_SECONDS=82800 # Job TTL (must be < 24h)
Starting the Audit Worker
npm run worker
Important: Audit events are queued but not written to database until worker processes them. Always keep worker running in production.
Audit Events
Authentication Events
user.signup- User creates accountuser.login- User logs inuser.logout- User logs outuser.login.failed- Failed login attemptauth.logout- Session logoutauth.logout_all- Logout from all devicesauth.suspicious_token- Token fingerprint mismatchauth.session_revoked- Session revoked
File Events
file.upload- File uploadedfile.upload.bulk- Multiple files uploaded in bulkfile.download- File downloadedfile.delete- File moved to trashfile.delete.permanent- File permanently deletedfile.restore- File restored from trashfile.rename- File/folder renamedfile.move- Files/folders movedfile.copy- Files/folders copiedfile.star/file.unstar- File starred/unstarred
Folder Events
folder.create- Folder created
Share Events
share.create- Share link createdshare.delete- Share link removedshare.access- Public access to shared file/folder
Document Events (OnlyOffice)
document.open- Document opened in OnlyOfficedocument.save- Document saved from OnlyOffice
Settings Events
admin.settings.update- Admin setting changed (e.g. signup enabled/disabled)admin.settings.read- Admin read of protected settings
Querying Audit Logs
View User Activity
SELECT event_type, status, metadata, created_at
FROM audit_logs
WHERE user_id = 'user_abc123'
ORDER BY created_at DESC;
View Failed Operations
SELECT event_type, user_id, metadata, created_at
FROM audit_logs
WHERE status = 'failure'
ORDER BY created_at DESC;
View File Operations
SELECT event_type, user_id, metadata->>'fileName' as file_name, created_at
FROM audit_logs
WHERE resource_type = 'file'
AND event_type LIKE 'file.%'
ORDER BY created_at DESC;
Search by Metadata
-- Find operations on specific file
SELECT * FROM audit_logs
WHERE metadata @> '{"fileId": "file_123"}'::jsonb
ORDER BY created_at DESC;
-- Find large file uploads
SELECT user_id, metadata->>'fileName' as file_name,
(metadata->>'fileSize')::bigint as size, created_at
FROM audit_logs
WHERE event_type = 'file.upload'
AND (metadata->>'fileSize')::bigint > 10485760
ORDER BY created_at DESC;
-- Find bulk uploads (e.g. folder uploads)
SELECT user_id,
metadata->>'fileCount' as file_count,
metadata->>'parentId' as parent_id,
created_at
FROM audit_logs
WHERE event_type = 'file.upload.bulk'
ORDER BY created_at DESC;
Audit Worker Management
Monitor Worker
npm run worker
# Logs show: "Audit worker started", "Processing audit event: ..."
Check Queue Status
-- View pending jobs
SELECT * FROM pgboss.job
WHERE name = 'audit-log' AND state = 'created'
ORDER BY createdon DESC;
-- View completed jobs
SELECT * FROM pgboss.job
WHERE name = 'audit-log' AND state = 'completed'
ORDER BY completedon DESC LIMIT 100;
Worker Concurrency
Higher values = faster processing but more database connections. Recommended: 5-10.
Related Topics
- Logging - Application logging
- Monitoring - System monitoring
- Reference: Audit Events - Complete event list