1.0.4 • Published 5 months ago

@kamal-02/audit-logger v1.0.4

Weekly downloads
-
License
MIT
Repository
github
Last release
5 months ago

@kamal-02/audit-logger

A lightweight, secure and very simple audit logging package for Node.js applications, designed to send structured audit logs to Grafana Loki.

Features

  • Simple integration with Winston and Loki
  • Standardized audit log format for user actions
  • Configurable logging environment and service name

Installation

npm install @kamal-02/audit-logger

Setup

  1. First, set up your environment variables in a .env file (recommended):
LOKI_USERNAME=your_loki_username
LOKI_API_KEY=your_loki_api_key
LOKI_HOST=https://your-loki-instance.grafana.net
  1. Initialize the logger in your application:
const { initAuditLogger, logUserAction } = require('@kamal-02/audit-logger');

initAuditLogger({
  service: 'my-service',
  environment: 'production',
  lokiHost: 'https://logs-prod-XXX.grafana.net' || process.env.LOKI_HOST,
  user: 'your_loki_username' || process.env.LOKI_USERNAME,
  apikey: 'your_loki_api_key' || process.env.LOKI_API_KEY,
  labels: { job: 'my-audit-logs' }
});

Usage

// Log a user action
logUserAction({
  actor: {
    id: 'user123',
    type: 'user',
    name: 'John Doe'
  },
  action: 'DOCUMENT_UPDATE',
  target: {
    id: 'doc456',
    type: 'document',
    name: 'Important Contract'
  },
  source: {
    ip: '192.168.1.1',
    userAgent: 'Mozilla/5.0...'
  },
  metadata: {
    changes: ['title', 'content'],
    previousState: 'DRAFT',
    newState: 'PUBLISHED'
  }
});

API Reference

initAuditLogger(options)

Initializes the audit logger with the provided options.

ParameterTypeDescriptionRequiredDefault
servicestringService nameNo'unknown-service'
environmentstringEnvironment nameNo'development'
lokiHoststringGrafana Loki host URLYesNone
userstringLoki usernameYesNone
apikeystringLoki API keyYesNone
labelsobjectAdditional Loki labelsNo{ job: 'audit-log' }

logUserAction(logData)

Logs a user action to Loki.

ParameterTypeDescriptionRequired
actorobject/stringThe user or system performing the actionYes
actionstringThe action being performedYes
targetobject/stringThe resource being acted uponYes
sourceobjectSource information (IP, user agent)No
timestampstringISO timestampNo
metadataobjectAdditional contextual informationNo

License

MIT

1.0.4

5 months ago

1.0.3

5 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.0

6 months ago