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

simple-logging-helper

v1.0.8

Published

a helper tool for logging javascript in a browser

Downloads

2

Readme

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')