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

loghandler

v1.1.12

Published

Fully tested and strongly typed flexible Log Handler for all NodeJS projects.

Downloads

114

Readme

LogHandler

npm versionnpmCodacy BadgeCodacy BadgeKnown Vulnerabilities

A fully tested strongly typed (typescript), and modular log handler that gives you easy the possibility to send your applications log messages in a async way to every system you prefer. LogHandler can be used in every nodeJS project, no matter of typescript is on top of it.

How it works

LogHandler uses a reporters to report log items to certain destinations like console, syslog, rollbar, trackjs. To make this plugin as modular as possible I didn't add any reporters into this plugin. Seperate reporters need to be installed or self developed before LogHandler is capable to report log items. Further down in this readme I provide you with a full lists of publicly available reporters. Beside that you can find some instructions about how to create your own reporter even in the bottom of this readme.

Installation

You can install LogHandler pretty easy. Just by installing the package with node package manager. For Typescript users: The definitions are provided in the plugin. To install the plugin:

npm install --save loghandler

Usage

import loghandler from 'loghandler'

const config = {
  reporters: []
  reporting: {
    silent: false
    minimalLevel2Report: 'debug'
  }
}

const log = loghandler(config)


try{
  throw new Error("Something goes wrong!")
}catch(err){
  log.emerg(err)
}

Options

| Name | Default | Description | | ------------------------------- | ------- | ----------------------------------------------------------------------------------------- | | reporters | [] | list of all reporters | | reporting.silent | false | if true loghandler doesn't send logevent to all reporters. | | reporting.minimalLevel2Report | debug | the minimal logging level that any logevent should have before the reportes got notified. |

Commands Loghandler implements the syslog Message Serverties as they are provided in RFC 5424. This means that you're able to log events on different levels depanding on impact of the event. In LogHandler each severty level has there own command. In combination with the setting reporting.minimalLevel2Report you can add debug logevents in your application without that the reporters will be notified in a production environment. The full list of commands is:

See the list below:

| Code | Command | Description | | ---- | --------- | ---------------------------------------- | | 0 | emerg | Emergency: system is unusable | | 1 | alert | Alert: action must be taken immediately | | 2 | crit | Critical: critical conditions | | 3 | err | Error: error conditions | | 4 | warning | Warning: warning conditions | | 5 | notice | Notice: normal but significant condition | | 6 | info | Informational: informational messages | | 7 | debug | Debug: debug-level messages |

The input arguments of each command is the same. Each command must have at least a string input or Error message, but other attributes can be provided as well. Each command has the following format: logHandler.emerg(msg: string | Error, data?: {[key: string]: any}, ...args: any[]) => void

Typescript For the developers who want to use LogHandler in combination with Typescript. I exported the general definitions that you need to setup LogHandler. There are two defitions available:

| definition | Description | | ----------- | ------------------------------------------ | | Config | Interface with all available config values | | LogLevels | Literal Type with all available log levels |

Reporters

As explained before. LogHandler use reporters to report logevents to certain systems. LogHandler itself can't report logevents without reporters. So it's important in every installation that you setup at least 1. I tried to make it as easy as possible to make your own reporters, but it's ofcourse okay to use one that is already publicly available.

How to create your own reporters

To make your own reporter isn't hard at all. I tried to make it as easy as possible so that everybody could implement their own way of logging. To create your own reporter you need to make a reporter. How to make it differs a little of you using JavaScript or Typescript. For each language here an example:

Javascript:

class reporter {
  constructor(){
    this.name = 'Example reporter'
    this.timeout = 2500
  }

  async log(logObj) {
    // Put here your reporter code
    console.log(obj)
  }
}

Typescript:

class reporter implements ReportersInterface {
  public readonly name = 'Example reporter'
  public readonly timeOut = 2500

  async log(obj: LogObjectInterface) {
    // Put here your reporter code
    console.log(obj)
  }
}

However i think the examples speak for themself i want to explain you certain things. First all reporters need a name. This to report an error to other setted reporters when something goes wrong in your own developed reporter. Second there is an option to set a timeOut (ms) for eacht reporter. The timeOut is optional, but it is there to make sure that other reporters get informed as soon when the connection establishment to a tirth party takes to long.

Thirth you see that the reporters class requires an log method. This is the method where the magic happens. The method has one argument what is the complete log event object. The log event object looks like:

| key | type | Description | | --------- | ------ | --------------------------------------------------------------------------------------------------------------------------------------------------------- | | level | number | The numeric code of the log level See table 2 | | error | Error | The error message that is send to logHandler. If log is given as a string. Loghandler transforms it to an Error to make sure the stacktrace is available. | | data | {} | The data object. (second argument of any logHandler command) | | args | [] | An array of all other arguments that are given to LogHandler | | createdAt | Date | The exact date when LogHandler received the logevent |

How to enable a reporter

Enabling a reporter that you downloaded or just created by yourself isn't hard. To add a reporter to the list of reporters that are used by loghandler can be easly done by adding the reporter to the list of reporters in the Loghandler options. Emample:

import loghandler from 'loghandler'
import example_reporter from 'example_reporter'

const config = {
  reporters: [
    new example_reporter(),
  ]
  reporting: {
    silent: false
    minimalLevel2Report: 'debug'
  }
}

const log = loghandler(config)


try{
  throw new Error("Something goes wrong!")
}catch(err){
  log.emerg(err)
}

List of publicly available reporters

As far as I know there aren't any publicly available reporters jet. I'm happy to add your reporter here as soon when you make it open source and publicly available. If you did just make a Pull Request with and change this readme and don't forget to add yourself to the Contributers ;-).

Contributers

Michel Bitter (author) Codacy Badger (bot)