Logging node module based on Winston.
- NodeJS >= 8.0
npm install --save @neo9/n9-node-log
import n9Log from '@neo9/n9-node-log'
const log = n9Log('my-app-name')
// Write on stdout
log.verbose('This is a verbose message')
log.debug('This is a debug message')
log.info('This is an information message')
log.warn('Warning, this feature will be removed soon')
// Write on stderr
log.error('An error appened')
2017-05-12T15:57:14.474Z - info: [my-app-name] This is an information message
2017-05-12T15:57:14.689Z - warn: [my-app-name] Warning, this feature will be removed soon
2017-05-12T15:57:14.974Z - error: [my-app-name] An error appened
log.info('Log with metadata', { anything: 'this is metadata' })
log.error('Here an error', new Error('hello'))
2017-05-12T15:57:14.474Z - info: [my-app-name] Log with metadata anything=this is metadata
2017-05-12T15:57:14.785Z - error: [node-ts-skeleton] Here an error Error: hello
at Object.<anonymous> (/home/schopin/Neo9/node-ts-skeleton/src/index.ts:16:28)
at Module._compile (module.js:571:32)
at ...
const logUsers = log.module('users')
logUsers.info('Log specific to users module')
2017-05-12T15:57:14.474Z - info: [my-app-name:users] Log specific to users module
log.profile('test')
setTimeout(() => log.profile('test'), 1000)
2017-05-12T15:57:14.474Z - info: [my-app-name] test durationMs=1000
You can filter the list of logs on startup with the N9LOG
environment or with the level
option.
Possible values:
verbose
: Display all logsdebug
: Display debug + info + warn + error logsinfo
: Display info + warn + error logswarn
: Display warn + error logserror
: Display only error logs
Default value: verbose
Example: N9LOG=error node server.js
will display only error logs.
Define the transporter(s) you want to use to store your logs, notice that you can combine them.
Will output the logs into process.stdout
and process.stderr
.
- Type:
Boolean
- Default:
true
Example:
const log = n9Log('my-app-name', {
console: false, // Don't output the logs into the console
})
Will write the log output into specified file(s).
- Type:
Array
- Default:
[]
- Properties:
level
: Level of messages to write into the file (default:'info'
)filename
: Path of the logfilemaxsize
: Max sizes in bytes of the logfilemaxFiles
: Limit the number of files created
Example:
const log = n9Log('my-app-name', {
files: [
{
level: 'info',
filename: '/tmp/info-log.log'
}
]
})
- Type:
Array
- Default:
[]
- Properties:
host
: (Default:'localhost'
) Remote host of the HTTP logging endpointport
: (Default:80
or443
) Remote port of the HTTP logging endpointpath
: (Default:'/'
) Remote URI of the HTTP logging endpointauth
: (Default: None) An object representing theusername
andpassword
for HTTP Basic Authssl
: (Default:false
) Value indicating if we should us HTTPS
Example:
const log = n9Log('my-app-name', {
http: [
{
host: 'my-logs.io',
path: '/save',
ssl: true
}
]
})
Useful for adding custom transports into n9-node-log
:
- Type:
Array
- Default:
[]
Example:
import * as WinstonElasticSearch from 'winston-elasticsearch'
const log = n9Log('test', {
console: false,
transports: [
new WinstonElasticSearch({
index: 'n9-log',
level: 'info',
mappingTemplate: require('winston-elasticsearch/index-template-mapping.json'),
flushInterval: 200
})
]
})