scarlet-contrib-logger
Scarlet plugin for a simple logger
Install
npm install scarlet-contrib-logger
Start logging
var Scarlet = ;var scarlet = '../lib/scarlet-contrib-logger';var scarletLogger = scarletpluginslogger; //Attach Logger to objectscarletLogger; //Now use intercepted object with logging!Math;//->[Mon Sep 02 2013 01:07:05 GMT+0100 (BST)] - Debug - calling - Object::min(1,2,3)//->[Mon Sep 02 2013 01:07:05 GMT+0100 (BST)] - Debug - Object::min(1,2,3) - returned:1 - execution time(0:0:0.0)
Getting Started
This plugin requires Scarlet ~0.5.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-contrib-logger --save
Once the plugin has been installed, you can use it in your application as follows:
//load scarletvar Scarlet = ; //Initialize scarlet with the pluginvar scarlet = 'scarlet-contrib-logger';var scarletLogger = scarletpluginslogger;
Project Purpose
This project uses Scarlet to show how logging and interception can be easily integrated into your project.
Instead of having to write code that mixes in logging and application logic like this:
{ this{ console; //do important stuff console; }}
Using scarlet logger you can attach every instance of a function to the logger, and get rid of extra logging code.
var Scarlet = ;var scarlet = 'scarlet-contrib-logger';var scarletLogger = scarletpluginslogger; { this{ //do important stuff }}//Attach the logger to the functionFunctionToLog = scarletLogger;//-> now any instances of FunctionToLog will log.
This can also be helpful in debugging where you just want to find out what is getting called when. Just attach the logger to an instance like so:
scarletLogger;someInstance;////-> someFunction will now be logged, along with subsequent calls to methods on *someinstance*
Scarlet is a javascript interceptor that allows access to important information about methods, object names, method names. In addtion, it allows Scarlet Logger get access to a method before and after it is called.
Using Your own appender
Scarlet logger uses a console appender as default to use a different appender just do the following when initializing scarlet logger:
scarletLogger;
by calling the appender method with your custom appender the logger will use that instead of the default console appender.
A custom appender must have a append method. Here is an example as follows:
var CustomAppender = moduleexports = {}; CustomAppenderprototype{ //do something custom with log message};
Using Your own Log Interceptor
Scarlet logger uses a log interceptor that logs specfic details; execution time, function names, etc. If you want to use your own log interceptor do the following:
scarletLogger;
by calling the interceptor method with your custom interceptor the logger will use that when intercepting methods.
A custom interceptor must be a Scarlet interceptor. Here is an example of a simple custom interceptor:
var { console; ; console;}
The above doesn't use an appender and is a very basic interceptor.
Examples
Start logging a single instance
//Init ScarletLoggervar Scarlet = ;var scarlet = 'scarlet-contrib-logger';var scarletLogger = scarletpluginslogger; //Define a function to log{ this{ console; }};var functionToLogInstance = ; //Attach Logger to objectscarletLogger; //Now use intercepted object with logging!functionToLogInstance;//-> 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 function
//Init ScarletLoggervar Scarlet = ;var scarlet = 'scarlet-contrib-logger';var scarletLogger = scarletpluginslogger; { this{ console; }}; //Attach Logger to objectFunctionToLog = scarletLogger; //Now use intercepted object with logging!var functionToLogInstance = ;//-> Outputs the following to the console://->[Tue Aug 27 2013 09:39:55 GMT+0100 (BST)] - Debug - calling - FunctionToLog::FunctionToLog()//->[Tue Aug 27 2013 09:39:55 GMT+0100 (BST)] - Debug - FunctionToLog::FunctionToLog() - returned:undefined - execution time(0:0:0.0) functionToLogInstance;//-> 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
//Init ScarletLoggervar Scarlet = ;var scarlet = 'scarlet-contrib-logger';var scarletLogger = scarletpluginslogger; //Define an prototype object to logvar {};ObjectToLogprototype{return 1;}; //Attach Logger to objectObjectToLog = scarletLogger; //Now use intercepted object var 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 loggedvar result = objectToLog;//-> 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)