Skip to content

KaluBekalu/logger

Repository files navigation

Logger README

A simple logging utility for Node.js applications, designed to log messages to various destinations, including a remote API endpoint, console, and a local log file.

Installation

npm install @kalbekalu/logger

Usage

Importing the Logger

import Logger from "./Logger";

Creating an instance of Logger

const logger = new Logger(appInfo);
  • appInfo (any): Additional information about the application (e.g., name, version) to include in each log entry.

Logging to a Remote API Endpoint

logger.logToUrl(level, logData, config);
  • level (string): Log level (TRACE, INFO, DEBUG, WARN, ERROR, FATAL).
  • logData (Record<string, any>): Additional data to be logged - can be any valid record object - Required.
  • config (AxiosRequestConfig, optional): Additional Axios configuration options for the API request.

Logging to Console

logger.standard(level, logData);
  • level (string): Log level (TRACE, INFO, DEBUG, WARN, ERROR, FATAL) - Required.
  • logData (Record<string, any>): Additional data to be logged - can be any valid record object - Required.

Logging to a Local File

logger.file(level, logData, logFilePath);
  • level (string): Log level (TRACE, INFO, DEBUG, WARN, ERROR, FATAL) - Required.
  • logData (Record<string, any>): Additional data to be logged - can be any valid record object - Required.
  • logFilePath (string, optional, default: "./app.log"): Path to the local log file - Required.

Example

const logger = new Logger({
  appName: "MyApp",
  appVersion: "1.0.0",
});

logger
  .logToUrl("INFO", { message: "Application started" })
  .then((response) => console.log("Log sent successfully"))
  .catch((error) => console.error("Error sending log:", error));

logger.standard("DEBUG", { message: "Debugging information" });

logger.file("ERROR", { message: "An error occurred" }, "./errors.log");

In this example, logs are sent to a remote API endpoint, printed to the console, and appended to a local log file.

License

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

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published