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

@tsjam/logger

v1.0.3

Published

Vanilla TypeScript Logger with multiple output channels

Downloads

623

Readme

@tsjam/logger

Vanilla TypeScript Logger

Not opinionated ts Logger with multiple output channels.

Advantages:

  • appId (distinguish log between multiple instances)
  • tags support (tag child loggers, find and filter certain logs super-fast)
  • multiple channels output (add ur own output: e.g. parallel remote monitoring)
  • buffering (useful for crash reporting)
  • sanitization of sensitive fields (perf optimized, add { sanitize: ['sessionId'] })
  • stack output of any call (configurable, add { withStack: true })
  • fully customizable (use your own format)
  • fair Errors serialization into string (check JSON.stringify(new Error('Oops')); // {})
  • strigify payload at any moment (add { stringify: true })
  • zero third-party dependencies

Output example:
[app1611253982848][2024-01-21T18:33:02.981Z][info][#user] Logged In: { username: Bob, password: '***' }

Installing

npm install @tsjam/logger

Usage (Out of Box)

import { jamLogger } from '@tsjam/logger';

jamLogger.info('Hello Logger!');
// [app1611253982848][2024-01-21T18:33:02.981Z][info] Hello Logger!

Tagged Logger

const logger = jamLogger.tagged('user'); // child logger with added tags
logger.info('Greetings for', { name: 'Bob' });
// [app1611253982848][2024-01-21T18:33:02.981Z][info][#user] Greetings for { name: 'Bob' }

single usage

jamLogger.info({ tags: ['#user', '#vip'] }, 'Greetings for', { name: 'John' });
// [app1611253982848][2024-01-21T18:33:02.981Z][info][#user,#vip] Greetings for { name: 'John' }

Sensitive Fields Sanitization

jamLogger.debug({ sanitize: ['password'] }, 'Logged in', { name: 'Bob', password: 'ABC' });
// [app1707238076394][2024-02-06T16:47:56.398Z][debug] Logged in  { name: 'Bob', password: '***' }

Stack Visibility (show / hide / trim)

Shown on Error payloads (same like Console.log);

jamLogger.warn('Oops!', new Error('Something went wrong'));
// [app1707238920096][2024-02-06T17:02:00.108Z][warn] Oops  Error: Something went wrong
//    at Object.<anonymous> (...tsjam-logger/tests/logging/log.util.spec.ts:10:49)
//    at Promise.then.completed (...tsjam-logger/node_modules/jest-circus/build/utils.js:298:28)
//    ...

Hide stack

jamLogger.warn({ withStack: false }, 'Oops!', new Error('Something went wrong'));
// [app1707238920096][2024-02-06T17:02:00.108Z][warn] Oops  Error: Something went wrong

Show stack for any call

jamLogger.warn({ withStack: true }, 'Oops! Spoiled the milk!');
// [app1707239639479][2024-02-06T17:13:59.496Z][warn] Oops! Spoiled the milk! Stack:
// at Object.<anonymous> (...tsjam-logger/tests/logging/log.util.spec.ts:10:15)
// at Promise.then.completed (...tsjam-logger/node_modules/jest-circus/build/utils.js:298:28)
// ...

Note: it's also possible to just trim the stack to a certain depth via trimStack


Usage (bake ur Own Logger)

export const logger = createLogger({
  appId: `ioApp${Date.now()}`,
  channels: [...Logger.getDefaultChannels() /* { out: MyOutput } */], // default output channel is ConsoleOutput
  translator: Logger.jsonStringifyTranslator, // JSON.stringify All log data arguments
});
export const aiLogger = logger.taggeg('ai'); // child logger with #ai tag

Note: if U want to use ur own Output Channel or to Customize output format, to get raw (not stringified) payload do not set translator.

Custom Output Channel

const myOutput: LogOutput = {
  write: ({ appId, date, level, message, data, context }: LogEntry) => {
    // do something with log payload
  },
};

Buffering

Use simplistic BufferOutput to buffer logs for any crash reporting or remote monitoring. Do not forget to flush after report is sent.

export const logBuffer = new BufferOutput(2000);
export const logger = createLogger({
  channels: [...Logger.getDefaultChannels(), { out: logBuffer }],
});

Translators

There are some built-in translators for log data u could use while baking ur own logger:

  • Logger.jsonStringifyTranslator – JSON.stringify All log data arguments (used when { stringify: true } is passed in context)
  • Logger.stringifyErrorStackTranslator – Fairly serialize Error into string (used by default on Errors payload)

@seeAlso

TSJam API Documentation