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

@yopdev/logging

v0.0.2

Published

This module implements a logging library used on all PoK services and internal libraries.

Downloads

10

Readme

Logging

This module implements a logging library used on all PoK services and internal libraries.

API

  • LoggerFactory object - single create(name) method

  • Logger object:

export interface Logger {
  debug(msg?: string, ...args: any[]): void;
  debug<T>(obj?: T, msg?: string, ...args: any[]): void;
  info(msg?: string, ...args: any[]): void;
  info<T>(obj?: T, msg?: string, ...args: any[]): void;
  warn(msg?: string, ...args: any[]): void;
  warn<T>(obj?: T, msg?: string, ...args: any[]): void;
  error(msg?: string, ...args: any[]): void;
  error<T>(obj?: T, msg?: string, ...args: any[]): void;
}

Usage

  1. Import the logging package
import { LoggerFactory } from "@yopdev/logging";
  1. Create a new Logger instance (ideally private to the component you'll be logging from)
    private logger = LoggerFactory.create(Config.name);
  1. Log stuff
this.logger.info("Logging stuff will take you places, %s", name);

Settings via env-vars

  • LOG_LEVEL: Sets the root logging level (debug, info, warn, error)
  • LOG_TRANSPORT: pretty is only supported for now. Uses the stdOut and colorizes output with a simple formatter. Enabled when running with NODE_ENV === development
  • LOG_CALLER: Enables caller information in logs. Also enabled when running with NODE_ENV === development.

WARNING:: There are no settings available to alter how we log in production. Prod-mode is our default logging strategy. Don't add these settings to production or pre-production environments as they will incurr in additional costs.

Best practices

  • Don't send unpredictable contents to the logger as objects. DON'T DO THIS:
    const someObject: SomeTypeThatMayHoldLotsOfData = ...
    this.logger.info(someObject, "I'm logging this at the info level and serializing a huge object...");
  • Choose the proper logging level.

    • Don't do INFO unless you're certain it's something you want logged every time in production.
    • Use WARN for potential errors, like things that you want to inform to the operations team but aren't necesarily errors.
    • Use ERROR for real errors. Make sure you're sending the Error object first! No need to use templates/interpolation, keep it clean.
  • Send the right parameters the the logger (see Logging methods)

Logging methods

Templated string

Will log a string from a templated string (aka string.format).

this.logger.info("a string %s, a number %d, an object %O", "one", 2, {
  one: 2,
  three: "four",
});

NOTE: You can still log serialized objects using one of %j, %o or %O placeholders, but keep in mind these will take a hit on performance and costs in production.

Placeholders

  • %s - String.

  • %d - Number (integer or floating point value) or BigInt.

  • %i - Integer or BigInt.

  • %f - Floating point value.

  • %j - JSON. Replaced with the string '[Circular]' if the argument contains circular references.

  • %o - Object. A string representation of an object with generic JavaScript object formatting. Similar to inspect() with options { showHidden: true, showProxy: true }. This will show the full object including non-enumerable properties and proxies.

  • %O - Object. A string representation of an object with generic JavaScript object formatting. Similar to inspect() without options. This will show the full object not including non-enumerable properties and proxies.

Message with merged serialized object

Will log the same templated message, but will merge the given object into the resulting log NDJSON record.

this.logger.info(authInfo, "User %s logged in succesfully", authInfo.username);

References

See more docs on the underlying library we use for logging: https://getpino.io/#/