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

funlog

v1.1.1

Published

A customizable function-foused logger: Log input, output and everything in between

Downloads

7

Readme

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 [email protected]. All feedbacks are welcomed.