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

Any suggestions on good logging practice? #119

Closed
samtgarson opened this issue Jan 3, 2017 · 3 comments
Closed

Any suggestions on good logging practice? #119

samtgarson opened this issue Jan 3, 2017 · 3 comments

Comments

@samtgarson
Copy link

More of a question than a bug, I was wondering how you have handled logging with micro?

I'd like to get some reasonable logging (e.g. response time, status, request content length etc.) out of my microservice. I've spent 5 mins playing around trying to get morgan to play with micro but it follows the middleware ideology which micro doesn't.

Any tips would be great! Thanks for open sourcing this 👍

@timneutkens
Copy link
Member

timneutkens commented Jan 3, 2017

Have you seen #104?

On topic, this should work. haven't tested.
Basically calling morgan('combined') returns a function with this footprint: function (req, res) which can be called inside of a micro function 👍. Note: you can use any express middleware with the same footprint this way, though it's not always wise to do so 😄

// Based on https://www.npmjs.com/package/morgan#vanilla-http-server
const { send } = require('micro')
const morgan = require('morgan')
const logger = morgan('combined')
 
module.exports = async function (req, res) {
  logger(req, res, function (err) {
    if (err) {
      return send(res, 500, 'Error')
    }
    
    send(res, 200, 'OK!')
  })
})

// Using pify to turn logger into a Promise.
const { send } = require('micro')
const pify = require('pify')
const morgan = require('morgan')
const logger = pify(morgan('combined'))
 
module.exports = async function (req, res) {
  await logger(req, res)
  return 'OK!'
})

@samtgarson
Copy link
Author

I did see #104 but I figured it would be useful to be able to customise the log output, especially as I have one project that needs to log JSON to stdout to be picked up by fluentd. Thanks for the tips.

@timneutkens
Copy link
Member

👍

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