@inveris/dev-logger
v2.0.9
Published
A console logger for development purpose with human readable output and easy modifiability.
Downloads
60
Maintainers
Readme
dev-logger
A ESM console logger for development purpose with human readable output and easy modifiability.
Log levels can be customized, as well as the output (via formatter methods). View examples/ directory.
Please do not use this logger in production mode! For production use pino, winston, bunyan, ...
Install
npm install --save-dev @inveris/dev-logger
Usage
import DevLogger from '@inveris/dev-logger'
const log = new DevLogger(__filename)
log.trace('trace message')
log.debug('debug message')
log.info('info message')
log.success('success message')
log.warn('warn message')
log.error('error message')
log.fatal('fatal message')
const str = 'some value'
log.info('with a string', str)
const obj = {
value1: 'abc',
value2: 123
}
log.info('with an object', obj)
const arr = [ 'a', 'b', 4711 ]
log.info('with an array', arr)
Options
const log = new DevLogger(options)
Options could be a string (group) or an object with the following settings:
group
{string} Name of the group.
A group is a coherent block of messages that belong together.
Simpelst usage is setting the current filename.group: __filename
name
{string} An identifier at each line.
E.g. the application name frompackage.json
could be used.logLevel
{number|string} The level from where the output is visible.upperCaseLevelName
{boolean} Should the level name displayd in upper case. Defaulttrue
.padStartLevelName
{boolean} Should the level name padded before. Defaultfalse
.padEndLevelName
{boolean} Should the level name padded after. Defaultfalse
.withDate
{boolean} Should the date be visible. Defaultfalse
.withGroup
{boolean} Should the group be visible. Defaulttrue
.withName
{boolean} Should the name be visible. Defaulttrue
.colors
{object} Colors of the output.
View index.jsdefaultColors
colors: { trace: 'green', info: 'blue' }
levels
{object} List of log methods.
View index.jsdefaultLevels
levels: { 10: 'debug', 20: 'info', 30: 'warn warning', 40: 'error' }
In this example, we had only debug, info, warn, warning and error methods.
The first string is the prefix, that is visible on output, e. g.WARN message
will be displayed withlog.warn('message'
) orlog.warning('message'
)
Methods
setColors(colors)
colors
{object} Colors of the output.
View Options - colors
setGroup(group)
colors
{object} Name of the group.
View Options - group
setName(name)
name
{object} An identifier at each line.
View Options - name
setLevels(levels)
levels
{object} Name of the group
View Options - levels
setLogLevel(level)
level
{number|string} The level from where the output is visible.
Default Log-Levels
trace
debug
info
success
warn
/warning
error
fatal
Custom Log-Levels
import DevLogger from '@inveris/dev-logger'
const log = new DevLogger({
levels: {
100: 'basic',
200: 'normal',
300: 'extended'
},
colors: {
basic: 'grey',
normal: 'green',
extended: 'yellow'
}
})
log.basic('basic message')
log.normal('normal message')
log.extended('extended message')
Custom formatter
View examples/custom-formatter.js.
Examples
More examples in examples/ directory.
Example:
import DevLogger from '@inveris/dev-logger'
const log = new DevLogger('some id')
log.setLogLevel('trace')
log.trace('trace message')
log.debug('debug message')
log.info('info message')
log.success('success message')
log.warn('warn message')
log.error('error message')
log.fatal('fatal message')