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

log-method-decorator

v1.0.1

Published

<h1 align="center" style="border-bottom: none;">log-method-decorator</h1> <h3 align="center">Easily log method calls in Typescript without boilerplate</h3> <p align="center"> <a href="https://travis-ci.com/dizco/log-method-decorator"> <img alt="

Downloads

4

Readme

Logging all method calls can add a lot of boilerplate inside your methods. This clutters your actual code and reduces code readability. Here comes log-method-decorator! By leveraging the experimental feature of typescript decorators, we can log all function calls and their execution time with a reusable one-liner :)

Install

npm install --save log-method-decorator

Usage

class Logger {
    log(message: string): void {
        console.log(message);
    }
}

interface Metadata {}

const logOptions: LogOptions<Logger, Metadata> = {
    onMethodStart: ((logger, method) => {
        logger.log(`[${method.className}.${method.methodName}] was invoked`);
    }),
    onMethodEnd: ((logger, method, executionTimeResult) => {
        logger.log(`[${method.className}.${method.methodName}] completed in ${executionTimeResult.executionTimeMs}ms`);
    }),
}

@LogClass<Logger, Metadata>(MyClass.name, logOptions)
class MyClass {
    constructor(public readonly logger: Logger) {}

    @LogSyncMethod<Metadata>({})
    public myMethod(): void {
        // Do something
    }

    @LogAsyncMethod<Metadata>({})
    public async myNetworkCall(): Promise<void> {
        // Do something
    }
}
const myClass = new MyClass(new Logger());
myClass.myMethod();
await myClass.myNetworkCall();
// Prints:
// [MyClass.myMethod] was invoked
// [MyClass.myMethod] completed in 0ms
// [MyClass.myNetworkCall] was invoked
// [MyClass.myNetworkCall] completed in 1ms

Add method metadata

It is possible to add metadata on each method that is then available on the log callbacks.

interface Metadata {
    normalExecutionTimeMs: number;
}

const logOptions: LogOptions<Logger, Metadata> = {
    onMethodStart: ((logger, method) => {
        // method.metadata.normalExecutionTimeMs
    }),
    ...
}

@LogClass<Logger, Metadata>(MyClass.name, logOptions)
class MyClass {
    constructor(public readonly logger: Logger) {}

    @LogSyncMethod<Metadata>({ normalExecutionTimeMs: 100 })
    public myMethod(): void {}

    @LogAsyncMethod<Metadata>({ normalExecutionTimeMs: 80 })
    public async myNetworkCall(): Promise<void> {}
}

Note: You are responsible to make sure that the Metadata typing is compatible between the class decorator and the method decorator. This package does not yet support this.

FAQ

How do I log all method calls?

Easy! Add the decorators on all the methods that you want to be logged :)

Why do we actually need the class decorator? Isn't the method decorator enough?

We need the class decorator in order to have the class name available and to be able to type check the logger instance. Simple method decorators don't have access to the class scope. log-method-decorator uses a logger from the class scope, rather than having to create a new logger.

This package doesn't quite fit my needs...

Please open a discussion, an issue or a pull request where we can discuss your use case. Please keep in mind that this package will never be able to answer all the use cases. Feel free to star the repo :star: and copy the library code and adapt it to your needs.

For more details

For more details on how to integrate log-method-decorator in your application, have a look at the example folder.

Contibuting

  • Star this GitHub repo :star: (it really helps)
  • Create pull requests, submit bugs, suggest new features or documentation updates :wrench:. See contributing doc.

License

MIT © Gabriel Bourgault