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

flexible-logging-service

v1.0.0

Published

General purpose remote logging module

Downloads

5

Readme

Node.js CI

Flexible Logger

A simple logging package for nodejs. Oriented towards flexibility, supports logging to an ElasticSearch instance by default through the ElasticSearchDriver class.

Support for GeoLite IP lookup is immediate, you just need to extend the IpLogEntry class.

Support for access logging of an express web server is out-of-the-box through the AccessLogEntry class, which will extract all the useful information from the Express Request object.

Support for additional logging facilities is easily accomplished by extending the interface LoggerDriver which has just one method: write(LogEntry).

Additionally, to limit the rate of network requests, the indexing can be bulked by moving it to an additional node.js instance using the UDP driver. Set up the LogUdpServer on a different instance running inside your network and use the UdpDriver to send the logs to it.

Usage:

import FlexibleLogger from 'flexible-logger';
...

/* Instanciated asynchronously */

const Logger = await FlexibleLogger.with(new ElasticSearchDriver({
    setup: { ... }, // ElasticSearch Configuration
    indexPattern: 'my-index' // Index where documents will be inserted
    }), 
    'path/to/geolite2-city.mmdb' // Path to the GeoLite2 City database
);

/* or */

FlexibleLogger.with(...).then(Logger => {
    ...
});

/* Instanciated synchronously
 * Might be a better choice especially if you need a global instance that
 * gets initialized once and gets called in your code like a Singleton
 */

const Logger = FlexibleLogger.withSync( ... );

Logging...

Logger.log(new AccessLogEntry(
    req, // the Express Request object
    'access-log' // context info
));

/* You can use one of the builtin LogEntries or 
 * you can subclass one of them to add custom data 
 */

class MyLogEntry extends IpLogEntry {
    anotherProperty: any;
    
    constructor(context: string, ip: string, myCustomProperty: number) {
        // if you extend IpLogEntry class and you specified a GeoLite db, 
        // the ip will be parsed into geo location data
        super(context, ip, "MyLogEntry");
        
        // the body property is the one that will be sent to the logging service
        this.body.my_property = myCustomProperty;
        
        // or you can add new a new one and write a new driver to handle it
        this.anotherProperty = "Cheese Cake";
    }
}

A simple access-log middleware for Express servers

app.use((req, res, next) => {
    Logger.log(new AccessLogEntry(req, "access-log"));
    next();
});

Usage with the UDP Driver

const Logger = FlexibleLogger.withSync(
    new UdpDriver('localhost', 12345)
);

which requires a server listening on the given UDP port

const Server = new LogUdpServer(
    new ElasticSearchDriver({ ... }), 
    {
        port: 1234,
        address: 'localhost'
    }
);

Server.bind();
Server.poll();

The UDP server can be configured with the following options

export interface ServerConfig {
    /**
     * Port to bind the server on
     */
    port: number;
    /**
     * Address to bind the server on. Defaults to 0.0.0.0 (any)
     */
    address?: string;
    /**
     * If an error occurs, bind again the port after a timeout. Default is true
     */
    retryOnError?: boolean;
    /**
     * Timeout in ms to wait before retrying binding the port. Default is 10000
     */
    bindRetryTimeout?: number;
    /**
     * Rate in ms at which to flush the queue and send it to the service. Default is 20000
     */
    queuePollRate?: number;
    /**
     * Minimum amount of entries before the messages are sent to the service. Default is 1
     */
    logEntriesTreshold?: number;
    /**
     * If set to true, messages queue will always be flushed even if an error prevented them
     * from being sent to the service. Default is true
     */
    alwaysFlush?: boolean;
}