1.0.0 • Published 2 months ago

prosole v1.0.0

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

Prosole

npm version License Prosole is an all-in-one logger and alerts utility designed for Node.js applications. It simplifies the process of logging information and generating alerts, making it an essential tool for developers looking to monitor, manage, and debug their Node.js projects.

Features

Logger Utility

  • Transport Configuration:
    • Specify one of three supported transports: console, file, or Socket.IO.
    • Logs are directed to the specified transport, allowing customization based on different environments.
  • Custom Base Log Level:
    • Set a base log level to filter log messages, allowing you to ignore messages below this level.
    • Log levels range from "Log" (lowest) to "Error" (highest).
  • Selective Logging:
    • Enable or disable logging by specifying a transport; no logs will be printed if no transport is specified.
    • This feature provides flexibility in managing logging for different scenarios and environments.
  • Console Integration:
    • Optionally extend the Node.js console object to seamlessly integrate logging features directly into it.
    • Note that this feature is turned off by default and may not be recommended for all use cases.
  • Template Customization:
    • Define custom log message templates with support for variable interpolation, allowing for flexible formatting of log messages based on predefined patterns and values.

Alert Utility

  • Transport Configuration:
    • Choose from multiple alert transports, including Discord, Slack, or custom HTTP requests.
    • Set up different transports for different environments.
  • Selective Alerting:
    • Define alerting behavior by specifying a transport; no alerts will be sent if no transport is specified.
    • This feature allows you to adapt alerting to your specific requirements and scenarios.
  • Integration with Third-Party Platforms:
    • Send alert messages to third-party platforms like Slack for immediate notification.
    • Also, send HTTP requests to a custom server to facilitate alerting.
  • Console Integration:
    • Optionally extend the Node.js console object to seamlessly integrate alerting features directly into it.
    • Note that this feature is turned off by default and may not be suitable for all use cases.

You can easily install Prosole via npm or yarn to start using it in your Node.js projects. Choose one of the following methods to get started:

Installation

Using npm

npm install prosole --save

Using yarn

yarn add prosole

Basic Usage

Getting started with Prosole is quick and straightforward. Follow these steps to integrate the Logger and Alert Utility into your Node.js project:

Importing the Package

You can import the Prosole package in your Node.js application using either ES6 import or CommonJS require syntax:

Using ES6 Import:

import { logger, alerts } from "prosole";

Using CommonJS Require:

const { logger, alerts } = require("prosole");

Initializing the Logger

To begin using the logger, initialize it with your project details, preferred transport, and other configuration options:

logger.init({
  project: {
    name: "YourProjectName",
    version: "1.0.0", // will default to 1.0.0 if not specified
  },
  transport: "console", // Choose your preferred transport (e.g., "console", "file", "Socket.IO" or null to bypass)
  env: "development", // Set the environment context
});

Logger Configuration Options

OptionDescriptionTypeDefault ValueAccepted Values
projectAn object containing information about the project, such as name and version.ProjectN/AN/A
transportSpecifies the transport mechanism for logging: console, file, or Socket.IO.string | nullnull'console', 'file', 'Socket.IO'
baseLevelSets the base log level to filter log messages. Log levels range from "Log" (lowest) to "Error" (highest).stringN/A'Log', 'Info', 'Warn', 'Error'
envSpecifies the environment in which the logger is running.stringN/AAny string value
extendConsoleOptionally extends the Node.js console object to integrate logging features directly into it.booleanfalsetrue, false
templateDefines a template or formatting function for log message output, allowing for custom formatting.TemplateN/AN/A
fileConfiguration options for file-based logging, including the directory and optional prefix.LogsFileN/AN/A
socketIOConfiguration options for Socket.IO-based logging, including the base URL, optional headers, and events.LogsSocketION/AN/A

Examples

Template Configuration

To configure a custom template for log messages, you can use the following syntax:

const loggerConfig = {
  template: "{{project}} - {{timestamp}} [{{level}}]",
};

Available variables for the template:

  • timestamp: The timestamp when the log message is generated.
  • level: The log level (e.g., 'Info', 'Warn', 'Error').
  • project: The name and version of the project (e.g., "MyProject:1.0").

File Transport Configuration

To configure file-based logging, specify the directory where logs will be stored:

const loggerConfig = {
  transport: "file",
  file: {
    directory: "/var/log/myapp",
    prefix: "myapp",
  },
};

Socket.IO Transport Configuration

To configure Socket.IO-based logging, specify the base URL and optional headers and the events to be used:

const loggerConfig = {
  transport: "Socket.IO",
  socketIO: {
    baseUrl: "http://example.com/logs",
    headers: {
      Authorization: "Bearer YOUR_ACCESS_TOKEN",
    },
    events: {
      log: "log",
      error: "error",
      ...<more events>,
    },
  },
};

Initializing Alerts

To set up alerting, initialize it with your project details, preferred alert transport (e.g., Discord), and other relevant options:

alerts.init({
  project: {
    name: "YourProjectName",
    version: "1.0.0",
  },
  transport: "slack", // Choose your preferred alert transport ("slack", "http" or null to bypass )
  environment: "development", // Set the environment context
  slack: {
    channels: {
      "channel-name":<webhook url>
    },
  },
});

Alert Configuration Options

OptionDescriptionTypeDefault ValueAccepted Values
projectAn object containing information about the project, such as name and version.ProjectN/AN/A
environmentSpecifies the environment in which alerts are triggered.stringN/AAny string value
transportSpecifies the transport mechanism for sending alerts: slack or HTTP.string | nullnull'slack', 'discord', 'http'
extendConsoleOptionally extends the Node.js console object to integrate alerting features directly into it.booleanfalsetrue, false
slackConfiguration options for Slack alerts, including channels to send alerts to.SlackAlertN/AN/A
httpConfiguration options for HTTP alerts, including the alert URL, headers, and health check settings.ApiAlertN/AN/A

Examples

Slack Alert Configuration

To configure Slack alerts, specify the channels to send alerts to:

const alertConfig = {
  transport: "slack",
  slack: {
    channels: {
      "#channel1": "webhook_url1",
      "#channel2": "webhook_url2",
    },
  },
};

HTTP Alert Configuration

To configure HTTP alerts, specify the alert URL and optional headers:

const alertConfig = {
  transport: "http",
  http: {
    alertUrl: "http://example.com/alerts",
    headers: {
      Authorization: "Bearer YOUR_ACCESS_TOKEN",
    },
    healthCheck: {
      path: "/health",
      method: "GET",
    },
    body: (type, project, environment, alert) => {
      return {
        type,
        project,
        environment,
        alert,
      };
    },
  },
};

Getting the Slack Webhook URL

To send alerts to Slack, you need to provide a webhook URL. You can get this URL by using the "incoming webhook" feature in Slack. Click here to see how to set up an incoming webhook in Slack.

Actual Usage

Now that you have initialized the Logger and Alert Utility, you can start using them in your code. Here's how you can log an error message and send an alert message:

Logging an Error Message

logger.error("This is an error",...<more data>);

Sending an alert using Slack

alerts.error("This is an error alert", "<slack-channel>");

Sending an alert using HTTP

alerts.error("This is an error alert");

These simple examples demonstrate how to use Prosole for logging and alerting in your Node.js application. Customize the log and alert messages, as well as the destinations, to meet your specific needs.

For more detailed usage examples and additional features, please refer to the documentation or code samples provided in the repository.

License

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

Support

If you have any questions or issues, please open an issue on the GitHub repository.

Author

1.0.0

2 months ago

0.1.1

5 months ago

0.1.0

6 months ago

0.0.2

7 months ago

0.0.1

7 months ago