hiplog
v1.0.2
Published
Fancy lightweight logging utility.
Downloads
3
Maintainers
Readme
Fancy lightweight logging utility for Node.js
Installation
Prerequisites: Node.js 6+, npm 3+.
npm install --save hiplog
Usage
const { Log } = require('hiplog');
const log = new Log({ level: 'debug' });
log.debug('messages to debug an application');
log.info('a purely informational message');
log.notice('a normal but significant condition');
log.warning('warning condition');
log.error('error condition');
log.critical('the system is in critical condition');
log.alert('action must be taken immediately');
log.emergency('system is unusable');
Objects
Objects are automatically stringified using Purdy. Small objects (< 200 characters) are inlined.
log.info('a small object:', { int: 123, bool: true, str: 'Hello' });
const circular = {};
circular.inner = circular;
log.info('a bigger object', {
null: null,
undefined,
integer: 123,
boolean: true,
string: 'Hello',
funtion: function myFunction() {},
circular,
array: ['one', 'two', 'three', 'four'],
});
Errors
Errors are displayed using jest-message-util (powered by :sparkles: babel-code-frame :sparkles:):
try {
throw new Error('Error example');
} catch (e) {
log.error(e);
}
Options
level
- type:
string
- default value:
'info'
Minimum level to display. All messages below this level will be ignored.
stream
- type:
Steam | function: integer -> Stream
- default value:
level => (level <= 4 ? process.stderr : process.stdout)
Stream to write to.
displayTime
- type:
boolean
- default value:
false
Whether to display time information or not. Example:
displayTimeFormat
- type:
string
- default value:
'yyyy-mm-dd HH:MM:ss.l'
Date format to display time in, when displayTime
is set to true
. See dateformat
for possible values.
separator
- type:
string
- default value:
' • '
Separator between message header and body, and also between time and and label,
when displayTime
is set to true
;
format
- type:
function: string -> string
- default value:
hiplog.format
Message formatter function.
fromEnv
const hiplog = require('hiplog');
const log = hiplog.fromEnv();
fromEnv
is a utility function that will create a new instance of Log
with
options taken from environment variables:
NODE_ENV
:'development'
(default):displayTime
is disabled,'test'
:displayTime
is disabled,'level'
is set to'critical'
,'production'
: use default values for each option.
LOG
: setslevel
value.LOG_LEVEL
: alias forLOG
.LOG_TIME
: when set totrue
or1
, enablesdisplayTime
.LOG_TIME_FORMAT
: setsdisplayTimeFormat
value.
Note: fromEnv
accepts an options
parameter that allow overriding these defaults. Example:
const hiplog = require('hiplog');
const log = hiplog.fromEnv({ displayTime: false });
// displayTime will be always `false`, disregarding the value of `LOG_TIME`.
Contributing
Please refer to the guidelines for contributing.
License
Created with npm-package-skeleton.