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

@topmarksdevelopment/javascript-logger

v1.1.0

Published

A compact tool to help log messages/errors in your javascript code

Downloads

5

Readme

JavaScript Logger

A custom package to log steps/errors throughout your package. There is also a static field that you can use to serialize catch errors.

Links

Usage

Import the package and create a class that extends the JavascriptLogger class. Then provide the body for the two functions and also the 'type' for the extraDetails argument (as shown below). If you don't want a 'type' then just use object.

interface ExtraInfo {
    error?: unknown
    logLevelForNow?: LogLevel
}

class MyLogger extends JavascriptLogger<ExtraInfo> {
	logLevel = (extraDetails?: ExtraInfo): LogLevel => 
        extraDetails?.logLevelForNow ?? LogLevel.warn;

	logIt = (logLevel: LogLevel, title: string, message?: string | string[], extraDetails?: ExtraInfo): void => {
        // consume the information and do stuff
    };
}

const logger = new MyLogger();

// Some code, somewhere
function thisWillAlwaysWork() {
    try
    {
        throw new Error("Whoops, guess not!");
    }
    catch (err)
    {
        logger.logCritical(
            "0x100001 - Critical 1",
            [
                "Oh no, there was a critical error in my program!",
                "The hex above can assist in identifying the location"
            ],
            {
                // Log the error, rather than use properties from it
                error: JSON.parse(JavascriptLogger.convertToJson(err))
            }
        )
    }
}

Functions

logLevel

This specifies the log level to start outputting the messages

Arguments: extraDetails?: T(custom type)
Output: LogLevel (critical, error, warn, info, debug, trace)

logIt

This is the function that is called when a log occurs that meets the minimum log level specified by logLevel

Arguments: logLevel: LogLevel, title: string, message?: string | string[], extraDetails?: T(custom type)
Output: NONE

Built in components

JavascriptLogger.convertToJson(object)

This built in function will convert an object to a JSON string. This feature exists primarily to help with outputting an Error (as shown above).

ConsoleJavascriptLogger

This built in class will output straight to the console. You set the log level with it's constructor (as shown below).

var consoleLogger = new ConsoleJavascriptLogger(LogLevel.info);

Other ideas

Log to your server: rather than JS errors being lost to the cosmos with no idea they're happening. Use the logger to send the data to your server! Just remember to secure the access point 😁

Store the logs: implement a storage solution that saves all the logs and only outputs certain instances. Then you can have a custom function/command that can process them all to your will at any point! Maybe in a plugin that you can include all the logs when the user wants to raise a GitHub issue 🤷‍♂️