3.0.3
I wanted to be able to have chattier daemons running in development, and more succinct logging in production but wanted to keep the simplicity of using console.log()
etc.
Normally I pass in a granular verboseness level via arguments to control the verbosity level for the running process.
npm install --save verbosity
Simply override the built in console object:
import {createConsole} from 'verbosity'
const console = createConsole({
outStream: process.stdout,
errorStream: process.stderr,
verbosity: 5
})
console.log('Works like normal...')
console.debug('...but now controllable.')
console.verbosity(3) // Use numbered levels 5 (debug) to 1 (error)
console.debug('...this isn’t printed now.')
console.canWrite(5) && console.dir({print: 'this won’t.'})
console.verbosity('debug') // Use named levels [debug, info, log, warning, error]
console.canWrite(5) && console.dir({print: 'this will now.'})
This will direct all console output to stderr, but silence 'info' and 'debug' messages.
import {createConsole} from 'verbosity'
const console = createConsole({
outStream: process.stderr,
verbosity: 3
})
console.log('Picked brown jacket...') // Printed
console.debug('Purple tie chosen...') // Not printed
console.warn("That tie doesn't go with that jacket.") // Printed
Or go mad with making up any number of custom console writers.
import {createConsole} from 'verbosity'
const myUberConsole = createConsole({
outStream: myFancyWriteableStream,
verbosity: 5
})
myUberConsole.panic('Core Flux Capacitor Meltdown!')
Create a new Verbosity object.
(object)
Options to pass to the factory.
Verbosity
:
Verbosity's console object.
Return the modules version metadata.
(number)
Version format required.
string
:
The version string.
Generate a verbosity console
Extends Console
(object
= {}
)
Configuration options.
Name | Description |
---|---|
options.outStream stream.writable
|
Stream to write normal output |
options.errorStream stream.writable
|
Stream to write error output |
options.verbosity number
(default 3 )
|
The verboseness of output: 0: Mute 1: Errors 2: Notice 3: Log 4: Info 5: Debug |
options.timestamp string
|
Timestamp format. |
options.namespace string
|
Sparkles namespace to emit events to. |
options.global boolean
|
Should changes to verbosity be made globally? |
options.prefix string
|
Logging message prefix. |
Verbosity
:
Verbosity's console object.
Log a debug message. (Level 5)
Log an info message. (Level 4)
Log a normal message. (Level 3)
Log a warning message. (Level 2)
Log an error message. (Level 1)
Log a critical error message, if something breaks. (Level 1)
Log a panic error message if something unexpected happens. (Level 1)
Log a emergency message, for when something needs emergency attention. (Level 1)
As console.dir, but defaults to colour (if appropriate) and zero depth.
Pretty prints object, similar to OS X's plutil -p. Defaults to zero depth.
(object)
The Object to print.
(number
= 0
)
How many object levels to print.
(boolean
= true
)
Print output in color, if supported.
console.pretty(console)
// Outputs:
Object: VerbosityMatrix
critical ▸ [Function]
error ▸ [Function ▸ bound ]
warn ▸ [Function ▸ bound ]
log ▸ [Function ▸ bound ]
info ▸ [Function ▸ bound ]
debug ▸ [Function]
canWrite ▸ [Function]
...
Helper function for pretty printing a summary of the current 'yargs' options.
Only prints 'long options', ._
as 'arguments' and $0
as 'self'.
(object)
The Yargs argv object to print.
(boolean
= true
)
Print output in color, if supported.
console.yargs(yargs)
// Outputs:
Object (yargs):
left ▸ 2
right ▸ 2
mode ▸ 'hard'
encoding ▸ 'utf8'
...
self ▸ '/usr/local/bin/truwrap'