2.0.0 • Published 2 days ago

als-logger v2.0.0

Weekly downloads
-
License
ISC
Repository
-
Last release
2 days ago

ALS Logger (Version 2.0)

als-logger is a flexible and powerful logging utility for Node.js applications. It supports customizable log formats, file management, error handling, and much more.

Features

  • Customizable Logging Levels: Easily log different message types like log, warn, and error.
  • File Management: Automatically rotates log files and removes old entries based on age.
  • Error Handling: Captures uncaught exceptions and unhandled promise rejections.
  • Retry Mechanism: Retries log writes with configurable attempts and wait times.

Installation

Install the package via npm:

npm install als-logger

Usage

Basic Example

Create an instance of the Logger and start logging messages.

const Logger = require('als-logger');

const logger = new Logger('/path/to/logs', { maxdays: 5, dev: true });

// Log messages of various types
logger.log('This is a log message.');
logger.warn('This is a warning.');
logger.error('This is an error.');

Capturing Uncaught Exceptions

Enable automatic logging of uncaught exceptions and unhandled promise rejections by setting the exceptions option to true.

const logger = new Logger('/path/to/logs', { exceptions: true });

Retrieve Logs

Use the logs method to retrieve and sort log entries.

// Retrieve the last 10 logs in ascending order
const { reports, errors } = await logger.logs(10, true);

console.log(reports);
if (errors.length > 0) console.error(errors);

Configuration Options

  • dirPath (string): Path to the directory where log files will be stored. Defaults to __dirname.
  • maxdays (number): The maximum number of days to retain logs. Older logs will be automatically removed. Defaults to 0 (no removal).
  • attempts (number): Number of attempts for retrying a failed log write. Defaults to 3.
  • waitTime (number): Time in milliseconds between retry attempts. Defaults to 1000.
  • timeout (number): Maximum time in milliseconds for writing a log entry. Defaults to 5000.
  • exceptions (boolean): Whether to capture and log uncaught exceptions and unhandled promise rejections. Defaults to false.
  • dev (boolean): If true, outputs log messages to the console. Defaults to false.
  • loggerError (function): A custom error handler function for logging errors.
2.0.0

2 days ago

1.0.0

5 months ago

0.5.0

8 months ago

0.6.0

8 months ago

0.2.1

11 months ago

0.2.0

11 months ago

0.1.1

11 months ago

0.1.0

11 months ago