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

log4js-function-designation-wrapper

v6.0.0

Published

A simple wrapper for log4js that provides module and function name information in logs without having to define more than one configured logger.

Downloads

3

Readme

log4js-function-designation-wrapper

A simple wrapper for log4js that provides module and function name information in logs without having to define more than one configured logger. It adds the calling module and function names into the resulting log entry by default but can be overridden to provide contextual information for annoymous functions, callback functions and IIFEs.

For normal logging scenarios, you can just call the wrapper as you would the underlying log4js object. For logging from annoymous functions you can override the module designation and / or function designation, as well as the arguments for the underlying log4js function. Please see below for example usage.

  • log4js-function-designation-wrapper v0.0.1 - works with Log4js versions below v2.0.0
  • log4js-function-designation-wrapper v6.0.0 and above - designed to work with Log4js v6.4.0 and above

Install

npm install log4js-function-designation-wrapper

Usage


var log4js = require('log4js');
var log4jsWrapper = require('./log4js-function-designation-wrapper');

// one confgured logger
// you would usually set this up in your app.js or app initialisation module
global.logger = log4js.getLogger();

// module level logger
var logger = log4jsWrapper(global.logger, "module-name");

// do some logging using a named function expression
var logSome = function logSome(){
    logger.trace('trace');
    logger.debug('debug');
    logger.info('info');
};

// do some more logging using a named function expression
var logSomeMore = function logSomeMore(){
    logger.warn('warn');
    var error = new Error('test');
    error.code = 'fatal';
    logger.error(error);
    logger.fatal('fatal');
    logger.mark('mark');
}

// do some logging using a annoynmous function expression
var logFromAnnonFn = function(){
    var err = new Error('test');
    // override the function designation so this annoymous function can be
    // identified easier in the log
    logger.error({ functionDesignation: "my-annonymous-fn", arguments: ["Failed to do something.", err] });
};

// do some more logging using a annoynmous function expression
var logFromAnnonFn2 = function(){
    var err = new Error('test');
    // override the module and function designations for this log - useful for logging from
    // anonymous or named callbacks that were set up in an initialisation module
    logger.error({ moduleDesignation: "my-custom-module", functionDesignation: "my-annonymous-fn-2", arguments: ["Failed to do something.", err] });
};

logSome();
logSomeMore();
logFromAnnonFn();
logFromAnnonFn2();

// log from an IIFE
(function(){
    var obj = { name: "Lord British", hitpoints : 255 };
    // when overriding, you can pass arguments to the underlying log function via this arguments property,
    // it can be an array of things or a single thing
    logger.debug({ functionDesignation: "my-IIFE", arguments: "Lord British is here." });
    logger.warn({ functionDesignation: "my-IIFE", arguments: ["Failed to do something.", obj] });
}());

Example Output


[2016-07-15 15:35:07.397] [TRACE] [default] - module-name : [logSome] : trace
[2016-07-15 15:35:07.404] [DEBUG] [default] - module-name : [logSome] : debug
[2016-07-15 15:35:07.405] [INFO] [default] - module-name : [logSome] : info
[2016-07-15 15:35:07.406] [WARN] [default] - module-name : [logSomeMore] : warn
[2016-07-15 15:35:07.406] [ERROR] [default] - module-name : [logSomeMore] : { [Error: test] code: 'fatal' }
Error: test
    at logSomeMore (/Users/retroburst/Development/log4js-function-designation-wrapper/test.js:21:17)
    at Object.<anonymous> (/Users/retroburst/Development/log4js-function-designation-wrapper/test.js:45:1)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3
[2016-07-15 15:35:07.414] [FATAL] [default] - module-name : [logSomeMore] : fatal
[2016-07-15 15:35:07.414] [MARK] [default] - module-name : [logSomeMore] : mark
[2016-07-15 15:35:07.415] [ERROR] [default] - module-name : [my-annonymous-fn] : Failed to do something. [Error: test]
Error: test
    at logFromAnnonFn (/Users/retroburst/Development/log4js-function-designation-wrapper/test.js:30:15)
    at Object.<anonymous> (/Users/retroburst/Development/log4js-function-designation-wrapper/test.js:46:1)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3
[2016-07-15 15:35:07.416] [ERROR] [default] - my-custom-module : [my-annonymous-fn-2] : Failed to do something. [Error: test]
Error: test
    at logFromAnnonFn2 (/Users/retroburst/Development/log4js-function-designation-wrapper/test.js:38:15)
    at Object.<anonymous> (/Users/retroburst/Development/log4js-function-designation-wrapper/test.js:47:1)
    at Module._compile (module.js:435:26)
    at Object.Module._extensions..js (module.js:442:10)
    at Module.load (module.js:356:32)
    at Function.Module._load (module.js:313:12)
    at Function.Module.runMain (module.js:467:10)
    at startup (node.js:136:18)
    at node.js:963:3
[2016-07-15 15:35:07.416] [DEBUG] [default] - module-name : [my-IIFE] : Lord British is here.
[2016-07-15 15:35:07.416] [WARN] [default] - module-name : [my-IIFE] : Failed to do something. { name: 'Lord British', hitpoints: 255 }