pino-elasticsearch icon indicating copy to clipboard operation
pino-elasticsearch copied to clipboard

Help wanted: How do I integrate it with express-pino-logger

Open usama-dev opened this issue 5 years ago • 3 comments
trafficstars

I am using express-pino-logger and want to log the http and routes logs to elasticsearch using this module.

Can you please provide an example for such case?

Thanks.

usama-dev avatar Feb 17 '20 11:02 usama-dev

Just pipe the two processes.

mcollina avatar Feb 17 '20 11:02 mcollina

This is how I am doing it:

const streamToElastic = pinoElastic({
	index: 'an-index',
	type: 'log',
	consistency: 'one',
	node: 'http://localhost:9200',
	'es-version': 6,
	'bulk-size': 200,
	ecs: true
})
const logger = pino({ level: 'info' }, streamToElastic)

var myLogger = function (req, res, next) {
	logger.info(loggerHttp(req,res))	// This does not work
	req.logger = logger;
	next()
}
app.use(myLogger)

app.get('/demo', (req, res, next) => {
	req.logger.info('Route Log!')	// This works
	res.send('demo')
})

These are the records saved in ES:
{
  "took": 3,
  "timed_out": false,
  "_shards": {
    "total": 5,
    "successful": 5,
    "skipped": 0,
    "failed": 0
  },
  "hits": {
    "total": 2,
    "max_score": 1.0,
    "hits": [
      {
        "_index": "an-index",
        "_type": "log",
        "_id": "BQURU3ABd7Iq_pY3IBo_",
        "_score": 1.0,
        "_source": {
          "ecs": {
            "version": "1.0.0"
          },
          "@timestamp": "2020-02-17T12:13:15.459Z",
          "message": "Route Log!",
          "log": {
            "level": 30
          },
          "host": {
            "hostname": ""
          },
          "process": {
            "pid": 23332
          }
        }
      },
      {
        "_index": "an-index",
        "_type": "log",
        "_id": "BAURU3ABd7Iq_pY3IBoR",
        "_score": 1.0,
        "_source": {
          "ecs": {
            "version": "1.0.0"
          },
          "@timestamp": "2020-02-17T12:13:15.448Z",
          "log": {
            "level": 30
          },
          "host": {
            "hostname": ""
          },
          "process": {
            "pid": 23332
          }
        }
      }
    ]
  }
}

This is the HTTP log I get in terminal and want this in ES too:
`{"level":30,"time":1581941935630,"pid":23332,"hostname":"","req":{"id":2,"method":"GET","url":"/demo","headers":{"host":"localhost:4000","user-agent":"insomnia/7.1.0","x-access-token":"123","content-type":"application/json","accept":"*/*","content-length":"192"},"remoteAddress":"::1","remotePort":61463},"res":{"statusCode":200,"headers":{"x-powered-by":"Express","access-control-allow-origin":"*","content-type":"text/html; charset=utf-8","content-length":"4","etag":"W/\"4-ieSV55Qc+eQOaYDRSha/AjzNTJE\""}},"responseTime":6,"msg":"request completed","v":1}`

usama-dev avatar Feb 17 '20 12:02 usama-dev

what is loggerHttp?

If it's pino-http, you need to pass the the pino logger to pino-http: https://github.com/pinojs/pino-http#examples.

mcollina avatar Feb 17 '20 15:02 mcollina