Socket
Socket
Sign inDemoInstall

event2log

Package Overview
Dependencies
0
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Install Socket

Detect and block malicious and high-risk dependencies

Install

    event2log

Translates events to human-readable log


Version published
Maintainers
1
Install size
8.20 kB
Created

Readme

Source

event2log

Little helper module for translating events into log messages.

Will perfectly work with NodeJS 4.0.0 and above.

Example

'use strict';

const events = require( 'events' );
const event2log = require( 'event2log' );


// First we create a dummy instance. It might be anything emitting events.
// This is the instance you are willing to observe and log upon emitted events.
let instanceThrowingEvents = new events.EventEmitter();

// The translation table converts events into human-readable strings.
// The field name corresponds to an event name. The field itself accommodates a
// function that receives the event arguments and alway will return an array!
// The first argument of that array names a logging method and the following
// items will be passed to logging method as arguments.
const translationTable = {
	'event1': ( a, b ) => [ 'log',   `Hello ${a}! Are you afraid of ${b}?` ],
	'event2': ( c )    => [ 'error', `${c} is watching you!` ]
};

// The following statement bundles everything together. For testing purposes it
// just prints stuff to the console. But you also could replace the 'console' by
// something more sophisticated (or bloated?) like 'winston'.
let e2lHandler = event2log( translationTable, instanceThrowingEvents, console );


instanceThrowingEvents.emit( 'event1', "World", "Chuck Norris" );
// Will print to stdout: Hello World! Are you afraid of Chuck Norris?

instanceThrowingEvents.emit( 'event2', "Big Brother" );
// Will print to stderr: Big Brother is watching you!


// Calling the returned e2lHandler releases all listening events bound by
// event2log.
e2lHandler();

instanceThrowingEvents.emit( 'event1', "Universe", "human beings" );
// Won't print anything to the console.

Keywords

FAQs

Last updated on 18 Apr 2016

Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc