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

zerg

v2.1.2

Published

lightweight logging library for apps and libs

Downloads

75

Readme

Build Status Coverage Status npm version npm downloads

Zerg

Lightweight logging library for apps and libs

Futures

  • Zero dependencies
  • TypeScript support
  • Easy to use
  • Custom listeners/transports
  • Support Node.js and Browsers

Getting started

Installation

npm i --save zerg

or

yarn add zerg

Usage

Make module logger.js:

import zerg from 'zerg';
import {
  consoleNodeColorful,
  consoleBrowserColorful,
} from 'zerg/dist/transports';

const logger = zerg.createLogger();

// Add console logger
const listener = zerg.createListener({
  handler: consoleBrowserColorful, // for browser
  // handler: consoleNodeColorful, // for node
});

logger.addListener(listener);

export default logger;

Make your module and import logger.js:

import logger from './logger';

const log = logger('moduleName');

log.verbose('verbose message');
log.debug('debug message');
log.info('info message');
log.warn('warn message');
log.error('error message', {foo: 'bar'});

Result:

ScreenShot

API

Types

type TExtendedData = Record<string, any>;
type TLogMessage = {
  timestamp: number;
  loggerName: string;
  moduleName: string;
  level: TLogLevel;
  message: string;
  extendedData?: TExtendedData
};
type LogLevel = 'verbose' | 'debug' | 'info' | 'warn' | 'error';
type Listener = (log: TLogMessage) => void;

zerg.createLogger(): Logger - Create logger instance

zerg.createListener(params): LogListener - Create listener for logger

  • params.handler: (logMessage: TLogMessage) => void;
  • params.filter?: (logMessage: TLogMessage) => boolean; (optional)
  • params.levels?: LogLevel; (optional)

logger.addListener(listener: LogListener)

import zerg from 'zerg';

const logger = zerg.createLogger();

const listener = zerg.createListener({
  handler: (logMessage) => console.log(logMessage),
  levels: ['info'], // listen only `info` level
});

logger.addListener(listener);

logger.module('myModule').info('Info message', {foo: 'bar'});
logger.module('myModule').warn('Warn message', {bar: 'baz'});
logger.module('myModule').error('Error message');

/* console
{
  timestamp: 1467967421933,
  level: 'info',
  moduleName: 'myModule',
  message: 'Info message',
  extendedData: {foo: 'bar'},
}
*/

Use filter - Listen messages only from "Sarah" module

import zerg from 'zerg';

const logger = zerg.createLogger();

const listener = zerg.createListener({
  handler: (logMessage) => console.log(logMessage),
  filter: (logMessage) => logMessage.moduleName === 'Sarah', // listen messages only from "Sarah" module
  // levels: [], // at this case levels are ignoring
});

logger.addListener(listener);

logger.module('Alice').info('Info message', {foo: 'bar'});
logger.module('Bob').warn('Warn message', {bar: 'baz'});
logger.module('Sarah').error('Error message');

/* console
{
  timestamp: 1467967421933,
  level: 'info',
  moduleName: 'myModule',
  message: 'Info message',
  extendedData: {foo: 'bar'},
}
*/

removeListener(LogListener): void;

removeAllListeners(): void;

module(moduleName: string): LoggerModule;

getModule(moduleName: string): LoggerModule | null;

getModules(): Record<string, LoggerModule>;

Other Examples

Sentry transport

import zerg from 'zerg';
const logger = zerg.createLogger();

const SENTRY_LEVEL_MAP = {
  info: 'info',
  warn: 'warning',
  error: 'error',
  fatal: 'error',
};

function sentryTransport(logMessage) {
  const level = SENTRY_LEVEL_MAP[logMessage.level];

  Sentry.withScope((scope) => {
    scope.setLevel(level);

    Object.keys(logMessage.extendedData).forEach((key) => {
      scope.setExtra(key, logMessage.extendedData[key]);
    });

    scope.setTag('module', logMessage.moduleName);

    Sentry.captureMessage(logMessage.message);
  });
}

const listener = zerg.createListener({handler: sentryTransport});

logger.addListener(listener);

Remote debug transport

It is be useful for debug when browser (or device) doesn't provide tool: Android with default browser, WinPhone, SmartTV.

At browser:

import zerg from 'zerg';
const logger = zerg.createLogger();

function remoteTransport(logMessage) {
  const req = new XMLHttpRequest();
  req.open('POST', 'http://myhost.com:3000/log', false);
  req.setRequestHeader('Content-type', 'application/json');
  req.send(JSON.stringify(logMessage));
}

const listener = zerg.createListener({handler: remoteTransport});

logger.addListener(listener);

Don't forget, host (http://myhost.com:3000/log) must be reachable from device.

At server you may use express:

const express = require('express');
const bodyParser = require('body-parser');

const app = express();
app.use(bodyParser.json()); // for parsing application/json

app.post('/log', (req, res) => {
  console.log(req.body);
});

app.listen(3000);