Funlog
A customizable function-focused Nodejs log wrapper: Log functions input, output and everything in between.
Motivation
I wrote some handler functions for API routes and wanted to automatically log out inputs and outputs of all functions inside an object. The same need arised when trying to debug an application, so I decided to write this for daily usage, but also for functional programming practice and for fun as well - hence the name funlog
.
Quick start
Check out the basic usage and other examples in the examples folder. All examples can be run using
node /path/to/example/file.js
Usage
Simply import and put your functions into the constructor and save it to a variable or pass it as a parameter.
const funlog = ; const func = { console;};const loggedFunction = ;
The constructor also takes an additional options object, log
and logger
are the most important ones (see: options).
const yourLogger = console;const yourLogFunction = { logger;} const options = log: yourLogFunction logger: yourLogger
Table of Contents
API
Constructor
The constructor takes in an object or a function as the first parameter and an optional options
object as the second parameter.
If the first parameter is an object, the constructor returns a new object, preserving the original fields, but replace functions in that object with a logging wrapper function.
If the first parameter is a function, the constructor returns a new function, which is a wrapper around the original function.
It will return null
in all other cases.
Syntax
const variable = require("funlog")(element,options)
Parameters
-
element : can be of type object or function
- Required.
-
options: must be of type object
- Optional. All default options can be view in the defaultOptions.js file
options
Properties in the options
are not currently checked for types and unknown ones are ignored.
logger
A logger object, something that could print to an output. It could be the console or any other logger you prefer. If it has an info
method for printing then you can plug it in. If it doesn't you can still configure a logging function in log.
- Default value:
console
log
A logging function. It needs to take in 2 parameters (logger,string). This function will be called for printing.
- Default value:
{ logger;}
preEx
A message to print after the function name message and before the printing the arguments.
- Default value:
"Input: "
durEx
A message to print just before the original function gets executed.
- Default value:
"Process: "
postEx
A message to print after execution finished and before printing function output.
- Default value:
"Output: "
logErr
A logging function specifically for exceptions. It needs to take in 2 parameters (logger,string). This function will be called for printing exceptions.
- Default value:
{ logger;}
reThrowErr
A boolean to indicate that the wrapper will rethrow any exceptions out or swallow them.
- Default value:
true
Installation
npm install funlog
In source code
const funlog = ;
is enough to get going.
Testing
This project uses Mocha
, nyc
, sinon
and chai
for testing and coverage.
Simply run
npm test
or
npm run coverage
Future
Hopefully will add support for asynchronous functions, better formatting, customization abilities and better exception handling.
Contributing
If you have any trouble or have some suggestions, don't hesitate to either create a new issue, open a new pull request or just email me at nguyenqviet98@gmail.com. All feedbacks are welcomed.