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

@vebgen/logger

v0.0.3

Published

Logging for web apps

Downloads

5

Readme

logger

Logging utilities.

Usage

First create a logger and provide it with a minimum log level (messages below this level will not be logged) and a list of handlers:

const loggerLevel = 1;
const logHandlers = [
    new ConsoleHandler(),
]
const logger = new Logger(
    loggerLevel, logHandlers
);

There are two handlers that you can use out of the box:

  • ConsoleHandler: logs to the WebConsole and
  • ApiHandler: logs to a remote point.

You can implement your own handler based on LogHandler class and you can have as many handlers in a logger as you like.

Now you can send messages to the logger:

logger.error("Something went wrong");

Logging

The main function that implements the logging mechanism is Logger.log(). You provide it with:

  • a log level indicating the severity of this message,
  • a message body,
  • some parameters related to this message (optional) and
  • a context (also optional, see below).

To make things easier the class also implements following methods that call the Logger.log() with an appropriate log level: critical, security, error, warn, info, debug and trace.

Handlers

The only required method of the handlers is the handle() method. It receives the message that passed the level filtering and has access to the logger that issued the message.

The default console handler attempts to give an unique look to the messages generated by the logger.

The api logger attempts to use the beacon API used for web vitals and falls back to using fetch. It expects an url at construction time.

Context

The logger holds a stack of strings to which the user can push and pop to indicate context for the messages to follow.

On top of that each log call can indicate its own context either as a single string or as an array of strings.

Levels

The default implementation takes hints from the python logging module. The log level can be indicated by a numeric value of by a named level.

You can choose to use different numeric constants for the different levels by providing the logger constructor with a list of your named levels. For the default implementation of specific log functions (error, warn, debug, etc) to work your levels have to be named the same as default ones (CRITICAL, SECURITY, ERROR, WARNING, INFO, DEBUG, TRACE). You can, of course, override the implementation of these functions, in which case you can use whatever you like as level names.

Note that handlers receive only the numeric value of the level so they are not affected in any way by the implementation of the levels.

Running unit tests

Run nx test logger to execute the unit tests via Jest.