npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

audit-pro

v1.1.1

Published

Advanced audit logging and trail package supporting multiple databases with dynamic columns, log levels, and customizable console logs.

Downloads

212

Readme

Audit Pro

Audit Pro is an advanced audit logging and trail package for Node.js applications that supports multiple databases with dynamic columns, log levels, and customizable console logs. It enables developers to log, retrieve, and manage audit events easily and efficiently.

Table of Contents

Installation

To install the audit-pro package, use npm:

npm install audit-pro

Storage Dependencies

Depending on which storage type you plan to use, you may need to install additional dependencies:

  • Sequelize (for SQL databases): If you intend to use Sequelize for SQL-based storage (e.g., MySQL, PostgreSQL), you must install sequelize and a corresponding SQL driver (e.g., mysql2, pg).

    npm install sequelize 
  • MongoDB: If you are using MongoDB as your storage, install the mongodb package.

    npm install mongodb

Getting Started

To start using Audit Pro, you need to create instances of the AuditLogger, the storage implementations (like SequelizeStorage or FileStorage), and the ConsoleLogger.

Basic Setup

import { SequelizeStorage, LogLevel, AuditLog } from 'audit-pro';
import { DataTypes, Model, ModelAttributes, Sequelize } from 'sequelize';


// Setup your database connection
const sequelizeInstance = new Sequelize('mysql://user:password@localhost:3306/audit', {
  logging: false,
});

// Create storage and logger instances
const storage = new SequelizeStorage(sequelizeInstance, 'AuditLogs', {});
const logger = new ConsoleLogger(true); // true enables console logging

// Create an AuditLogger instance
const auditLogger = new AuditLogger(storage, logger);

OR

Using Storage Instance to Log Events
// Define dynamic columns (if any)
const dynamicColumns: Partial<ModelAttributes<Model<AuditLog>, AuditLog>> = {
    additionalInfo: {
        type: DataTypes.STRING,
        allowNull: true,
    },
};
// Create the storage instance with the necessary table name and dynamic columns
const storage = new SequelizeStorage(sequelizeInstance, 'AuditLogs', dynamicColumns);

Usage

Using Storage Instance to Log Events

You can also directly use the storage instance to log events. This is particularly useful if you want to log without going through the AuditLogger instance.

// Create the storage instance with the necessary table name and dynamic columns
const storage = new SequelizeStorage(sequelizeInstance, 'AuditLogs', dynamicColumns);

// Example of logging an event
const logEvent: AuditLog = {
    id: 'unique-log-id',
    userId: 'user123',
    action: 'User Login',
    logLevel: LogLevel.INFO,
    timestamp: new Date(),
    metadata: { ipAddress: '192.168.1.1' },
    additionalInfo: 'extra information'
    // more custom fields on the dynamic column
}; 

// Log the event directly using the storage instance
await storage.logEvent(logEvent);

Logging Events

To log an event, use the logEvent method. The method accepts an AuditLog object.

const log: AuditLog = {
  id: 'unique-log-id',
  userId: 'user123',
  action: 'User Login',
  logLevel: LogLevel.INFO,
  timestamp: new Date(),
  metadata: { ipAddress: '192.168.1.1' },
  //any additional fields
};

// Log the event
await auditLogger.logEvent(log);

Fetching Logs

You can retrieve logs with specific filters using the fetchLogs method.

const logs = await auditLogger.fetchLogs({ userId: 'user123' });
console.log(logs);

Updating Logs

To update an existing log, use the updateLog method.

await auditLogger.updateLog('unique-log-id', { action: 'Updated Action' });

Deleting Logs

You can delete logs by using the deleteLog method.

await auditLogger.deleteLog('unique-log-id');

Counting Logs

To count logs based on specific filters, use the countLogs method.

const count = await auditLogger.countLogs({ userId: 'user123' });
console.log(`Total logs for user123: ${count}`);

API Documentation

AuditLogger

The AuditLogger class is the main entry point for logging events.

Constructor

new AuditLogger(storage: StorageInterface, logger: ConsoleLogger)
  • storage: An instance of a storage implementation (e.g., SequelizeStorage, FileStorage).
  • logger: An instance of ConsoleLogger for logging to the console.

Methods

  • logEvent(log: AuditLog): Promise<void>
  • fetchLogs(filter: object): Promise<AuditLog[]>
  • fetchLog(filter: object): Promise<AuditLog>
  • updateLog(id: string, updates: object): Promise<void>
  • deleteLog(id: string): Promise<void>
  • countLogs(filter: object): Promise<number>

Storage Interfaces

Implement your storage interface by extending StorageInterface. The main methods to implement are:

  • logEvent(log: AuditLog): Promise<void>
  • fetchLogs(filter: object): Promise<AuditLog[]>
  • updateLog(id: string, updates: object): Promise<void>
  • deleteLog(id: string): Promise<void>
  • countLogs(filter: object): Promise<number>

ConsoleLogger

The ConsoleLogger class is used for logging events to the console.

Constructor

new ConsoleLogger(isEnabled: boolean)
  • isEnabled: Boolean indicating whether console logging is enabled.

Methods

  • getIsEnabled(): boolean
  • logEventToConsole(log: AuditLog): void
  • colorize(message: string, color: Color): string

Available Log Levels

  • LogLevel.INFO
  • LogLevel.WARN
  • LogLevel.ERROR
  • LogLevel.DEBUG
  • LogLevel.CUSTOM

Viewing Logs

You can use the LogViewer class to fetch and display logs from any storage implementation. Here’s an example:

import { LogViewer, ConsoleLogger } from 'audit-pro';

const consoleLogger = new ConsoleLogger(true); // Enable colorized console logging

// Assuming you have configured one or more storages
const logViewer = new LogViewer([storage], consoleLogger);

// View logs based on filters (e.g., userId)
logViewer.viewLogs({ userId: 'user123' });

Customizing Console Logs

Audit-Pro allows you to customize the console output with different colors:

import { ConsoleLogger } from 'audit-pro';

// Enable colorized logs
const consoleLogger = new ConsoleLogger(true);

// Log events to the console
consoleLogger.logEventToConsole({
    id: 'unique-log-id',
    userId: 'user123',
    action: 'User Login',
    logLevel: LogLevel.INFO,
    timestamp: new Date(),
    metadata: { ipAddress: '192.168.1.1' },
});

Examples

Here’s a simple example of how to use Audit Pro in an Express.js application.

import express from 'express';
import { AuditLogger, ConsoleLogger, SequelizeStorage } from 'audit-pro';
import { Sequelize } from 'sequelize';

const app = express();
const sequelizeInstance = new Sequelize('mysql://user:password@localhost:3306/audit', {
  logging: false,
});
const storage = new SequelizeStorage(sequelizeInstance, 'AuditLogs', {});
const logger = new ConsoleLogger(true);
const auditLogger = new AuditLogger(storage, logger);

app.post('/login', async (req, res) => {
  // Simulating a login action
  const log: AuditLog = {
    id: 'unique-log-id',
    userId: req.body.userId,
    action: 'User Login',
    logLevel: LogLevel.INFO,
    timestamp: new Date(),
    metadata: { ipAddress: req.ip },
  };

  await auditLogger.logEvent(log);
  res.send('Login logged!');
});

app.listen(3000, () => {
  console.log('Server is running on port 3000');
});

Testing

To run the tests for Audit Pro, ensure you have Jest installed. Run the following command:

npm test

License

This project is licensed under the MIT License - see the LICENSE file for details.