scarlet-log4js
Scarlet plugin for using Log4js with method and property event interception
Install
npm install scarlet-log4js
Start logging
var Scarlet = ;var scarlet = '../lib/scarlet-log4js';var scarletLog4js = scarletpluginslog4js; //Attach Logger to objectscarletLog4js; //Now use intercepted object with logging!Math;//->[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 scarletvar Scarlet = ; //Initialize scarlet with the pluginvar scarlet = 'scarlet-log4js';var scarletLog4js = scarletpluginslog4js;
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;
Also, scarletLog4js extends log4js so has all its methods, in addition you can access log4js as follows:
//getting instance used to logscarletLog4jslog4js //getting a new log4js logger and logging with itvar logger = scarletLog4js;logger;
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{ var logger = scarletLog4js; logger;}; scarletLog4js{ var logger = scarletLog4js; logger;};
Examples
Start logging for an instance
var Scarlet = ;var scarlet = 'scarlet-log4js';var scarletLog4js = scarletpluginslog4js; //Define a function to log{ this{ var logger = scarletLog4js; logger; }};var functionToLogInstance = ; //Attach Logger to objectscarletLog4js; //Now use intercepted object with logging!functionToLogInstance;//->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 = ;var scarlet = 'scarlet-log4js';var scarletLog4js = scarletpluginslog4js; //Define a function to log{ this{ var logger = scarletLog4js; logger; }};var functionToLogInstance = ; //Create a custom log4js configurationscarletLog4js; //Attach Logger to objectscarletLog4js; //Now use intercepted object with logging!functionToLogInstance;//->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 = ;var scarlet = 'scarlet-log4js';var scarletLog4js = scarletpluginslog4js; //Define a function to log{ var logger = scarletLog4js; this{ logger; } this{ logger; }};var functionToLogInstance = ; //Attach Logger to objectscarletLog4js; //Call a non intercepted methodfunctionToLogInstance;//-> no output //Now use intercepted object with logging!functionToLogInstance;//->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 = ;var scarlet = 'scarlet-log4js';var scarletLog4js = scarletpluginslog4js; { var logger = scarletLog4js; this{ logger; }}; //Attach Logger to objectFunctionToLog = scarletLog4js; //Now use intercepted object with logging!var functionToLogInstance = ;//-> 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;//-> 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 = ;var scarlet = 'scarlet-log4js';var scarletLog4js = scarletpluginslog4js; //Define a prototype object to logvar {};ObjectToLogprototype{ var logger = scarletLog4js; logger; }; //Attach Logger to objectObjectToLog = scarletLog4js; //Now use intercepted object var 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 loggedvar result = objectToLog;//-> 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)