@heathmont/node-logger
v2.1.0
Published
node-logger
Maintainers
Keywords
Readme
Simple logging library for node projects
Main purpose for this library is having streamlined logging logic, like in production output would be in JSON and while developing it's output is simpler with better readability (default winston console). Library has two main components, logger and loggerExpress (express' middleware, which is easy to plug in).
Environment variables
LOG_SERIALIZATION = json | text- when json is set, log output will be json serialized, text otherwiseNODE_ENV = production | development- when production is set only info, warn and error levels are logged and for loggerExpress only erroneous response codes (>400) will be logged.LOG_LEVEL = debug | info | warn | error- set log level without having to change NODE_ENV of your applicationLOG_SANITIZATION = true- if not specified, sanitization depends onNODE_ENV: disabled fordevelopment, enabled forproduction. Setting this totrueforces sanitization on in non-production environments. Production sanitization is always on and cannot be disabled via this flag.LOG_SANITIZE_CUSTOM_FIELDS = field1,field2,field3- sanitize additional fields (when sanitization is enabled), defined as a comma-separated list of field names
logger
Logger adds metadata for instance (like host), replaces sensitive meta fields (like password) with asterisk.
Usage
It has typical logging functions like:
logger.init({ prefix: 'myProject' }); // All the meta info will be prefixed with this key
logger.debug('This is verbose debugging message', { someMetaData: 'Like so', user: { username: 'someuser' } });
logger.info('This is informational', { someMetaData: 'Like so', username: 'something', password: 'secret123' });
logger.warn('This warns about something', { someMetaData: 'Like so' });
logger.error('Some kind of error, things went bad', { someMetaData: 'Like so', username: 'something', password: 'secret123' });loggerExpress
It adds metadata for:
- instance's hostname (os.hostname()) as host
- request
- request.headers[x-forwarded-for] as meta.request.xForwardedFor
- request.headers.authorization without signature as meta.request.authorization
- req.connection.remoteAddres as meta.request.remoteAddress
- body content (for debugging when error occur)),
- response
- response.headers[content-length] as meta.response.contentLength
- time duration how long does the serving took as meta.response.timeMs
Usage
As it's just express middleware you can simply import and use:
expressServer.use(loggerExpress);Example json output
From express
{
"timestamp": "2017-09-20T10:36:10.110Z",
"level": "error",
"host": "fdd9bb5a29dd",
"method": "POST",
"url": "\/graphql",
"status": 400,
"meta": {
"request": {
"xForwardedFor": "213.219.106.114, 172.68.182.194, 10.0.2.164",
"body": {
"id": "q7",
"query": "query ...",
"variables": {
}
}
},
"response": {
"contentLength": 132,
"timeMs": 8.024
}
}
}From logger
{
"@timestamp": "2017-09-20T10:36:10.110Z",
"level": "error",
"meta": {
"stack": "GraphQLError: Cannot query field \"someRandomField\" on type \"SomeRandomType\".\n at Object.Field (\/...\/FieldsOnCorrectType.js:66:31)\n at Object.enter (\/...\/visitor.js:297:29)\n",
"nodes": [
{
"selectionSet": null,
"loc": {
"start": 694,
"end": 707
},
"directives": [
],
"kind": "Field",
"name": {
"loc": {
"start": 694,
"end": 707
},
"kind": "Name",
"value": "someRandomField"
},
"alias": null,
"arguments": [
]
}
],
"locations": [
{
"line": 1,
"column": 695
}
],
"positions": [
694
],
"source": {
"name": "GraphQL request",
"body": "query Application {...."
},
"message": "Cannot query field \"someRandomField\" on type \"SomeRandomType\"."
},
"host": "fdd9bb5a29dd",
"message": "",
"type": "live"
}