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

lognostic

v0.0.1

Published

Logger facade that provides decorators and context management - agnostic from implementation.

Downloads

1

Readme

Lognostic

Lognostic is a logger facade that provides decorators and context management.

WARNING: This module does not yet have any functionality.

Please do not install this module and expect it to work until you see v1.0.0.

Proposal

When designing an anostic logger, we have to keep in mind that some loggers:

  • support custom log levels
  • do not have the same builtin log levels (e.g. bunyan has fatal but winston does not)

Note: lognostic should work irrespective of log format (e.g. space separated vs json).

You should be able to pass in any logger like so:

// Assume logger is created according to logger facade:
const bunyan = new BunyanLogger();
const winston = new WinstonLogger();
const pino = new PinoLogger();

import { LogFactory, Log } from 'lognostic';

// Interface for setting/defining a logger change over time. This is just an example:
LogFactory.setLogger(winston);

class InvoiceService {
    // The idea is to have level override which log level to label the events as.
    @Log({ level: 'debug' })
    getUser(username: string) : Promise<User> {
        return wait this.dynamoDbClient.get(/*...parameters...*/).promise();
    }

    @Log({ level: 'debug' })
    generateInvoice(user: User, session: Session) : Invoice {
        return new Invoice(user, session);
    }

    @Log({ level: 'info' })
    submitInvoice(invoice: Invoice) : Promise<void> {
        return this.invoiceQueue.send(invoice);
    }
}

In this example, we invoke the class methods like so:

const service = new InvoiceService();
const user = await service.getUser('username123');
const invoice = await service.generateInvoice(user, session);
await service.submitInvoice(invoice);

Every function invocation with a decorator would pass events to the registered logger as a type:

{
    level: <LOG LEVEL>,
    logEvent: 'start',
    function: {
        name: 'getUser'
        args: [ 'username123' ]
    }
}
{
    level: 'error',
    logEvent: 'error',
    function: {
        name: 'getUser'
        args: [ 'username123' ]
    },
    error: {
        name: 'SomeError',
        message: 'some error message',
        stack: 'stack trace...'
    }
}
{
    level: <LOG LEVEL>,
    logEvent: 'end',
    function: {
        name: 'getUser'
        args: [ 'username123' ]
    }
}

In the future, the idea is to allow the creation and maintainence of contexts for each @Log decorator:

class InvoiceService {
    // The idea is to have level override which log level to label the events as.
    @Log({ level: 'debug', using: Contextual.get() })
    getUser(username: string) : Promise<User> {
        return wait this.dynamoDbClient.get(/*...parameters...*/).promise();
    }

    @Log({ level: 'debug', using: { ...Contextual.get(), /*...figure out how to create child context from a parent context...*/} })
    generateInvoice(user: User, session: Session) : Invoice {
        return new Invoice(user, session);
    }

    @Log({ level: 'info' })
    submitInvoice(invoice: Invoice) : Promise<void> {
        return this.invoiceQueue.send(invoice);
    }
}

import { initializeTraceContext } from 'b3trace';
const invoiceService = new InvoiceService();
function handler({headers}, ctx) {
    const traceId = headers['x-b3-traceid'];
    const spanId = headers['x-b3-spanid'];
    const trace = initializeTraceContext({ traceId, spanId });

    Contextual.using(traceId, { trace });
    try {
        const user = await service.getUser('username123');
        const invoice = await service.generateInvoice(user, session);
        await service.submitInvoice(invoice);
        return { statusCode: 200 };
    } catch(err) {
        return {
            // handle error states
        };
    } finally {
        Contextual.remove(traceId);
    }
}

export {
    handler
};

The exact details for preserving and resetting a context has not been fully defined.

The problem with the proposed design above is that there is no clear way to create & manage child Contexts:

/**
 * How do I construct a child trace from the parent?
 * Will other methods that want to create a child context point to the correct parent?
 * How will a context know if it's the child or the parent?
 * Can I guarantee the context tree integrity if the a method is called multiple times within a single context? Will this cause issues down the line?
 */
@Log({ level: 'debug', using: Contextual.get() })
getUser(username: string) : Promise<User> { /*...*/ }