herb

2.3.0 • Public • Published

Herbal Logo

Add some true flavour to your NodeJS console!

herb is a magical layer for the most complex logs. To put it in simple words it enables advanced functions missing in the current Node logging system (console.log). You can group, count, and add lines, paragraphs or tables all in color! and the best part about it is the fact that you can just replace the global console with herb without further modifications!

Installation

npm install herb

or if you are developing a module

npm install herb --save

Usage

Logging

Either

var herb = require('herb');

herb.log("An herb log");
herb.warn("An herb warning");

Or

var console = require('herb');

console.log("An herb log");
console.warn("An herb warning");
// Even
console.count('label');

Colors & Styles

All Culinary styles and colors are supported! e.g.

herb.log(herb.green('SUCCESS'), herb.underline('www.example.com'))
  • Colors: Black, White, Green, Blue, Cyan, Magenta, Red, Yellow
  • Backgrounds: bgBlack, bgWhite, bgGreen, bgBlue, bgCyan, bgMagenta, bgRed, bgYellow
  • Spices: bold, underline, strikethrough (barely supported by clients), italic, hidden, invert, reset

Methods

Info, Log, Warn, Error (color:blue, blue, yellow, red, verbosity:4, 3, 2, 1) -> herb.log(log, json, ...)

Write "Writes logs through process.stdout rather than console.log but retains similar behavior" -> herb.write(log1, log2, ...)

Template "Allows for color templating" -> herb.template(type, color1, color2, style3, ...)

var header = herb.template('log', 'green', 'white', 'green');
header('This will be green', 'white here', '& back to green again', 'this will be ignored');

Clear "Clears console screen" -> herb.clear()

ClearLine "Clears last line for writing" -> herb.clearLine()

Count "Counts to label" -> herb.count(label)

herb.count("Apples");
herb.count("Apples");
/// Output:
// Apples : 1
// Apples : 2

Group, GroupEnd "Creates a group" -> herb.group(title)

herb.group("Fruits");
herb.log("Apples");
herb.log("Oranges");
herb.groupEnd();
herb.warn("Group ended!");
/// Output:
// > Fruits
// | Apples
// | Oranges
// Group ended!

Time, TimeEnd "Measure time in ms" -> herb.time(label)

herb.time("timeout");
setTimeout(function() { herb.timeEnd("timeout") }, 1200);
/// Output after 1200ms: (the output includes timeout computation time ~ 4ms to 12ms)
// timeout: 1200ms

Humanify "Output human friendly json" -> herb.humanify(json)

herb.humanify({
	"_id": "54a643971784e5031c7a34a2",
	"index": 3,
	"guid": "37f345d8-de55-46c2-9071-73d0b6982194",
	"isActive": true,
	"name": "Head Schneider"
});
/// Output:
// _id:       54a643971784e5031c7a34a2
// index:     3
// guid:      37f345d8-de55-46c2-9071-73d0b6982194
// isActive:  true
// name:      Head Schneider

Paragraph "Output a formatted paragraph" -> herb.paragraph(text)

herb.paragraph("Anim magna velit ipsum id et dolor labore."
+ "Irure ipsum enim in laborum deserunt elit sit eu sit id"
+ "et adipisicing eu do. Ad nulla ullamco excepteur consequat"
+ "veniam ut. Tempor elit excepteur nulla pariatur irure nisi.",
{ alignment: "center", color: "green", width: '50%', margin: '50%' });
/// Outputs a green centered text with half width in the middle of the screen

Table "Output a formatted table" -> herb.table({ headers: [...], rows: [[...],[...],...], borders: false })

herb.table({
	headers: ['id','name','email','last logged'],
	rows: [
		['1', 'Lorem ipsum', 'lorem@example.com', 'January 1st 2015'],
		['2', '', '', ''],
		['3', 'John Doe', 'info@example.com', 'November 5th 2014']
	],
	borders: false // Full is a table style
});
/// Outputs a green centered text with half width in the middle of the screen

Line "Output a divider" -> herb.line('_')

herb.line('_');
// Or combine multiple characters
herb.line('<-->');
/// Outputs two full width (console width) lines e.g.:
// _________________________
// <--><--><--><--><--><--><-

You can check out pictures of these methods here.

Config & LogFile

var herb = require('herb');

// Config is dynamic
// You can modify config at any point
herb.config({
  // Verbosity of logs (integer)
  // Note that higher numbers cover the entire group e.g. 3 = ['Log','Warn','Error']
  verbose: 3, // 4: Info, 3: Log, 2: Warn, 1: Error, 0: Fatal

  prependTime: false, // Prepends time [hour:minute:seconds] to every log if enabled

  // Can be set to a file e.g. "./log" & logs every log within the verbosity into the file
  logFile: undefined,

  // Add a prefix to all output
  prefix: undefined
})

Log files can be read using a simple code:

var fs = require('fs');
console.log(fs.readFileSync('./log', {encoding: 'utf8'}));

Marker

You can modify background, color and style of every output using marker. If { permanent: boolean } is set to true then the marker will override every output otherwise it will be cleared after each output.

var herb = require('herb');

herb.marker({ color: 'green' }).log('A success story!');
// A Magenta line
herb.marker({ color: 'magenta' }).line('-');

Test Suite

You can do a complex yet boring test by running the following in the source directory:

npm install mocha -g
npm test

I'll soon add a visual test.

Contributing

The goal is to create a rich command-line interface experience for NodeJS. I am currently looking for active contributors to create documentation, improve the code and add innovations to the module. If you are interested you can contact me directly at info@schahriar.com!

License

MIT © Schahriar SaffarShargh info@schahriar.com

Package Sidebar

Install

npm i herb

Weekly Downloads

13

Version

2.3.0

License

MIT

Unpacked Size

590 kB

Total Files

29

Last publish

Collaborators

  • schahriar