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

timberwolf

v2.1.0

Published

An extremely lightweight dependency-free logger for Typescript

Downloads

289

Readme

TimberWolf 2.0

An extremely lightweight dependency-free logger for Typescript

Installation

NPM:

npm install timberwolf --save

Yarn:

yarn add timberwolf --save

Example

import { logger } from "timberwolf";

logger.info("Hello World!"); 
// "[ INFO ] Hello World!"

logger.addMeta({
  user: "John Doe",
})

logger.debug('Hi John!')
// "[ DEBUG ] Hi John!", { user: "John Doe" }

logger.error('Something went wrong!', { error: 'John did it' }) 
// "[ ERROR ] Something went wrong!", { user: "John Doe", error: "John did it" }

## Usage

### Logging and levels

There are 6 log levels from `fatal` to `trace`.

```js
import { logger } from "timberwolf";

logger.fatal(message: string, metaData?: object);
logger.error(message: string, metaData?: object);
logger.warn(message: string, metaData?: object);
logger.info(message: string, metaData?: object);
logger.debug(message: string, metaData?: object);
logger.trace(message: string, metaData?: object);

Configuration

Set the logger

Timberwolf uses a single function to output all logs.

By default, the console logger is used, which outputs logs to console.log. This can be overwritten by using the setLogger method.

import { logger , Logger, LogLevel } from "timberwolf";

export const myLogger: Logger = (
  logLevel: LogLevel,
  msg: string,
  meta?: object,
) => {
  // Log however you want
};

// Set the logger to use myLogger
logger.setLogger(myLogger)

Setting the Log Level

The order of log priority is:

`` { FATAL: 1, ERROR: 2, WARN: 3, INFO: 4, DEBUG: 5, TRACE: 6, }


Everything below the LogLevel will not be pushed into the transport and not logged.

By default, the log level is set to `INFO` unless you provide a `LOG_LEVEL` environment variable, which will be parsed as a LogLevel.

Additionally, you can set the log level any time using the `setLogLevel` method.

```js
import { logger, LogLevel } from "timberwolf";

logger.setLogLevel(LogLevel.ERROR);

Adding meta data to every log

You may add any object of metadata to be added to the logs.

import { logger } from "timberwolf";

logger.addMeta({
  username: "Batman",
});

You may also clear the meta object.

import { logger } from "timberwolf";

logger.clearMeta();

The metadata will be spread over the metadata object for every subsequent log.

Meta masking

By default Timberwolf will mask sensitive keys in the metadata object. These are defined in the metaMask.ts file.

If you want to disable this, you can use the disableMetaMask method.

import { logger } from "timberwolf";

logger.disableMetaMask();

logger.info("Hello", {
  password: "pass123",
});
// "[ INFO ] Hello", { password: "********" }

logger.enableMetaMask();

logger.info("Hello", {
  password: "pass123",
});

Throwing errors

There is a convenience method to throw an error after logging it. This is available for all log levels.

import { logger } from "timberwolf";

logger.fatal("Oops").throw();
// Throws an error "Oops" after logging it

logger.fatal("Oops").throw("Oh no");
// Throws an error "Oh no" after logging "Oops"

Conditional logging

There is a convenience method when that only logs if the condition is truthy

import { logger } from "timberwolf";

logger.when(true).debug("Simon says");

logger.when(false).warn("Simon didn't say it");