Skip to main content

Get All Logs

Retrieve all system logs including user access, changes, and errors.
Admin Only: This endpoint requires admin role. Only users with role_key = 'admin' can access system logs.
curl -X GET https://your-api.com/api/logs \
  -H "Authorization: Bearer YOUR_TOKEN"

Authentication

Authorization
string
required
Bearer token from login response

Response

success
boolean
Request success status
logs
object
Combined logs from all logging tables
Response Example
{
  "success": true,
  "logs": {
    "access_logs": [
      {
        "access_id": 12345,
        "user_id": 15,
        "username": "jsmith",
        "email": "[email protected]",
        "ip_address": "192.168.1.100",
        "user_agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64)...",
        "login_timestamp": "2026-03-03T08:30:00Z",
        "logout_timestamp": "2026-03-03T17:45:00Z",
        "session_duration_minutes": 555
      }
    ],
    "user_deactivation_logs": [
      {
        "log_id": 5,
        "user_id": 20,
        "deactivated_by": 1,
        "reason": "Employee left the company",
        "deactivated_at": "2026-03-01T14:30:00Z"
      }
    ],
    "permission_change_logs": [
      {
        "log_id": 123,
        "user_id": 15,
        "module_id": 5,
        "changed_by": 1,
        "action": "granted",
        "old_permissions": {"can_view": false, "can_edit": false},
        "new_permissions": {"can_view": true, "can_edit": true},
        "changed_at": "2026-03-02T10:15:00Z"
      }
    ],
    "whatsapp_webhook_logs": [
      {
        "log_id": 789,
        "event_type": "incoming",
        "payload": {
          "object": "whatsapp_business_account",
          "entry": [...]
        },
        "processed": true,
        "error": null,
        "created_at": "2026-03-03T09:20:00Z"
      }
    ],
    "email_logs": [
      {
        "mail_id": 456,
        "recipients": ["[email protected]"],
        "subject": "Service Update",
        "status": "sent",
        "sent_by": 1,
        "created_at": "2026-03-03T11:00:00Z"
      }
    ],
    "error_logs": [
      {
        "error_id": 55,
        "error_type": "database_error",
        "error_message": "Connection timeout",
        "stack_trace": "Error: timeout\n  at ...",
        "user_id": 15,
        "request_path": "/api/requests",
        "created_at": "2026-03-03T12:30:00Z"
      }
    ]
  }
}

Log Types

access_logs
array
User login/logout events
  • User identification
  • IP addresses and user agents
  • Session duration
  • Login and logout timestamps
user_deactivation_logs
array
User deactivation events
  • Deactivated user ID
  • Admin who performed deactivation
  • Reason for deactivation
  • Timestamp
permission_change_logs
array
Permission modifications
  • User and module affected
  • Admin who made the change
  • Old and new permission values
  • Action type (granted/revoked/modified)
whatsapp_webhook_logs
array
WhatsApp webhook events
  • Event type (incoming/outgoing)
  • Full webhook payload
  • Processing status
  • Errors if any
email_logs
array
Email send history
  • Recipients list
  • Subject and status
  • Sender user
  • Timestamps
error_logs
array
System errors and exceptions
  • Error type and message
  • Stack trace
  • User who triggered error
  • Request path

Error Responses

Unauthorized (401)
{
  "success": false,
  "error": "Token requerido"
}
Forbidden (403)
{
  "success": false,
  "error": "Solo los administradores pueden ver los logs"
}

Log Data Structure

The fn_get_all_logs() database function aggregates logs from multiple tables:

Access Log Fields

  • access_id: Unique identifier
  • user_id: User who logged in
  • ip_address: Client IP address
  • user_agent: Browser/client information
  • login_timestamp: When user logged in
  • logout_timestamp: When user logged out (null if still active)
  • session_duration_minutes: Calculated session length

Permission Change Log Fields

  • log_id: Unique identifier
  • user_id: User whose permissions changed
  • module_id: Affected module
  • changed_by: Admin who made the change
  • action: Type of change (granted, revoked, modified)
  • old_permissions: Permission state before change
  • new_permissions: Permission state after change
  • changed_at: Timestamp of change

WhatsApp Webhook Log Fields

  • log_id: Unique identifier
  • event_type: incoming or status_update
  • payload: Full webhook JSON payload
  • processed: Boolean indicating if webhook was successfully processed
  • error: Error message if processing failed
  • created_at: When webhook was received

Security Considerations

Sensitive Data: Logs may contain sensitive information including:
  • User IP addresses and session data
  • Permission changes and security events
  • Full webhook payloads (may include phone numbers)
  • Error stack traces (may reveal system architecture)
Access should be strictly limited to administrators only.

Role Check Implementation

The endpoint verifies admin role using:
SELECT 1
FROM db_ambiotec.users u
LEFT JOIN db_ambiotec.user_roles ur ON ur.user_id = u.user_id
LEFT JOIN db_ambiotec.roles r ON r.role_id = ur.role_id
WHERE u.user_id = $1
  AND r.role_key = 'admin'
LIMIT 1
If no matching record is found, the request is rejected with 403 Forbidden.

Use Cases

Audit Trail

Track all user activities, permission changes, and administrative actions for compliance and security audits.

Security Monitoring

Monitor login patterns, failed authentication attempts, and suspicious activities.

Debugging

Investigate errors and webhook processing issues using detailed logs.

Analytics

Analyze user session durations, email delivery rates, and system usage patterns.

Build docs developers (and LLMs) love