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 🙏

© 2025 – Pkg Stats / Ryan Hefner

event-system

v1.0.2

Published

Event class system for systematize event logging

Downloads

14

Readme

Node.js Event class system

Event class system has been developed for systematize event logging. You have three base classes:

  • ErrorEvent;
  • InfoEvent;
  • WarningEvent.

All classes can be found at EventSystem.events. Each class can be inherited by calling static method .extend from class. Each class can be instantiated via new.

Quick start example

Install it

npm install --save event-system

Require and use

const EventSystem = require("event-system");

EventSystem.setOutput(EventSystem.events.ErrorEvent, process.stderr); // setting output stream
let myError = new EventSystem.events.ErrorEvent(); // creating error event
EventSystem.log(myError); // logging event

As result, you will see same output:

Tue Jul 04 2017 12:27:34 GMT+0300 (MSK) - Error:
Stacktrace:
        at constructor.BaseEvent (/home/bymsx/Documents/logger/lib/logger/lib/events.js:12:29)
        at new constructor (/home/bymsx/Documents/logger/lib/logger/lib/events.js:38:23)
        at Object.<anonymous> (/home/bymsx/Documents/logger/test.js:4:15)
        at Module._compile (module.js:571:32)
        at Object.Module._extensions..js (module.js:580:10)
        at Module.load (module.js:488:32)
        at tryModuleLoad (module.js:447:12)
        at Function.Module._load (module.js:439:3)
        at Module.runMain (module.js:605:10)
        at run (bootstrap_node.js:422:7)
        at startup (bootstrap_node.js:143:9)
        at bootstrap_node.js:537:3

Note: header of that message will be red.

Inheritance and event fields

Events have fields which describe event.

| Name | Type | Description | Inheritance | |-------------|--------|----------------------------|:----------:| | header | String | Name of event | increases | | description | String | Short description of event | replaces | | timestamp | Date | Time when event instantiated | - | | stack | Array | Stacktrace | - | | data | Object | Additional data, you can use it as you want | increases |

Column 'Inheriting' means that field can be replaced, such as description and increased such as name. Here's an example:

| Value | FileSystemErrorEvent | FileNotFoundErrorEvent | | ----------------- | --------- | ------------ | | header | 'Error FileSystem' | 'Error FileSystem FileNotFound' | | description | 'Error in IO' | 'File not found' | | data | { someData: true } | { someData: true, code: -1 } |

As you see, header has a name from base class and the name from child class, provided to .extend.

Event's data

Field data has type Object and designed as storage for any information about event, such as filename of file not found error. It increases by inheritance and by constructor. Here's an example:

const EventSystem = require("event-system");
const fs = require("fs");

const FileSystemErrorEvent = EventSystem.events.ErrorEvent.extend('FileSystem', 'Error in IO', { someData: true });
const FileNotFoundErrorEvent = FileSystemErrorEvent.extend('FileNotFound', 'File not found', { code: -2 });

EventSystem.setOutput(EventSystem.events.ErrorEvent, process.stderr);

fs.readFile('UNKNOWN_PATH', function (err, data) {
    if (err) {
        EventSystem.log(new FileNotFoundErrorEvent({ filename: err.path }));
    } else {
        // do anything else
    }
});

In result, data will contains:

{ someData: true, code: -1, filename: 'UNKNOWN_PATH' }

So, if you needn't provide data into constructor, you can call it without arguments:

new FileNotFoundErrorEvent();

Also you can call .extend without data:

const FileSystemErrorEvent = EventSystem.events.ErrorEvent.extend('FileSystem', 'Error in IO');

BaseEvent#serialize

Serialize method called to transform data field before output. By defaults, .serialize returns JSON-string, generated from data. You can override this function when calls .extend. Here's example:

function FileSystemSerialize () {
    return `File: ${this.data.filename}, code: ${this.data.code}`;
}

const FileSystemErrorEvent = EventSystem.events.ErrorEvent.extend('FileSystem', 'Error in IO', FileSystemSerialize);

Now you can instantiate event, call log and in output you will see you prepared message instead of JSON-string.

BaseEvent#extend

This function accepts four arguments:

  • header;
  • description;
  • serialize;
  • data.

Header and description are mandatory arguments, but serialize and data you can combine in any order.

FileSystemErrorEvent = EventSystem.events.ErrorEvent.extend('FileSystem', 'Error in IO', { someData: true }, function () {}); // works fine

or

FileSystemErrorEvent = EventSystem.events.ErrorEvent.extend('FileSystem', 'Error in IO', function () {}, { someData: true }); // also works fine

Output and logging

Event system provides a logger for events.

EventSystem#setOutput

You can set output stream for each of three base classes. Function setOutput has two parameters:

  • Base class;
  • Output stream. Output stream must be an instance of Stream.Writable. Here's an example.
const fs = require("fs");

EventSystem.setOutput(EventSystem.events.ErrorEvent, process.stderr);
EventSystem.setOutput(EventSystem.events.WarningEvent, process.stderr);
EventSystem.setOutput(EventSystem.events.InformationEvent, fs.createWriteStream('./events.log'));

If you call setOutput with invalid arguments, it will throw an Error.

EventSystem#log

This function accepts one argument - instance of event. It logs information about event into stream, which specified for this type of event. If output stream isn't specified, event won't logged. If argument isn't instance of Event (or inherited), it will throw an Error. Example:

EventSystem.log(new EventSystem.events.ErrorEvent());

EventSystem#setUseColors

Here you can enable or disable colored output. Available modes are: true (enabled) and false (disabled). You can set it for all output and for each stream severally.

EventSystem.setUseColors(true); // Enable colors for ALL output
EventSystem.setUseColors(EventSystem.events.ErrorEvent, false); // Disable colors for ErrorEvent's output

If you call setUseColors with invalid arguments, it will throw an Error.

EventSystem#setPrintStackTrace

Same as #setUseColors, but for stacktrace.

EventSystem.setPrintStackTrace(true); // Enable stacktrace for all output
EventSystem.setPrintStackTrace(EventSystem.events.ErrorEvent, false); // Disable stacktrace for ErrorEvent's output

Testing

For tests you can run npm test. If you need extra output, follow the mocha documentation.

Bugs, Wishes, etc.

Now I haven't any bugtracker, so you can contact me by e-mail: [email protected]

I will be glad to see your feedback.