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

@tab-square/audit-logger

v1.0.1

Published

The Audit Log library provides functionality to send log messages to a Pub/Sub topic for auditing purposes. It is designed to be used in Node.js applications.

Downloads

2

Readme

Audit Log Library

The Audit Log library provides functionality to send log messages to a Pub/Sub topic for auditing purposes. It is designed to be used in Node.js applications.

Installation

Install the Audit Log library using npm:

npm install @tab-square/audit-logger

Usage

Import the AuditLog class and initialize it with the configuration options. Then, use the log method to send log messages.

// Import the AuditLog class
const { AuditLog } = require('@tab-square/audit-logger');

// Initialize the AuditLog with configuration
const config = {
  keyFilename: './service-account-key.json',
  topic: 'audit-log',
  environment: 'development',
  service: 'smartweb-backend',
  platform: 'smartweb',
};

const logger = AuditLog.init(config);

// Create a log message
const logMessage = {
  merchantKey: 'HQ-01-01',
  action: 'create',
  resourceName: 'save-order-type',
  resourceId: '1234',
  previousValues: null,
  updatedValues: { orderTypeId: 1 },
  userId: '1',
  clientIp: '127.0.0.1',
  metadata: { orderTypeId: 1 },
};

// Send the log message
logger.log(logMessage);

Configuration

The AuditLog class requires a configuration object with the following properties:

  • keyFilename (string): The path to the service account key file. Example: "./service-account-key.json".
  • topic (string): The name of the Pub/Sub topic to publish log messages to. Example: "audit-log".
  • environment (string): The environment that the service is running in. Valid values: "development", "staging", "uat", "production".
  • service (string): The name of the service that is publishing the logs. Example: "my-service".
  • platform (string): The platform that the service is running on. Example: "smartweb".

Log Message

The log message passed to the log method should be an object conforming to the LogData interface. It can include the following properties:

  • merchantKey (string): The merchant key of the merchant that the log is for.
  • action (string): The action that was performed. Example: "create", "update", "delete".
  • resourceName (string): The resource that was acted upon. Example: "user", "merchant", "order".
  • resourceId (string): The ID of the resource that was acted upon.
  • previousValues (any, optional): The previous values of the resource that was acted upon.
  • updatedValues (any, optional): The updated values of the resource that was acted upon.
  • userId (string, optional): The ID of the user that performed the action.
  • clientIp (string, optional): The IP address of the user that performed the action.
  • metadata (any, optional): Additional metadata associated with the log.

Example

Here's an example of how to use the Audit Log library:

const { AuditLog } = require('@tab-square/audit-logger');

// Initialize the AuditLog with configuration
const config = {
  keyFilename: './service-account-key.json',
  topic: 'audit-log',
  environment: 'development',
  service: 'my-service',
  platform: 'platform-A',
};

const logger = AuditLog.init(config);

// Create a log message
const logMessage = {
  merchantKey: 'HQ-01-01',
  action: 'create',
  resourceName: 'save-order-type',
  resourceId: '1234',
  previousValues: null,
  updatedValues: { orderTypeId: 1 },
  userId: '1',
  clientIp: '127.0.0.1',
  metadata: { orderTypeId: 1 },
};

// Send the log message
logger.log(logMessage);

Using Typescript:

import { AuditLog, AuditLogConfig, LogData } from '@tab-square/audit-logger';

class LoggerAudit {
  private logger: AuditLog;
  
  constructor() {
    this.logger = AuditLog.init({
      keyFilename: './service-account-key.json',
      topic: 'audit-loging',
      environment: 'development',
      service: 'my-service',
      platform: 'platform-A',
    } as AuditLogConfig);
  }

  public log(data: LogData) {
    this.logger.log(data);
  }
}

const logger = new LoggerAudit();
logger.log({
  merchantKey: "HQ-01-01",
  action: "create",
  resourceName: "testing-log",
  resourceId: "1234",
  previousValues: null,
  updatedValues: true,
  userId: "1",
  clientIp: "127.0.0.1",
  metadata: {
    message: "Testing purpose only",
  }
});

Note

  • Make sure to replace './service-account-key.json' with the actual path to your service account key file.
  • Adjust the configuration values (topic, environment, service, platform) and the log message properties according to your specific requirements.
  • The package already implements the singleton pattern by default, making it work automatically within this package. If you use it according to the example above, it will work just fine. Alternatively, if you want to use your own variation of the singleton pattern, that's fine.

That's it! You can now use the Audit Log library to send log messages to a Pub/Sub topic for auditing purposes in your Node.js applications.