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

@xeinebiu/ts-logger

v1.1.2

Published

Typescript Logger

Downloads

6

Readme

Logger for Typescript

Enable logging on your TS Project using decorators|annotations.


Installation

npm i @xeinebiu/[email protected]

Change Logs

1.1.1
    - Export LogClass
1.1.0
    - Add LogClass Decorator
1.0.1
    - Fix module exports
1.0.0
    - Initial Version

Usage


Logger

Setting the listner is necessary to enable the logging.

Logger.listener = {
    // Return True to output the given [log], false otherwise
    beforeLog: (log: Log, outputType: OutputType) => {
        return true;
    },

    // The given [log] is printed
    afterLog: (log: Log, outputType: OutputType) => {

    },

    // Apply different styles of [Log] and [outputType]
    applyStyle: (log: Log, outputType: OutputType) => {
        if (outputType === OutputType.log) {
            return 'background: #8c7ae6; color: #2f3640; padding: 3px';
        }
        return true;
    }
};

Method Logger

To log any method, use @Method.Log() decorator and @Method.asyncLong() for Asynchronous Methods.

Assume we have a class called Demo and we want to log some methods on it.

class Demo {
    @Method.log()
    public add(...args: number[]): number {
        let sum = 0;
        args.forEach(x => sum += x);
        return sum;
    }
}

Now, every time the method add(..args) is invoked, an output will result in console like the below

MethodLoggerOptions

The decorator @Method.log() accepts an MethodLoggerOptions as single argument where we can configure the Logger for each Method.

    @Method.log({tag: 'Sum collection of numbers', args: true, importance: 1, printResult: true})
public
add(...args
:
number[]
):
number
{
    let sum = 0;
    args.forEach(x => sum += x);
    return sum;
}

With the above configuration, we are about to see 2 logs for the same method call. First Log when the method is invoked, and the second when Return.

Arguments can be printed partially also.

    @Method.asyncLog({tag: 'Login', printResult: true, args: [0]})
public
login(username
:
string, password
:
string
):
Promise < boolean > {
    return new Promise < boolean > ((resolve) => {
        setTimeout(() => resolve(true), 1000);
    });
}


Injector

Injector can be used when Logging from inside of method is needed. Inject as last argument, the Logger and set it to optional.

    @Method.log({inject: true})
public
add(args
:
number[], logger ? : Logger
):
number
{
    logger.debug('Add method is called');
    let sum = 0;
    args.forEach(x => sum += x);
    logger.params.data.result = sum;
    logger.debug(`Total: ${sum}`);
    return sum;
}

Filter

Filter of logs can be done from the Listener beforeLog() callback.

beforeLog: (log: Log, outputType: OutputType) => {
    return log.data.importance
    ! > 2 && outputType === OutputType.debug;
},
...

Outputs only the Logs with importance greater than 2 and of type debug

beforeLog: (log: Log, outputType: OutputType) => {
    return log.data.tag === 'login';
},
...

Outputs only the logs with the Tag login

Depending on your needs, filter can be done on many ways as it needed.

Author

xeinebiu