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

@hyperflake/logger

v1.1.9

Published

A simple Node.js logger module for logging messages with flexible & customizable transports.

Downloads

297

Readme

@hyperflake/logger

A simple Node.js logger module for logging messages with flexible & customizable transports.

Installation

npm install @hyperflake/logger

Usage

import { Logger, transports } from '@hyperflake/logger';

// Create a logger instance with a transport configuration
const logger = new Logger({
    transports: [
        new transports.Console(),
        new transports.MongoDB({
            uri: 'mongodb://localhost/logging',
            collectionName: 'app-logs',
        }),
    ],
});

// Log an info message
logger.log({
    message: "This is an info message",
});

// Log an error message
logger.error({
    message: "This is an error message",
});

// Log a warning message
logger.warn({
    message: "This is a warning message",
});

API

Logger

Constructor

new Logger<TLoggerData>(options: LoggerOptions);

Creates a new logger instance with the specified transport configuration.

  • options: Logger options.
    • transports: Array of transports to be used for logging.

Methods

  • log(entry: LogEntry): void: Logs an info message.
  • error(entry: LogEntry): void: Logs an error message.
  • warn(entry: LogEntry): void: Logs a warning message.

Transports

Console Transport

A transport that logs messages to the console.

new transports.Console(options?: ConsoleTransportOptions);
  • options: Options for the console transport.
    • formatterFn: (data: LogEntry<TLoggerData>) => string Function that formats log data into a string before outputting it to the console. (Optional).

MongoDB Transport

A transport that logs messages to MongoDB.

new transports.MongoDB(options: MongoDBTransportOptions);
  • options: Options for the MongoDB transport.
    • uri: MongoDB connection URI.
    • dbName: Name of the MongoDB database. Defaults to DB name based on connection URI if not provided. (Optional)
    • collectionName: Name of the collection to store logs.
    • capped: Whether to create the log collection as capped or not. Defaults to true. (Optional)
    • cappedSize: Size of logs capped collection in bytes. Defaults to 10,000,000. (Optional)
    • cappedMax: Size of logs capped collection in number of documents. (Optional)

Example

import { Logger, transports } from '@hyperflake/logger'

// Create a logger instance with console transport & additional data type
const consoleLogger = new Logger<{ userId: number }>({
    transports: [
        new transports.Console(),
    ],
});

// Log a message with additional data
consoleLogger.log({
    message: "This is a log message",
    userId: 100001,
});

// Create a logger instance with custom formatterFn
const customizedConsoleLogger = new transports.Console({
    formatterFn: (data) => {
        return `[${data.timestamp.toString()}] ${data.level} - ${data.message}`;;
    } 
});

// This is the default formatterFn in case no formatterFn is passed
const defaultFormatterFn = (data: LogData<TLoggerData>) => {
    return JSON.stringify(data);
}

// Create a logger instance with MongoDB transport
const mongoLogger = new Logger<{ token: string }>({
    transports: [
        new transports.MongoDB({
            uri: 'mongodb://localhost/logging',
            collectionName: 'app-logs',
        }),
    ],
});

// Log a message with additional data
mongoLogger.log({
    message: "This is a log message",
    token: '0wq4kWuo63DJaCmLC',
});

// Create a mongodb logger instance with custom options
const customizedMongoLogger = new Logger<{ token: string }>({
    transports: [
        new transports.MongoDB({
            uri: 'mongodb://localhost',
            dbName: 'logging',
            collectionName: 'app-logs',
            capped: true, // Defaults to true,
            cappedSize: 1024 * 1024,
            cappedMax: 100000 
        }),
    ],
});