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

debugger-logger

v1.0.1

Published

Basic logging of all console actions and module calls and http(s) requests.

Downloads

1,381

Readme

Debugger Logger

Debugger Logger is a Node.js utility package for comprehensive logging and request interception. It provides in-depth debugging tools for Node.js applications, capturing HTTP/HTTPS requests, logging events, and managing application state through various utility methods.


Table of Contents


Installation

To use this package, clone the repository and install dependencies if required:

git clone https://github.com/overlord-303/debugger
cd debugger
npm install

or install via npm:

npm install debugger-logger@stable

Features

Logging Capabilities

  • File Logging: Saves log messages to the designated log file.
  • Console Logging: Outputs logs to the console.
  • Module Call Logging: Logs when a specific module is loaded, useful for tracing dependency execution.
  • Custom Log Levels: Supports log levels such as info, debug, error, warning, and traceback.

Event Handling

  • Supports adding and removing listeners for custom logging events, including:
    • filelog: Fires on logging events to a file.
    • consolelog: Fires on logging events to the console.
    • modulecall: Fires on logging events related to module loading or compiling.

HTTP Request Interception

  • Intercepts HTTP/HTTPS requests: Captures outgoing HTTP and HTTPS requests, logs their details, and measures request duration.
  • Module Backup and Restoration: Backs up original request functions (http/https) to restore the modules if needed, preventing conflicts or circular dependencies.

Environment Information

Provides a snapshot of environment details, such as platform, architecture, Node.js version, and process ID, as well as a utility for checking execution time.

Custom Error Handling

  • Integrates global error handling for uncaught exceptions and unexpected errors.
  • Allows setting custom shutdown and cleanup actions during application exit.

Usage

Initialization

To initialize the Debugger instance, import it into your main application file:

const Debugger = require('debugger-logger');

The Debugger is a singleton and is automatically instantiated as one.

Basic Logging

Logs are provided in the follow format:

[0000-00-00 00:00:00] level: 'content' +0ms

Use various log methods to track application events:

Debugger.log('Info message');
Debugger.logDebug('Debug message');
Debugger.logError('Error message');

Event Listeners

Add or remove event listeners to track specific events:

function log(filePath, content) {
   console.log(`Logged to file: ${filePath}, with message: ${content}.`);
}

Debugger.on('filelog', log);
Debugger.off(Debugger.EVENTS.FILELOG, log);

Intercepting HTTP Requests

Intercepted requests are logged automatically, capturing details such as:

  • Request method
  • Path and headers
  • Status code of the response
  • Duration of the request

Restoring Original Modules

If you need to revert back to the original HTTP/HTTPS request functionality:

Debugger.restore('http');
Debugger.restore('https');

Getting environment snapshot

Debugger.getData();

will return an object with the following structure:

{
  env: {
    name: string,
    version: string,
    platform: NodeJS.Platform,
    architecture: NodeJS.Architecture,
    nodePath: string,
    pid: number
  },
  memoryUsage: {
    rss: {
      bytes: number,
      kilobytes: number,
      megabytes: number
    },
    heapTotal: {
      bytes: number,
      kilobytes: number,
      megabytes: number
    },
    heapUsed: {
      bytes: number,
      kilobytes: number,
      megabytes: number
    },
    external: {
      bytes: number,
      kilobytes: number,
      megabytes: number
    },
    arrayBuffers: {
      bytes: number,
      kilobytes: number,
      megabytes: number
    }
  },
  executionTimePassed: number
}

Errors

Basics

MainError is the primary error class used in Debugger Logger to encapsulate detailed error information.

Each MainError instance contains:

  • Name: The name of the error.
  • Message: An error message providing more detail/context.
  • Timestamp: The date and time when the error occurred.
  • Stack Trace: A formatted and parsed string of stack trace details for pinpointing the error source.

Usage

Utility function to check if an error is a MainError instance, return value changes based on arguments passed to the function.

Debugger.isMainError(error);              // Returns a boolean.
Debugger.isMainError(errorOne, errorTwo); // Returns an array of booleans.

Getting Data

error.getData();

will return an object with the following structure:

{
    name: string,
    message: string,
    timestamp: Date,
    stack: string,
    code?: string, // Error-Code if created via static method `MainError.fromErrorCode()` or provided via `error.addData()`.
    
    ...key?: any,  // Any key-value pairs added via `error.addData()`.
}
error.getStacktraceArray();

will return an array with the following structure:

[
  number: {
      function?: string,
      file?: string,
      line?: number,
      column?: number,
    
      raw?: string, // If parsing fails the `raw` key is provided instead of the ones listed above.
  }
]

Formatting

error.toJSON();   // Returns a JSON formatted string useful for logging.
error.toString(); // Returns a formatted string useful for logging.

License

This project is licensed under the GNU AGPLv3 License.


Back to the top