Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
usama-dev opened this issue Feb 17, 2020 · 3 comments
Open

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

usama-dev opened this issue Feb 17, 2020 · 3 comments

Comments

@usama-dev
Copy link

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.

@mcollina
Copy link
Member

Just pipe the two processes.

@usama-dev
Copy link
Author

usama-dev commented Feb 17, 2020

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}`

@mcollina
Copy link
Member

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants