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

ts-advanced-logger

v1.1.3

Published

Advanced Logger Module

Downloads

344

Readme

ts-advanced-logger

Our Typescript advanced logger supports two main features:

  • Specific Logging by developers
  • Automatically Runtime Verification Logging

Gettings started

Install

First of all, add the ts-advanced-logger to your project.

npm install ts-advanced-logger

You can use the Logger out of the box. For detailed configuration options, see the following configuration-chapter.

Write your first log

To write a log, you only need an instance of the ts-advanced-logger and call a log-method. Lets see how it works:

import {GetLogger, ILogger} from "ts-advanced-logger"

class FooClass {

    @GetLogger()
    private logger: ILogger

    constructor() {
    }

    public bar(): void {
        this.logger.debug("Some debug message!");
        this.logger.info("Some info!");
        this.logger.warn("Oh warning!");
        this.logger.error("Some error occured!");
    }
}

To access your logger, you can use the GetLogger-Decorator. This will inject an instance of your into the decorated field. All accessable methods are defined in the ILogger-Interface.

Now you can use your logger-instance, to log on different logging-levels.

Configuration

You can use this logger as a simple wrapper for the JavaScript console-object, if you fish. But you can do much more with it.

You can:

  • Define Log-Levels for each Class
  • Send your logs to a Server-Enpoint, to see what your Clients are doing
  • Monitor your programs behaviour, by observing method invocations

This Chapter shows how to configure all these features.

Define Log-Levels

There is a Default-Configuration, that simply can be extended by your own Configuration. Lets see how to set the Log-Level for aboved FooClass to warn, by extending the Default-Configuration.

import { DefaultLoggerConfig, ILogLevelRule, LogLevelEnum} from "ts-advanced-logger"

class MyConfig extends DefaultLoggerConfig {
    rules: ILogLevelRule[] = [{
        className: "FooClass",
        logLevel: LogLevelEnum.WARN
    }]

}

// tell the ts-advanced-logger to use your configuration
LoggerConfig.setConfig(new MyConfig())

Send Logs to a Server

The following example show, how to configure the logger, that it will send all Logs to your Server-Endpoint. This might be very helpful to find bugs on production.

To send Logs to a Server, ts-advanced-logger comes with a so called BufferedRemoteLogger. We will do the following steps:

  • Change the default-logger from ConsoleLogger to BufferedRemoteLogger
  • Define where to send the Logs
  • Define the bufferSize, that will be used to collect an amount of bufferSize Logs and sends them all together.
import { DefaultLoggerConfig, BufferedRemoteLogger} from "ts-advanced-logger"

class MyConfig extends DefaultLoggerConfig {
    defaultLoggerClass = new BufferedRemoteLogger()
    serverEnpoint = "https://myService.tld/loggingEndpoint"
    bufferSize = 10
}

// tell the ts-advanced-logger to use your configuration
LoggerConfig.setConfig(new MyConfig())

The Logs will be converted in a JSON-format and sent as a HTTP-Post request.

Runtime Verification

With ts-advanced-logger you can observe method invocations to monitor your programs behaviour. This can be very usefull e.g. for verifying that your program respects some properties, that you defined. You may check thinks like "This method must not return null". Feel free to use any solution you want, to process your logs. You can use Hadoop for instance.

Lets see, how to do Runtime Verification using ts-advanced-logger.

To observe a method, you simply can add the @RVMethod-Decorator.

import { RVMethod } from "ts-advanced-logger"

class BarClass {

    @RVMethod()
    public sumUp(a:number, b:number): number {
        return a+b
    }

}

Now every invocation of the sumUp method will the tracked. Now lets do some configuration to send these Logs to a Server.

import { DefaultLoggerConfig, BufferedRemoteLogger } from "ts-advanced-logger"

class MyConfig extends DefaultLoggerConfig {
    bufferSize = 10
    // use a ajaxBuffer to send data to the endpoint
    rvEndpoint: IEndpoint = new BufferedAjaxEndpoint(this.bufferSize)
    
    constructor() {
        // use a converter, that will convert the logs to JSON
        this.rvEndpoint.setConverter(new JsonRvLogConverter())
        // set the server enpoints url
        this.rvEndpoint.setUrl("https://myService.tld/rvLogging")
    }
}

// tell the ts-advanced-logger to use your configuration
LoggerConfig.setConfig(new MyConfig())

Using this configuration, the logs will be sent as a JSON-Array using a HTTP-POST request. See an example, how the JSON will look like:

[
    {   
        "timestamp":"2017-07-26T13:26:51.236Z",
        "clientId":"1d4b182f-b0b5-4c84-a696-b364e8a55910",
        "logNumber":3,
        "methodName":"BarClass.sumUp",
        "arguments":[10, 5],
        "result":15,
        "executionTimeMS":1
    }
]

The clientId is an unique identifier for the client, that will be generated once, when the logger needs the id for the first time.

The logNumber is an auto-incrementing number to track the order of method-invocations.

Build the project

The following command will create the transpiled files in ./js

  • npm install
  • npm run compile

Tests

Use npm test to run all Test-Suites.