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-log4js

v0.1.6

Published

Scarlet plugin for using Log4js with method and property event interception

Downloads

38

Readme

scarlet-log4js

Scarlet plugin for using Log4js with method and property event interception

Build Status

##Install

npm install scarlet-log4js

##Start logging

var Scarlet = require('scarlet');
var scarlet = new Scarlet('../lib/scarlet-log4js');
var scarletLog4js = scarlet.plugins.log4js;

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

//Now use intercepted object with logging!
Math.min(1,2,3);
//->[2013-09-02 00:46:17.430] [INFO] Object - calling - Object::min(1,2,3)
//->[2013-09-02 00:46:17.432] [INFO] Object - Object::min(1,2,3) - returned:1 - execution time(0:0:0.0)

By default the name of the object being intercepted will be used as the logger name

function FunctionToLog(){};
var functionToLogInstance = new FunctionToLog();

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

//FunctionToLog is the name of the object being intercepted and is in the thrid [] of the message
[2013-08-31 11:15:59.965] [INFO] [FunctionToLog] someMessage

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-log4js --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-log4js');
var scarletLog4js = scarlet.plugins.log4js;

Motvation

Scarlet-log4js was created to allow applications to get the benefits of event based interception using scarlet and logging using log4js.

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

How do I Configure log4js?

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

scarletLog4js.logger(myCustomLogger).bindTo(functionToLog);

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

//getting instance used to log
scarletLog4js.log4js

//getting a new log4js logger and logging with it
var logger = scarletLog4js.getLogger();
logger.info("Important Log Message");

For more information on how to configure log4js 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:

scarletLog4js.beforeMethodCall = function(invocation){
	var logger = scarletLog4js.getLogger();
	logger.info( "Before Method Call");
};

scarletLog4js.afterMethodCall = function(invocation){
	var logger = scarletLog4js.getLogger();
	logger.info("After Method Call");
};

Examples

Start logging for an instance

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-log4js');
var scarletLog4js = scarlet.plugins.log4js;

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

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

//Now use intercepted object with logging!
functionToLogInstance.logMe();
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] calling - FunctionToLog::logMe()
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] In logMe
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] FunctionToLog::logMe() - returned:undefined - execution time(0:0:0.1)

Start logging for an instance with custom log4js configuration

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-log4js');
var scarletLog4js = scarlet.plugins.log4js;

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

//Create a custom log4js configuration
scarletLog4js.configure({
  appenders: [
    { type: 'console' },
    { type: 'file', filename: 'logs/functionToLog.log', category: 'FunctionToLog' }
  ]
});

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

//Now use intercepted object with logging!
functionToLogInstance.logMe();
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] calling - FunctionToLog::logMe()
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] In logMe
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] FunctionToLog::logMe() - returned:undefined - execution time(0:0:0.1)

Start logging an instances member

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-log4js');
var scarletLog4js = scarlet.plugins.log4js;

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

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

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

//Now use intercepted object with logging!
functionToLogInstance.logMe();
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] calling - FunctionToLog::logMe()
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] In logMe
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] 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-log4js');
var scarletLog4js = scarlet.plugins.log4js;

function FunctionToLog(){
   var logger = scarletLog4js.getLogger("FunctionToLog");
   this.logMe = function(){ logger.info("In logMe"); }
};

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

//Now use intercepted object with logging!
var functionToLogInstance = new FunctionToLog();
//-> Outputs the following to the console:
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] calling - FunctionToLog::FunctionToLog()
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] FunctionToLog::FunctionToLog() - returned:undefined - execution time(0:0:0.1)

functionToLogInstance.logMe();
//-> Outputs the following to the console:
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] calling - FunctionToLog::logMe()
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] In logMe
//->info: [2013-08-31 11:15:59.965] [INFO] [FunctionToLog] FunctionToLog::logMe() - returned:undefined - execution time(0:0:0.1)

###Start logging all instances of a prototype function

var Scarlet = require('scarlet');
var scarlet = new Scarlet('scarlet-log4js');
var scarletLog4js = scarlet.plugins.log4js;

//Define a prototype object to log
var ObjectToLog = function (){};
ObjectToLog.prototype.someMethod = function(){ 
   var logger = scarletLog4js.getLogger("ObjectToLog");
   logger.info("In logMe"); 
};
 
//Attach Logger to object
ObjectToLog = scarletLog4js.bindTo(ObjectToLog);
 
//Now use intercepted object 
var objectToLog = new ObjectToLog();
//-> Outputs the following to the console:
//->info: [2013-08-31 11:15:59.965] [INFO] [ObjectToLog] calling - ObjectToLog::ObjectToLog()
//->info: [2013-08-31 11:15:59.965] [INFO] [ObjectToLog] ObjectToLog::ObjectToLog() - returned:undefined - execution time(0:0:0.1)

//When called will now get logged
var result = objectToLog.someMethod();
//-> Outputs the following to the console:
//->info: [2013-08-31 11:15:59.965] [INFO] [ObjectToLog] calling - ObjectToLog::logMe()
//->info: [2013-08-31 11:15:59.965] [INFO] [ObjectToLog] In logMe
//->info: [2013-08-31 11:15:59.965] [INFO] [ObjectToLog] ObjectToLog::logMe() - returned:undefined - execution time(0:0:0.1)