Noteworthy Placebo Mongers

    funlog

    1.1.1 • Public • Published

    Build Status node npm

    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 = require("funlog");
     
    const func = function() {
      console.log("Hi");
    };
    const loggedFunction = funlog(func);

    The constructor also takes an additional options object, log and logger are the most important ones (see: options).

    const yourLogger = console;
    const yourLogFunction = function(logger,message) {
        logger.log(message);
    }
     
    const options = {
        log: yourLogFunction
        logger: yourLogger
    }

    Table of Contents

    1. Motivation
    2. Quick start
    3. Usage
    4. API
    5. Installation
    6. Testing
    7. Future
    8. Contributing

    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

    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:
    function(logger,message) {
        logger.info(message);
    }

    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:
    function(logger,message) {
        logger.error(message);
    }

    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 = require("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.

    Install

    npm i funlog

    DownloadsWeekly Downloads

    0

    Version

    1.1.1

    License

    MIT

    Unpacked Size

    19.9 kB

    Total Files

    18

    Last publish

    Collaborators

    • axblueblader