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

global-logger-factory

v1.0.0

Published

[![npm version](https://badge.fury.io/js/global-logger-factory.svg)](https://www.npmjs.com/package/global-logger-factory)

Downloads

256

Readme

Global logger factory

npm version

Provides utility functions to easily set up logging. Due to the nature of this library, any configuration changes you make have an effect on any library that makes use of it.

Creating a new logger

The getLoggerFor can be used inside a class as follows:

import { getLoggerFor } from 'global-logger-factory';

class MyClass {
  protected readonly logger = getLoggerFor(this);

  public myFunction(): void {
    this.logger.info('Something is happening');
  }
}

By referencing the class itself in the getLoggerFor call, the class name can be used in the log messages. Instead of using this, a string can also be used.

Setting the logger factory

To define which kind of logger will be created when getLoggerFor is called, setGlobalLoggerFactory needs to be called with an instance of a LoggerFactory. This library comes with a WinstonLoggerFactory that can be used.

import { setGlobalLoggerFactory, WinstonLoggerFactory } from 'global-logger-factory';

setGlobalLoggerFactory(new WinstonLoggerFactory('info'));

After calling this, all calls to getLoggerFor will return a logger created by that factory. Calls to getLoggerFor, and calls to the resulting loggers, will be buffered until setGlobalLoggerFactory is called, after which all messages will be emitted through the logger created by the new factory.

In case setGlobalLoggerFactory is not called before the buffer is full, it is assumed a factory will not be set and the buffer is cleared and all following log messages will be ignored.

There is also a VoidLoggerFactory which simply disables logging.

Setting the log level

The WinstonLoggerFactory takes as input the level of logged messages that should be emitted. new WinstonLoggerFactory('error') will only emit error messages, while new WinstonLoggerFactory('info') will emit error, warning, and info messages.

Components.js

Component.js components are generated for each of the classes. config/logger.json shows an example of how to set up a WinstonLoggerFactory, using a Components.js variable to set the log level.

This exact configuration can be used directly into your own Components.js configuration by importing glf:config/logger.json.