1.0.8 • Published 1 year ago

simple-logging-helper v1.0.8

Weekly downloads
-
License
ISC
Repository
-
Last release
1 year ago

Simple Logging Helper

Find on NPM

Why?

Because other logging tools do the console.log themselves, and therefore the browser dev tools prints a line number for the line within the logging tool and not the line in your code!

This tool doesn't do the console.log for you, it just creates a mechanism to control switching the logging on and off. That means you can still see what line called the log, and can easily navigate to it in the dev tools and set a breakpoint, etc.

Once initialized you have access to boolean methods for different log levels:
|Level | key | boolean fn | |---|---|---| |Trace | TRACE | shouldLogTrace | |Debug | DEBUG | shouldLogDebug | |Info | INFO | shouldLogInfo | |Warn | WARN | shouldLogWarn | |Error | ERROR | shouldLogError |

You put these in front of the console.log() statements in your code, like logger.shouldLogTrace() && console.log('trace') and then you can switch (for example) trace logs on and off with __LOGGING.MyModule.TRACE = true. (See below)

Usage

Installation

npm install simple-logging-helper

Initialize

At one place, when your app starts

import simpleLoggingHelper from 'simple-logging-helper';

// simple
simpleLoggingHelper.init()
  
// with options
simpleLoggingHelper.init({
  enableAllByDefault: true,
  keyOnWindow:'__My_APP_LOGGING',
  windowRef: someOtherObjectThatIsNotWindow,
})

init(opts) takes an optional argument with these keys:

  • keyOnWindow (string, defaults to '__LOGGING') - The object name on the window that is used to toggle logging on and off
  • windowRef (object, defaults to window) - If you want to bind to an object other than window, specify it here.
  • enableAllByDefault (boolean) - If set to true then all toggles will be set to true by default, unless overwritten when specified for a namespace.

(If you want to specify a different windowRef but use the default keyOnWindow pass undefined first, like simpleLoggingHelper.init(undefined, someOtherObjectThatIsNotWindow))

Within each module

Set up the logger like this

// in all different places you want to use it, create a logger based on a namespace
import simpleLoggingHelper from 'simple-logging-helper';

const logger = simpleLoggingHelper.createForNamespace('MyModule')

// OR: you can optionally add defaults
const logger = simpleLoggingHelper.createForNamespace('MyModule', {DEBUG: true, WARN: true})

createForNamespace(namespace, defaults) takes 2 arguments:

  • namespace (string, mandatory) - The unique name for the module that will be used as a reference
  • defaults (object, optional) - An object of booleans for the keys: TRACE, DEBUG, INFO, WARN, ERROR. Anything not specified will be set to false.

Where you want to do logging

logger.shouldLogTrace() && console.log(logger.prefix(), `About to do something tiny`);
doSomethingTiny();

When you run this (and have set no defaults), nothing will be logged to the console when this line is encountered, because the base defaults are for all logging to be turned off.

However, if you go into the console while your app is running and type this:

window.__LOGGING.MyModule.TRACE = true

... then subsequent calls to logger.shouldLogTrace() will now return true, and the logging will be printed! The logs will look like this:

MyModule: About to do something tiny

And they will still have the correct line number that points to your code! (By the way, logger.prefix() just returns ${namespace}:, so in this case "MyModule: ".)

You can switch logging back off again the same way:

window.__LOGGING.MyModule.TRACE = false

Different Log Levels in Different Modules

The pattern above applies for all the log levels, so you can toggle different log levels on and off for different parts of your app using the key for the log level and the namespace value you specified:

window.__LOGGING.MyModule.TRACE = true
window.__LOGGING.ADifferentModule.DEBUG = true

Using Object Spread

Make your code more readable by only getting the methods you need with object spread:

const { shouldLogTrace, prefix: logPrefix } = simpleLoggingHelper.createForNamespace('MyModule')

// and then
shouldLogTrace() && console.log(logPrefix(), 'I am a trace')
1.0.8

1 year ago

1.0.7

1 year ago

1.0.6

1 year ago

1.0.5

1 year ago

1.0.4

1 year ago

1.0.3

1 year ago

1.0.2

1 year ago

1.0.1

1 year ago

1.0.0

1 year ago