fwsp-logger
Advanced tools
Comparing version 0.1.6 to 0.2.0
@@ -6,3 +6,23 @@ 'use strict'; | ||
const hydraOpts = (opts, hydra) => { | ||
let augment = {serviceName: hydra.getServiceName()}; | ||
if (opts.augment) { | ||
opts.augment = Object.assign(opts.augment, augment); | ||
return opts; | ||
} | ||
return Object.assign(opts, {augment}); | ||
}; | ||
/** | ||
* @name HydraPinoLogger | ||
* @summary Extensions for Hydra | ||
* @extends PinoLogger | ||
*/ | ||
class HydraPinoLogger extends PinoLogger { | ||
constructor(opts, hydra) { | ||
super(hydraOpts(opts, hydra)); | ||
} | ||
} | ||
/** | ||
* @name HydraLogger | ||
@@ -35,7 +55,9 @@ * @summary HydraPlugin for logging | ||
initLogger() { | ||
this._logger = new PinoLogger( | ||
this._logger = new HydraPinoLogger( | ||
Object.assign( | ||
{serviceName: this.hydraConfig.serviceName}, | ||
this.opts | ||
)); | ||
), | ||
this.hydra | ||
); | ||
} | ||
@@ -42,0 +64,0 @@ /** |
@@ -21,3 +21,3 @@ 'use strict'; | ||
initPino(opts) { | ||
this.middleware = require('express-pino-logger')(opts, this.pt); | ||
this.middleware = require('pino-http')(opts, this.pt); | ||
this.pino = this.middleware.logger; | ||
@@ -24,0 +24,0 @@ this.logger = this.augment ? this.pino.child(this.augment) : this.pino; |
@@ -78,35 +78,53 @@ 'use strict'; | ||
serializers(redactFields = []) { | ||
let redacted = obj => { | ||
let censored = _.clone(obj); | ||
if (redactFields.length) { | ||
redactFields.forEach(field => { | ||
if (_.has(censored, field)) { | ||
_.set(censored, field, '[redacted]'); | ||
} | ||
}); | ||
} | ||
return censored; | ||
this.redactFields = redactFields; | ||
return { | ||
req: (req) => this.reqSerializer(req), | ||
res: (res) => this.resSerializer(res), | ||
err: (err) => this.errSerializer(err) | ||
}; | ||
} | ||
redacted(obj) { | ||
let censored = _.clone(obj); | ||
if (this.redactFields.length) { | ||
this.redactFields.forEach(field => { | ||
if (_.has(censored, field)) { | ||
_.set(censored, field, '[redacted]'); | ||
} | ||
}); | ||
} | ||
return censored; | ||
} | ||
resSerializer(res) { | ||
return { | ||
req: (req) => { | ||
return Object.assign({ | ||
url: req.url, | ||
originalUrl: req.originalUrl, | ||
fullUrl: `${req.protocol}://${req.get('host')}${req.originalUrl}`, | ||
method: req.method, | ||
body: (req.method === 'post' || req.method === 'POST') ? redacted(req.body) : {}, | ||
xForwardedFor: req.headers['x-forwarded-for'], | ||
host: req.headers['host'], | ||
userAgent: req.headers['user-agent'] | ||
}, this.augment); | ||
}, | ||
res: (res) => { | ||
return { | ||
statusCode: res.statusCode | ||
//,header: res._header | ||
}; | ||
} | ||
statusCode: res.statusCode | ||
}; | ||
} | ||
reqSerializer(req) { | ||
return Object.assign( | ||
{ | ||
id: req.id, | ||
url: req.url, | ||
originalUrl: req.originalUrl, | ||
fullUrl: `${req.protocol}://${req.get('host')}${req.originalUrl}`, | ||
method: req.method, | ||
body: (req.method === 'post' || req.method === 'POST') ? this.redacted(req.body) : {}, | ||
xForwardedFor: req.headers['x-forwarded-for'], | ||
host: req.headers['host'], | ||
userAgent: req.headers['user-agent'] | ||
}, | ||
this.augment | ||
); | ||
} | ||
errSerializer(err) { | ||
return err instanceof Error ? { | ||
type: err.name || err.constructor.name, | ||
message: err.message, | ||
stack: err.stack | ||
} : err; | ||
} | ||
/** | ||
@@ -113,0 +131,0 @@ * @name initFile |
{ | ||
"name": "fwsp-logger", | ||
"version": "0.1.6", | ||
"version": "0.2.0", | ||
"author": { | ||
@@ -24,8 +24,8 @@ "name": "Eric Adum", | ||
"chokidar": "1.7.0", | ||
"express-pino-logger": "2.0.0", | ||
"cross-spawn": "5.1.0", | ||
"hydra-express-plugin": "0.0.1", | ||
"hydra-plugin": "0.0.1", | ||
"hydra-express-plugin": "0.0.1", | ||
"lodash": "4.17.4", | ||
"pino": "4.5.2", | ||
"cross-spawn": "5.1.0" | ||
"pino-http": "2.6.1" | ||
}, | ||
@@ -32,0 +32,0 @@ "devDependencies": { |
# Logger [![npm version](https://badge.fury.io/js/fwsp-logger.svg)](https://badge.fury.io/js/fwsp-logger) | ||
## Synopsis | ||
## Summary | ||
Provides a [pino](https://github.com/pinojs/pino) logger | ||
that ships its logs to Elasticsearch via [pino-elasticsearch](https://github.com/pinojs/pino-elasticsearch). | ||
that ships its logs to elasticsearch via [pino-elasticsearch](https://github.com/pinojs/pino-elasticsearch). | ||
First, run `npm install -g pino-elasticsearch` | ||
## Usage | ||
Use HydraExpressLogger plugin for Hydra Express apps: | ||
Run `npm install -g pino-elasticsearch` to install the elasticsearch transport module. | ||
See [Configuration](https://github.com/flywheelsports/fwsp-logger#configuration) for details on the logger plugin config options. | ||
Use the `HydraExpressLogger` plugin for Hydra Express apps: | ||
```javascript | ||
@@ -14,4 +19,11 @@ const HydraExpressLogger = require('fwsp-logger').HydraExpressLogger; | ||
hydraExpress.init(...); | ||
hydraExpress.appLogger.info('information', {and: 'an object', with: 'some stuff'}); | ||
hydraExpress.appLogger.error({err: new Error('this will log a stack trace')}); | ||
// in a request handler | ||
req.log.info('this will also log information about the current request'); | ||
req.log.error({err: new Error('this will log a stack trace')}); | ||
``` | ||
with corresponding entry in config.json hydra.plugins: | ||
with corresponding entry in config.json: | ||
```json | ||
@@ -21,8 +33,5 @@ "hydra": { | ||
"logger": { | ||
"serviceName": "optional - will default to hydra.serviceName", | ||
"logPath": "optional - will default to service/servicename.log", | ||
"toConsole": false, // don't log to console | ||
"noFile": true, // don't log to disk | ||
"logRequests": true, // log all requests in development env | ||
"redact": ["password"], // fields to redact when logging req.body | ||
"serviceName": "foo-service", | ||
"toConsole": false, | ||
"noFile": true, | ||
"elasticsearch": { | ||
@@ -36,13 +45,17 @@ "host": "localhost", | ||
} | ||
, | ||
``` | ||
Or, use HydraLogger plugin for Hydra services: | ||
Or, use the `HydraLogger` plugin for Hydra services: | ||
```javascript | ||
const HydraLogger = require('fwsp-logger').HydraLogger; | ||
hydra.use(new HydraLogger()); | ||
let hydraLogger = new HydraLogger(); | ||
let log = hydraLogger.getLogger(); | ||
hydra.use(hydraLogger); | ||
hydra.init(...); | ||
log.info('some info'); | ||
log.error({err: new Error('error with stack trace')}); | ||
log.error('just a message, no stack trace'); | ||
``` | ||
General usage: | ||
General usage (outside of Hydra): | ||
```javascript | ||
@@ -52,5 +65,4 @@ const PinoLogger = require('fwsp-logger').PinoLogger, | ||
{ | ||
serviceName: 'my-service', // required - name of the app writing logs | ||
logPath: '/custom/log-file.log', // optional, defaults to ${cwd()}/serviceName.log | ||
toConsole: true, // defaults to false | ||
serviceName: 'my-app', /* required - name of the app writing logs */ | ||
logPath: '/custom/log-file.log', /* optional, defaults to ${cwd()}/serviceName.log */ | ||
elasticsearch: { | ||
@@ -64,3 +76,3 @@ host: 'your.elasticsearch.host.com', | ||
const appLogger = logger.getLogger(); | ||
appLogger.error('An error happened'); | ||
appLogger.error({err: 'An error happened'}); // pass {err} literal for proper error serialization | ||
appLogger.info({ | ||
@@ -75,2 +87,13 @@ message: 'Something else happened', | ||
## Configuration | ||
| Field | Description | Required | Default | ||
| --- | --- | ---| --- | ||
| serviceName | Name of the service doing the logging | N | `hydra.serviceName` | ||
| logPath | Path to log to if !noFile | N | `service/servicename.log` | ||
| toConsole | Log to console (stdout)? | N | `true` | ||
| noFile | Don't write log to disk | N | `false` | ||
| redact | Fields to redact (e.g. passwords, credit card numbers, etc.) | N | `[]` | ||
| elasticsearch | Connection object for ElasticSearch | N | *none* | ||
## Testing | ||
@@ -89,4 +112,4 @@ | ||
before you'll be able to view logs, which should be the value of | ||
logger.elasticsearch.index ('local-dev' in above examples), | ||
or 'pino' by default. | ||
`logger.elasticsearch.index` (`local-dev` in above examples), | ||
or `pino` by default. | ||
@@ -93,0 +116,0 @@ If you don't have any index patterns set up, Kibana won't let you |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
17319
352
116
+ Addedpino-http@2.6.1
+ Addedpino-http@2.6.1(transitive)
- Removedexpress-pino-logger@2.0.0
- Removedexpress-pino-logger@2.0.0(transitive)
- Removedpino-http@2.6.2(transitive)