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

ts-composite-logger

v1.2.8

Published

Logger written in typescript. Includes a console channel by default. Allows to add other log channels by extending ILogChannel interface.

Downloads

12

Readme

Composite logger

Use this package in your applications for logging. Out of the box the package outputs logs to console. You can create more custom channels and send data to them as well, dynamically add and remove them. The advantage of this logger is that it's easy to extend it.

npm install -S ts-composite-logger

Basic usage

import {Logger, Console, LogLevel} from "ts-composite-logger";
  
const console = new Console();  
console.setFormat("hh:mm:ss.SSS"); // this is default

global.logger = (new Logger())
  .addChannel(console)
  .setLevel(LogLevel.INFO);
	
global.logger.info("VM connected");

Available log levels:

  • fatal
  • error
  • warn
  • info
  • debug

By default debug log level is selected. Which means that every log reqest will be fullfilled.

Date format options for console channel

Format string can be anything, but the following letters will be replaced (and leading zeroes added if necessary):

  • dd - date.getDate()
  • MM - date.getMonth() + 1
  • yy - date.getFullYear().toString().substring(2, 4)
  • yyyy - date.getFullYear()
  • hh - date.getHours()
  • mm - date.getMinutes()
  • ss - date.getSeconds()
  • SSS - date.getMilliseconds()
  • O - timezone offset in +hm format (note that time will be in UTC if displaying offset)

The underlying package to format dates is date-format. You can find more details here

Create you own channel

This example demonstrates how to create a MongoDB channel. The implementation can be found here.

Create a class that implements ILoggerChannel interface.

import assert from "assert";
import {MongoClient} from "mongodb";
import {ILoggerChannel} from "ts-composite-logger";
import {ILogMessage} from "ts-composite-logger";

export class MongoDB implements ILoggerChannel {
  private readonly connectUrl: string;
  private readonly dbName: string;
  private readonly collectionName: string;
  private client;

  constructor(url: string, db: string, collection: string) {
    this.connectUrl = url;
    this.dbName = db;
    this.collectionName = collection;
  }

  public async write(message: ILogMessage) {
    if (!this.client) {
      throw new Error("Channel not connected to DB");
    }

    const db = this.client.db(this.dbName);
    const collection = db.collection(this.collectionName);
    await this.insert(collection, message);
  }

  private insert(collection, message: ILogMessage) {
    return new Promise((resolve, reject) => {
      collection.insertOne(message, (error, result) => {
        if (error) {
          return reject(error);
        }

        return resolve();
      });
    });
  }

  public connect(): Promise<ILoggerChannel> {
    return new Promise((resolve, reject) => {
      MongoClient.connect(this.connectUrl, { useNewUrlParser: true }, (err, client) => {
        try {
          assert.strictEqual(null, err);
        } catch (e) {
          return reject(e);
        }
        
        this.client = client;
        resolve(this);
      });
    });
  }
}

Now you can add an instance of this class to the composite logger by calling addChannel method.