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

scarlet-winston

v0.1.4

Published

Scarlet plugin for using Winston with method and property event interception

Downloads

8

Readme

scarlet-winston

Scarlet plugin for using Winston with method and property event interception

Build Status

##Install

npm install scarlet-winston

##Start logging

var Scarlet = require('scarlet');
var scarlet = new Scarlet('../lib/scarlet-winston');
var scarletWinston = scarlet.plugins.winston;

//Attach Logger to object
scarletWinston.bindTo(Math,'min');

//Now use intercepted object with logging!
Math.min(1,2,3);
//->info: [Mon Sep 02 2013 00:49:58 GMT+0100 (BST)] calling - Object::min(1,2,3)
//->info: [Mon Sep 02 2013 00:49:58 GMT+0100 (BST)] Object::min(1,2,3) - returned:1 - execution time(0:0:0.0)

Getting Started

This plugin requires Scarlet ~2.0.x

If you haven't used Scarlet before, be sure to check out the Documentation. To use this plugin perform the following:

Install scarlet

npm install scarlet --save

Install plugin

npm install scarlet-winston --save

Once the plugin has been installed, you can use it in your application as follows:

//load scarlet
var Scarlet = require('scarlet');

//Initialize scarlet with the plugin
var scarlet = new Scarlet('scarlet-winston');
var scarletWinston = scarlet.plugins.winston;

Motvation

Scarlet-Winston was created to allow applications to get the benefits of event based interception using scarlet and robust full featured logging using winston.

Scarlets event based interception is asynchronous and gets events on methods/properties before, after, and on error. Scarlet-Winston listens for these events and logs them.

How do I Configure Winston?

You can easily override the default winston implementation by passing the custom configured winston logger to scarlet-winston as follows:

scarletWinston.logger(myCustomWinstonLogger).bindTo(functionToLog);

Also, scarletWinston extends winston so has all its methods, in addition you can access winston as follows:

scarletWinston.winston

For more information on how to configure winston please go here.

Custom messages during the before and after events.

If you want to customize the before and after event logs do the following:

scarletWinston.beforeMethodCall = function(invocation){
  scarletWinston.log('info', "Before Method Call");
};

scarletWinston.afterMethodCall = function(invocation){
  scarletWinston.log('info', "After Method Call");
};

Examples

Start logging for an instance

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-winston');
var scarletWinston = scarlet.plugins.winston;

//Define a function to log
function FunctionToLog(){
  this.logMe = function(){ console.log("In logMe"); }
};
var functionToLogInstance = new FunctionToLog();

//Attach Logger to object
scarletWinston.bindTo(functionToLogInstance);

//Now use intercepted object with logging!
functionToLogInstance.logMe();
//->info: [Tue Aug 27 2013 23:51:16 GMT+0100 (BST)]calling - FunctionToLog::logMe()
//->In logMe
//->info: [Tue Aug 27 2013 23:51:16 GMT+0100 (BST)]FunctionToLog::logMe() - returned:undefined - execution time(0:0:0.1)

Start logging for an instance with custom winston configuration

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-winston');
var scarletWinston = scarlet.plugins.winston;

//Define a function to log
function FunctionToLog(){
  this.logMe = function(){ console.log("In logMe"); }
};
var functionToLogInstance = new FunctionToLog();

//Create a custom winston configuration
var logger = new (winston.Logger)({
    transports: [
      new (winston.transports.Console)(),
      new (winston.transports.File)({ filename: 'somefile.log' })
    ]
  });

//Attach Logger to object
scarletWinston.logger(logger).bindTo(functionToLogInstance);

//Now use intercepted object with logging!
functionToLogInstance.logMe();
//->info: [Tue Aug 27 2013 23:51:16 GMT+0100 (BST)]calling - FunctionToLog::logMe()
//->In logMe
//->info: [Tue Aug 27 2013 23:51:16 GMT+0100 (BST)]FunctionToLog::logMe() - returned:undefined - execution time(0:0:0.1)

Start logging an instances member

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-winston');
var scarletWinston = scarlet.plugins.winston;

//Define a function to log
function FunctionToLog(){
  this.logMe = function(){ console.log("In logMe"); }
  this.dontLogMe = function(){ console.log("In Don't logMe"); }
};
var functionToLogInstance = new FunctionToLog();

//Attach Logger to object
scarletWinston.bindTo(functionToLogInstance,'logMe');

//Call a non intercepted method
functionToLogInstance.dontLogMe();
//-> no output

//Now use intercepted object with logging!
functionToLogInstance.logMe();
//->info: [Tue Aug 27 2013 23:51:16 GMT+0100 (BST)]calling - FunctionToLog::logMe()
//->In logMe
//->info: [Tue Aug 27 2013 23:51:16 GMT+0100 (BST)]FunctionToLog::logMe() - returned:undefined - execution time(0:0:0.1)

###Start logging all instances of a function

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-winston');
var scarletWinston = scarlet.plugins.winston;

function FunctionToLog(){
 this.logMe = function(){ console.log("In logMe"); }
};

//Attach Logger to object
FunctionToLog = scarletWinston.bindTo(FunctionToLog);

//Now use intercepted object with logging!
var functionToLogInstance = new FunctionToLog();
//-> Outputs the following to the console:
//->info: [Tue Aug 27 2013 09:39:55 GMT+0100 (BST)] - calling - FunctionToLog::FunctionToLog()
//->info: [Tue Aug 27 2013 09:39:55 GMT+0100 (BST)] - FunctionToLog::FunctionToLog() - returned:undefined - execution time(0:0:0.0)

functionToLogInstance.logMe();
//-> Outputs the following to the console:
//->[Tue Aug 27 2013 09:39:55 GMT+0100 (BST)] - Debug - calling - FunctionToLog::logMe()
//->In logMe
//->[Tue Aug 27 2013 09:39:55 GMT+0100 (BST)] - Debug - FunctionToLog::logMe() - returned:undefined - execution time(0:0:0.0)

###Start logging all instances of a prototype function

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-winston');
var scarletWinston = scarlet.plugins.winston;

//Define a prototype object to log
var ObjectToLog = function (){};
ObjectToLog.prototype.someMethod = function(){ console.log("In logMe"); };
 
//Attach Logger to object
ObjectToLog = scarletWinston.bindTo(ObjectToLog);
 
//Now use intercepted object 
var objectToLog = new ObjectToLog();
//-> Outputs the following to the console:
//->[Tue Aug 27 2013 09:50:23 GMT+0100 (BST)] - Debug - calling - FunctionToLog::FunctionToLog()
//-?[Tue Aug 27 2013 09:50:23 GMT+0100 (BST)] - Debug - FunctionToLog::FunctionToLog() - returned:undefined - execution time(0:0:0.1)

//When called will now get logged
var result = objectToLog.someMethod();
//-> Outputs the following to the console:
//->[Tue Aug 27 2013 09:50:23 GMT+0100 (BST)] - Debug - calling - FunctionToLog::logMe()
//->In logMe
//->[Tue Aug 27 2013 09:50:23 GMT+0100 (BST)] - Debug - FunctionToLog::logMe() - returned:undefined - execution time(0:0:0.0)