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

better-logging-base

v1.3.0

Published

> Utility to produce better logging statements including level management

Downloads

743

Readme

MIT License Build Status Code Climate Codacy Badge Coverage Status

#better-logging-base


better-logging-base has optional dependencies on momentjs and sprintf.js: without moment you can't pattern a nicely readable datetime stamp and without sprintf you can't pattern your logging input lines. Default fixed patterns are applied if either they are missing.

Will be implemented

Include logger.js, momentjs and sprintf.js in your web app.

todo...

By default, the prefix is formatted like so:

datetime here::[context's name here]>your logging input here

However, you can change this as follows:

todo...

If you have included moment.js in your webapp, you can start using datetime stamp patterns with better-logging-base. The default pattern is dddd h:mm:ss a, which translates to Sunday 12:55:07 am. You customize the pattern as follows:

todo...

This way you can switch to a 24h format this way as well, for example, or use your locale-specific format.

If you have included sprintf.js in your webapp, you can start using patterns with better-logging-base.

Traditional style with console:

console.error ("Error uploading document [" + filename + "], Error: '" + err.message + "'. Try again later.")
// Error uploading document [contract.pdf], Error: 'Service currently down'. Try again later. "{ ... }"

Modern style with better-logging-base enhanced console:

console.error("Error uploading document [%s], Error: '%s'. Try again later.", filename, err.message)
// Sunday 12:13:06 pm::[myapp.file-upload]> Error uploading document [contract.pdf], Error: 'Service currently down'. Try again later.

You can even combine pattern input and normal input:

logger.warn("This %s pattern %j", "is", "{ 'in': 'put' }", "but this is not!", ['this', 'is', ['handled'], 'by the browser'], { 'including': 'syntax highlighting', 'and': 'console interaction' });
// 17-5-2015 00:16:08::[test]>  This is pattern "{ 'in': 'put' }" but this is not! ["this", "is handled", "by the browser"] Object {including: "syntax highlighting", and: "console interaction"}

To log an Object, you now have three ways of doing it, but the combined solution shown above has best integration with the browser.

logger.warn("Do it yourself: " + JSON.stringify(obj)); // json string with stringify's limitations
logger.warn("Let sprintf handle it: %j", obj); // json string with sprintf's limitations
logger.warn("Let the browser handle it: ", obj); // interactive tree in the browser with syntax highlighting
logger.warn("Or combine all!: %s, %j", JSON.stringify(obj), obj, obj);

working demo

Using logging levels, we can manage output on several levels. Contexts can be named using dot '.' notation, where the names before dots are intepreted as groups or packages.

For example for 'a.b' and a.c we can define a general log level for a and have a different log level for only 'a.c'.

todo...

The level's order are as follows:

  1. TRACE: displays all levels, is the finest output and only recommended during debugging
  2. DEBUG: display all but the finest logs, only recommended during develop stages
  3. INFO :  Show info, warn and error messages
  4. WARN :  Show warn and error messages
  5. ERROR: Show only error messages.
  6. OFF  : Disable all logging, recommended for silencing noisy logging during debugging. *will* surpress errors logging.

Example:

config.prefixPattern = '%s::[%s]> ';
config.logLevels = {
    'a.b.c': logEnhancerProvider.LEVEL.TRACE, // trace + debug + info + warn + error
    'a.b.d': logEnhancerProvider.LEVEL.ERROR, // error
    'a.b': logEnhancerProvider.LEVEL.DEBUG, // debug + info + warn + error
    'a': logEnhancerProvider.LEVEL.WARN, // warn + error
    '*': logEnhancerProvider.LEVEL.INFO // info + warn + error
};
// globally only INFO and more important are logged
// for group 'a' default is WARN and ERROR
// a.b.c and a.b.d override logging everything-with-TRACE and least-with-ERROR respectively

// modify later:
config.logLevels['a.b.c'] = $log.LEVEL.ERROR;
config.logLevels['*'] = $log.LEVEL.OFF;