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

@origranot/ts-logger

v1.12.0

Published

A powerful and easy-to-use logging and debugging package for TypeScript utilizing decorators and reflection API for automatically capturing context and debugging information.

Downloads

372

Readme

:zap: ts-logger

GitHub Workflow Status npm bundle size GitHub

A logging library designed to simplify the process of logging in your TypeScript applications. With zero dependencies and features such as five log levels, custom transports, method decoration options, timestamps, and log level thresholding, you'll have greater control over your log output.

Our library also provides the flexibility to extend its functionality through custom transports, enabling you to meet the specific needs of your project. Say goodbye to cluttered and unorganized logs and get started with ts-logger today!💪

Features :star:

  • Supports logging at five different levels (:bug: DEBUG, :information_source: INFO, :warning: WARN, :exclamation: ERROR, :fire: FATAL).
  • Zero dependencies 🚫
  • Support custom transports and formatters to extend the functionality of the library. 💬
  • Support multiple log transports out of the box. 📦 (Console, UDP and File)
  • Ability to decorate class methods to log their arguments, return values, and execution time. 📊
  • Option to include timestamps in logs. 🕰️
  • Option to set log level threshold. 🎛️
  • Option to log execution time of decorated functions. ⏱️
  • Logs can be color-coded based on the log level. 🎨

Installation

npm install @origranot/ts-logger

Usage

Basic logger

import { Logger } from '@origranot/ts-logger';

const logger = new Logger();

logger.debug('Debug message');
logger.info('Info message');
logger.warn('Warn message');
logger.error('Error message');
logger.fatal('Fatal message', new Error('We have a problem here'));

logger.info('You can also log objects', {
  foo: 'baz'
});

Optional parameters

  • name (string): The name of the logger. (optional)
  • timestamps (Boolean): Whether to include timestamps in logs (default: true)
  • transports: Array of transports to process and log the data. (default: ConsoleTransport)
  • override: Override default options (see below)

Override default options

  • logLevelColors: override and customize the default log level colors.

    You can see the default log level colors in the table below:

    | LOG_LEVEL | COLOR | | --------- | --------- | | DEBUG | 🔵 BLUE | | INFO | 🟢 GREEN | | WARN | 🟡 YELLOW | | ERROR | 🔴 RED | | FATAL | ⚪ WHITE |

    The logLevelColors object is a map of the LOG_LEVEL enum to the COLOR enum.

    Note: You can find the COLOR enum in the src/utils/color.ts file.

    For example, to override the default color of the DEBUG log level to YELLOW we can do the following:

    const logger = new Logger({
      override: {
        logLevelColors: {
          [LOG_LEVEL.DEBUG]: COLOR.YELLOW
        }
      }
    });

Decorated functions example

import { Logger, LOG_LEVEL } from '@origranot/ts-logger';

const logger = new Logger();

class ExampleClass {
  @logger.decorate(LOG_LEVEL.INFO, { executionTime: true })
  public exampleMethod(a: number, b: number): number {
    return a + b;
  }
}

const example = new ExampleClass();
example.exampleMethod(1, 2);

/* 
  Output: [2022-01-01 00:00:00] INFO [exampleMethod]
  {
    args: [1, 2],
    returns: 3,
    executionTime: 3ms
  }
*/

Optional parameters

  • executionTime (Boolean): Whether to calculate and print the function execution time. (default: false)

Log transports

This library provides a few built-in log transports that can be used out of the box:

  • Console: This transport outputs log messages to the console.
  • Udp: this transport sends log messages via UDP protocol.
  • File: This transport writes log messages to a file on disk.

The built-in transports have a default formatter assigned to them (SimpleFormatter), but you can override it by passing a custom formatter to the transport constructor. Here's an example of how to use the built-in log transports:

import { Logger, ConsoleTransport, FileTransport } from '@origranot/ts-logger';

// Create an instance of the console logger
const consoleTransport = new ConsoleTransport();

/*
  Note: The file transport will create the file if it doesn't exist, and append to it if it does.
  We can also provide log rotation options to the file transport, which will automatically
  rotate the log file according to the date.
*/
const fileTransport = new FileTransport({ path: 'logs/app.log' });

const logger = new Logger({
  transports: [consoleTransport, fileTransport]
});

/*
  Log messages will be handled by both transports provided:
  - The console transport will output the log message to the console.
  - The file transport will write the log message to the file.
*/
logger.info('Application started');

Note: There is a file in the examples directory that demonstrates how to use the built-in UDP transport to send log messages to Splunk.

Optional parameters

  • formatter: An instance of a formatter to format the log message. (default: SimpleFormatter)
  • threshold: The log level threshold, logs with a lower level than the threshold will be ignored (default: 'DEBUG').

You can add multiple transports to a single logger, so that log messages can be sent to multiple outputs. You can also create custom log transports by extending the Transport class as shown below:

import { Logger, TransportPayload } from '@origranot/ts-logger';

export class CustomTransport extends Transport {
  constructor() {
    super();
  }

  handle(payload: TransportPayload) {
    console.log([`[CustomTransport] - ${payload.message}`]);
  }
}

Note: You can also create custom log formatters by implementing the Formatter interface.

Contributing

Contributions are welcome! For feature requests and bug reports, please open an issue.