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

@tonysamperi/logger

v1.0.0

Published

A logger that can wrap and control almost every logger library

Downloads

562

Readme

smp-logger

A logger service that standardizes and centralizes logs across your applications.

What

It works as a singleton to avoid any computational waist. You can configure multiple instances by passing an application name when you call the init method. The logger is bound to the base logger (e.g. the console), so the Web Console shows the correct file and line number of the original call. To create your own logger wrapping any logger other than console, just extend this to unlock all the power of a commonly controllable interface to logs. This class allows adding more features, such as timestamp and metadata, or customizing the format of the logs, by enabling and configuring the preprocessingFn (again, extend it to customize the behaviour).

Why

I needed a way to centralize my log handling, to make all the logs go through log applications that apparently don't work with monkey patching and also have a central switch to decide which logs need to be actually produced. Also, while Sentry for example can collect everything without any action on your side, Datadog cannot. So I used this library to create a centralize handler for both FE and BE. At FE it wraps Datadog Browser Logger, at BE it wraps Winston to send logs with http calls.

Usage

Default singleton instance

SmpLoggerService.init({
    level: SmpLoggingLevels.DEBUG,
    enableSessionId: !0
});

//

const log = SmpLoggerService.INSTANCE;
const foo = "bar";
log.info("Set my var", foo);

Named multi instance

In this scenario we exploit the preprocessing to automatically add the "checkout" context (actually a tag) to each log automatically. See the customizing section to know how to change and improve this behaviour.

SmpLoggerService.init({
    level: SmpLoggingLevels.DEBUG,
    enableSessionId: !0,
    enablePreprocessing: !0
}, "checkout");

//

const log = SmpLoggerService.get("checkout");
const foo = "bar";
log.info("Set my var", foo);

Change the logger

    protected _updateLevel(newValue: KikLoggingLevels = KikLoggingLevels.WARN) {
        KikLoggerMethods.describe().forEach((key) => {
            this[key] = kikNoop;
        });
        KikFirebaseLoggerService._level = newValue;
        this._setup(logger || console);
    }

Why do I need to override the whole "_updateLevel" method?

Because you may want to load your own logger asyncronously. E.g.

    protected _updateLevel(newValue: KikLoggingLevels = KikLoggingLevels.WARN) {
        KikLoggerMethods.describe().forEach((key) => {
            this[key] = kikNoop;
        });
        KikFirebaseLoggerService._level = newValue;
        if(typeof window !== typeof void 0) {
            logger = await import("@datadog/browser-logs").then((lib) => lib.datadogLogs);
        }
        else {
            const winston = await import("winston");
            logger = winston.createLogger({
                // Don't even need to pass the level here, cause SmpLogger is already filtering enabled levels ;)
                format: winston.format.json() 
            });
        }
    }

Customize

Extend it to add your custom logics, or override behaviours. Here for example we want to exploit the default preprocessing, but convert all passed data to JSON

class MyLogger extends SmpLoggerService {
    preprocessArgs(level: KikGenericLoggerMethodKeys, ...args: any[]): [string, string, ...string[]] {
        const baseProcessing = this._defaultPreprocessArgs(level, ...args);

        return [
            baseProcessing.shift(), // Timestamp
            baseProcessing.shift(), // Message
            ...baseProcessing.map(value => {
                return JSON.stringify({
                    tags: [this._appName, `level-${KikLoggingLevels[this.level]}`, level],
                    timestamp: +now,
                    sessionId: this.sessionId,
                    metadata: this.filterSensitiveData(value)
                });
            })
        ];
    }

}

//

MyLogger.init({
    level: SmpLoggingLevels.INFO,
    enablePreprocessing: !0
});

//

const log = MyLogger.INSTANCE;
const someObject = {
    foo: new Date()
};
log.info("Set my var", someObject);