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

zz-log

v0.0.1

Published

Colorful and extensible logging.

Downloads

3

Readme

zz-log

Colorful and extensible logging.

Usage

Basic Console Logging

import { ConsoleLogger } from 'zz-log';

const logger = new ConsoleLogger();

// Log a simple message.
logger.info('Starting');

// Log a warning with more complex details.
logger.warn({
  title: 'No cheese left',
  detail: 'Store has no cheese left in stock',
  subDetail: 'Next delivery on Wednesday'
});

const error = new Error('Something bad happened');

// Log an error.
logger.error({
  title: 'Unexpected Error',
  error,
});

error.innerError = new Error('This is an inner error');
error.innerError.innerError = new Error('This is another inner error');

// Log an error that has innerErrors.
logger.fatal({
  title: 'Fatal Error',
  error,
});

// Log a debug message
logger.debug({ title: 'Memory Usage', subDetail: process.memoryUsage() });

// Log a trace message
logger.trace('Bye');

This outputs the following to the console:

Output in the console

Restricting Log Levels

You can choose which log levels are actually logged.

import { ConsoleLogger, LogLevel } from 'zz-log';

const errorOnlyLogger = new ConsoleLogger({ logLevel: LogLevel.error | LogLevel.fatal });

errorOnlyLogger.error('It logs this');
errorOnlyLogger.fatal('It logs this too');
errorOnlyLogger.warn('It does not log this');

This outputs the following to the console:

Output in the console

NOOP Logger

The NOOP logger does nothing, and is meant for certain environments like unit testing, where you may not want to log anything.

import { NoopLogger } from 'zz-log';

const noopLogger = new NoopLogger();

noopLogger.warn('It does not log this');
noopLogger.debug('Or this');
noopLogger.fatal('Or anything else');

Alternatively, you could use a ConsoleLogger or other logger, and set its logLevel option to LogLevel.none.

Custom ConsoleLogger formatting

You can extend ConsoleLogger and override its functions to implement custom formatting, for example:

import { ConsoleLogger } from 'zz-log';

class MyConsoleLogger extends ConsoleLogger {
  // Log custom "part" fields instead of just the "detail" field.
  formatDetail(logItem) {
    return `${logItem.part1} -- ${logItem.part2} -- ${logItem.part3}`;
  }

  // Disable timestamp logging.
  formatTimestamp() {
    return undefined;
  }
}

const myLogger = new MyConsoleLogger();

myLogger.warn({
  title: 'Custom Log Fields',
  part1: 'p1',
  part2: 'p2',
  part3: 'p3',
  subDetail: 'XyZ'
});

This outputs the following to the console:

Output in the console

Composite Logger

You can log to more than one logger, by creating a CompositeLogger.

import { CompositeLogger, ConsoleLogger } from 'zz-log';

const compositeLogger = new CompositeLogger([new ConsoleLogger(), new ConsoleLogger()]);
compositeLogger.warn('I get logged twice!');

This outputs the following to the console:

Output in the console

Custom Logger

You can also go one level higher and extend directly from BaseLogger to implement other log targets besides the console:

class MyCloudLogger extends BaseLogger {
  log(info, logLevel) {
    myLogToCloudFunction(logLevel, {
      title: info.title,
      detail: info.detail,
      error: info.error,
      otherFields: { ...info },
    });
  }
}

If that's still not flexible enough, you can also choose to implement the ILogger TypeScript interface (or in plain JS, create a class that implements these functions):

export interface ILogger {
  fatal(info: LogItem): void;
  error(info: LogItem): void;
  warn(info: LogItem): void;
  info(info: LogItem): void;
  debug(info: LogItem): void;
  trace(info: LogItem): void;
}