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

@codejamboree/js-logger

v2.2.0

Published

Simple logger utility

Downloads

268

Readme

js-logger

Simple logger utility that adds some color to the console, formatting, and error logging. The same common functions were being used on small utility scripts. Eventually they were separated for reusability accross projects.

npm install @codejamboree/js-logger
import logger from '@codejamboree/js-logger';
logger.debug('hello'); // log magenta
logger.log('hello'); // log standard white
logger.info('hello'); // log blue
logger.warn('hello'); // log yellow
logger.error('hello'); // log red

Colors

Color can also be extended with the ANSI helper.

logger.log(`This is ${logger.ansi.red('Red')}.`); // Colors as red.
logger.log(logger.ansi.bgRed('Red')); // background color is red

Colors available are black, red, green, yellow, blue, magenta, cyan, and white.

Timers

Timer labels are cyan.

logger.time('my label');
logger.timeLog('my label'); // time label colored in cyan
logger.timeEnd('my label'); // time label colored in cyan

Formatting

A few helper functions provide some common formatting.

  • done displays the console log, and the word, "done"
  • title displays a title centered
  • section displays a sub-title centered. Can customize start/end tags

Text is centered at 40 characters wide, with wrapping.

logger.title('my title');
// ----------------------------------------
//                 my title
// ----------------------------------------
logger.title('my title '.repeat(6));
// ----------------------------------------
//  my title my title my title my title my 
//              title my title             
// ----------------------------------------
logger.section('my section');
// 
//           ---{ my section }---          
// 
logger.section('my section '.repeat(4));
//
//    ---{ my section my section my }---   
//       ---{ section my section }---      
//
logger.section('my section', '<<< ', ' >>>');
//
//            <<< my section >>>           
//
logger.done();
// Running time: 18.331ms
// done

Error Logging

The logger has a logError method which can accept errors, objects, arrays, strings, promises, buffers, and more. An attempt has been made to handle just about any type of data and log it appropriately.

logger.logError(new Error('The Error'));
// The Error
logger.logError('The string error');
// The string error
logger.logError(['Error 1', 'Error 2']);
// Error (Array)
//   Error 1
//   Error 2
logger.logError({error: 'The error key'});
// The error key
// NOTE: same for keys: errors, message, message, reason, reasons
logger.logError(Buffer.from('The buffer error'));
// The buffer error
logger.logError(Promise.resolve('The resolved value'));
// Error (Promise)
// The resolved value
logger.logError(Promise.reject('The rejected value'));
// Error (Promise)
// The rejected value
logger.logError(1); 
// Error: 1
logger.logError(false); 
// Error: false
logger.logError(new Date()); 
// Error (Date): 2024-08-31T05:50:06.145Z
logger.logError({unrecognized: 'The unknown value'});
// Error (Object)
//   { unrecognized: 'The unknown value' }
logger.logError(null);
// Error (Empty)
logger.logError(undefined);
// Error (Empty)
logger.logError("");
// Error (Empty)

If an error has some specific keys, they will be listed as well. Specifically, these are data and rawPacket.

logger.logError({
  error: 'The Error',
  data: 'the data',
  rawPacket: Buffer.from("Hello")
});
// The Error
//   Hello
//   the data

Console

The logger is separat from the console, but has many of the same method names. You can override the consoles methods (debug, info, timeStamp, timeEnd) to apply color.

console.debug('foo'); // writes foo in white
logger.attach();
console.debug('foo'); // writes "foo" in magenta
logger.restore();
console.debug('foo'); // writes foo in white

Ideal Script

import logger from '@codejamboree/js-logger';

const main = async () => {
  console.log('do something.');
}

try {
  logger.attach();
  logger.title('Script Name');
  main()
    .catch(logger.logError)
    .finally(logger.done);
} catch (e) {
  logger.logError(e);
  logger.done();
}