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-logs

v3.0.4

Published

Really flexible and fast logger for web servers, applications and daemons

Downloads

43

Readme

Even Better Logs for NodeJS

npm package

Build status Dependency Status Known Vulnerabilities Gitter

We've found this logger to be immensely useful and flexible for us when we develop and work. Hopefully you would find it useful as well!

npm install --save better-logs

Usage

var log = require('better-logs')('section');

log.format('awesome', '{{timestamp}} AWESOME: {{message}}\n');
log.awesome('custom format');

log.debug('debug stuff');
log.info('test');
log.warn('warning');
log.error('error with stack');

Sections

In your module, you should distinguish between different types of logs.

// server.js
var log = require('better-logs')('server');

// download.js
var log = require('better-logs')('download');

// etc.

This way, when logs for a certain module get spammy, you can hide them (or show them when you are debugging.) To do this, it's quite simple:

log.hide('server');

Then to re-enable it:

log.show('server');

Groups

This can get kind of tedious if you have lots of files/sections, so you can define a group to refer to several sections.

log.group('http', ['server', 'download', 'upload']);

Now, instead of hiding/showing individual sections, you can do:

log.hide('http');

You can get even finer control:

log.show('http');      // Show all http logs
log.hide('http/warn'); // ... except warnings

// Now:
httpLog.info('This will be shown.');
httpLog.warn('This will be hidden.');

Modes

Having to show and hide modules can get annoying if you tend to switch between looking at different things. So we've defined modes to automatically set hide/show settings for you.

log.mode('silent', {
	showByDefault: false,
	hide: ['http'],
	show: ['other-modules']
})

You can then switch between modes using:

log.mode('silent')

Formats

By default, we've defined a few formats for convenience, but you can go a step further and define your own formats too.

log.display('dateformat', 'HH:MM');
log.format('custom', '{{file}}:{{line}} custom: {{message}} {{timestamp}}');

Then to invoke it:

log.custom('hello');

Reading and writing outputs

All logs are stream.Readable, so you can simply pipe them to whatever you want. Alternatively, you can also write output to any Writable stream:

// Writes all logs to logs.txt (default is process.stdout)
log.output(fs.createWriteStream('logs.txt'));

// Writes all log.errors to ./error.log
log.output('error', './error.log');

// Writes all section logs to myWriteStream
log.output('section', myWriteStream);

// Writes all errors in section to myErrorStream
log.output('section/error', myErrorStream);

Full Documentation

  • hide([section]) - Hides all logs. If a section/group string is provided, it would only hide those logs.
  • show([section]) - Shows all logs. If a section/group string is provided, it would only show those logs.
  • reset() - Resets all visibility back to its default state.
  • modes() - Returns all defined modes
  • mode() - Returns current active mode
  • mode(modeName) - Sets the active mode
  • mode(modeName, options) - Defines modeName mode with options. Note that if options is null or false this will delete the mode.
  • groups() - Returns all defined groups
  • group(groupName) - Returns the sections/groups in groupName
  • group(groupName, members) - Defines groupName to be a group containing members. This is an array of strings representing a section or another group.
  • formats() - Returns all defined formats
  • format(logType, formatter) - Defines a format for logType (string). formatter may be a string or a function that runs when the log is called. The function gets passed along the arguments from the log call.
  • output([section], writable) - Optionally define a group, section or section/log-type string. Whenever logs happen, they will be written to the writable stream. If no section string is defined then it will write all logs to the writable stream.

Contributions welcome!

Credits

This library was initially made by the awesome team of engineers at Diamond.

If you haven't already, make sure you install Diamond!