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

@soinlabs/hawk

v1.0.0

Published

Package to better manage errors, logs, and its notifications

Downloads

1,154

Readme

Hawk

codecov npm version npm downloads

Error and logs framework for Soin Labs projects.

How to use

For getting a simple use of Hawk, follow the steps bellow:

  1. Export the Hawk constructor and make an instance:

    // your index.js to handle hawk singleton configuration
    
    const { Hawk } = require("@soinlabs/hawk");
    
    const customizedHawk = new Hawk({
      logsPath: "yourWorkspace/route/example/logsDirectory",
      slackToken: "yourWebhookChannelURL",
      appName: "yourApplicationName",
      optionalSign: "optionalBenchmark",
    });
  2. Use your Hawk instance to perform your logs

    customizedHawk.log("My first log");
    
    const newError = customizedHawk.CreateElasticError("message", 300);
    customizedHawk.log(newError);
    
    // It is possible to perform a simple log with logging level
    const { Logger } = require("@soinlabs/hawk");
    customizedHawk.log("My first log", Logger.SILLY);
  3. Some error and logs handling utilities

    // Some error and logs handling utilities:
    
    const { Logger, LoggedError } = require('@soinlabs/hawk')
    
    const LoggingLevels = {
        error: Logger.ERROR,
        warn: Logger.WARN,
        info: Logger.INFO,
        http: Logger.HTTP,
        verbose: Logger.VERBOSE,
        debug: Logger.DEBUG,
        silly: Logger.SILLY,
    };
    
    const ErrorLevels = {
        low: LoggedError.LOW_LEVEL,
        medium: LoggedError.MEDIUM_LEVEL,
        high: LoggedError.HIGH_LEVEL,
    };
    
    /**
    * It creates an Elastic Error with message and code, after, it makes log with this error
    * and throw the created error as an exception
    * @param {String} message what happened?
    * @param {Number} status status code
    * @param {Number} errorLevel optional
    */
    function sendError(message, status, errorLevel = ErrorLevels.low) {
        const error = customizedHawk.CreateElasticError(message, status);
        error.setErrorLevel(errorLevel);
        customizedHawk.log(error, LoggingLevels.error);
        throw error;
    }
    
    /**
    * It performs a log with this message at warning level
    * @param {String} msg
    */
    function sendWarning(msg) {
        customizedHawk.log(
            { message: msg, referenceCode: customizedHawk.getReferenceCode() },
            LoggingLevels.warn
        )
    }
    
    /**
     * It creates an Elastic Error with message and code, after, it makes log with this error
    * @param {String} message
    * @param {Number} code
    * @param {Number} errorLevel optional
    */
    function logError(message, code, errorLevel = ErrorLevels.low) {
        const error = customizedHawk.CreateElasticError(message, code)
        customizedHawk.log(error, LoggingLevels.error)
    }
    
    ...
    // Use your utilies
    
    // This throw an exception
    sendError("No id provided", 400);
    
    // It performs a warning level log
    sendWarning('Bad request')

Another alternatives

You could do some things like these:

  1. Throw LoggedError

    // Use builder pattern
    
    throw new LoggedError()
      .setMessage(`Error captured by hawk: ${message}`)
      .setReferenceCode("myFile.js")
      .setErrorLevel(LoggedError.HIGH_LEVEL)
      .setToLog(true)
      .setToSlack(true);
    
    // Another way
    
    throw new LoggedError({
      message: `Error captured by hawk: ${message}`,
      status: 401,
      toLog: true,
      toElastic: false,
      toSlack: true,
      newField: "yourNewFieldValue",
    });