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

file-error-logging

v1.2.2

Published

Log your errors directly to the console and to a .log file

Downloads

51

Readme

file-error-logging

A lightweight, flexible logging library for Node.js applications. This library provides an intuitive API for managing log levels, formatting logs, and writing logs to files. It includes a Singleton-based Logger class with out-of-the-box support for logging info, warn, error, and verbose levels.

Future updates will introduce a CLI for easier configuration and use.

Features

  • Default Log Levels: info, warn, error, verb (verbose).
  • File-based Logging: Logs are written to individual files for each log level.
  • Log Rotation: Save logs on folders depending on the day, month or year.
  • Dynamic Configuration: Add custom log levels with their own color, timestamping, and file logging rules.
  • Extensive Configuration: Change the behavior of the logger by passing an options object.
  • Singleton Pattern: Ensures a single instance of the logger throughout the application.

Installation

To install the package, use npm:

npm install file-error-logging

Usage

Basic Example

import Logger from "file-error-logging";

// Configure the logger
Logger.setConfig({
  development: false,
  rotation: "monthly",
});

// Log messages with different levels
Logger.log("info", "This is an info message", {
  includeTimestampInConsole: true,
  logToFile: false,
  color: "whiteBright",
});
Logger.log("warn", "This is a warning message", {
  includeTimestampInConsole: false
});
Logger.log("verb", "This is a verbose message");
Logger.log("error", new Error("This is an error message"));

Adding a Custom Log Level

// Add a new custom log level
Logger.addLogLevel("question", {
  color: "cyan",
  logToFile: true,
  includeTimestampInConsole: true,
  logFileName: "questions.log",
});

// Log a message with the custom log level
Logger.log("question", "What is the meaning of life?");

Overriding Default Options

You can override log level configurations on a per-message basis.

Logger.log("info", "Custom timestamp and color", {
  includeTimestampInConsole: true,
  color: "green",
});

Logs Directory

By default, logs are stored in the logs directory at the root of your project. Ensure your application has the necessary permissions to create and write to this directory.

You can change this behavior by changing the logsDirectory option on the setConfig method.

API Reference

Methods

Logger.getInstance()

Returns the singleton instance of the logger.

Logger.log(level: LogLevel, message: string, options?: Object)

Logs a message at the specified log level.

  • level: The log level (info, warn, error, verb, or custom).
  • message: The message to log.
  • options: Optional overrides for includeTimestamp, logToFile, and color.

Logger.addLogLevel(name: string, params: Object)

Adds a new log level with specified configurations.

  • name: The name for the log level.
  • params.color: The color used for console output.
  • params.includeTimestampInConsole: Whether to include timestamps in the logs.
  • params.logToFile: Whether to log messages to a file.
  • params.logFileName: File name for the logs (optional).

Logger.setConfig(params: Object)

Sets the configuration to be used by the Logger through the entire project.

  • params.logsDir: Changes the directory in which the logs will be stored (optional).
  • params.rotation: Sets the rotation period for logs, splitting them by date (optional).
  • params.development: Avoids sending logs to console if set to false (optional).

Contributing

Contributions are welcome! Please submit a pull request or file an issue on the GitHub repository.

License

This project is licensed under the Apache-v2.0 License. See the LICENSE file for details.


Happy Logging!