logging-manager.coffee 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. bunyan = require('bunyan')
  2. module.exports = Logger =
  3. initialize: (name) ->
  4. @logger = bunyan.createLogger
  5. name: name
  6. serializers: bunyan.stdSerializers
  7. return @
  8. initializeErrorReporting: (sentry_dsn, options) ->
  9. raven = require "raven"
  10. @raven = new raven.Client(sentry_dsn, options)
  11. @lastErrorTimeStamp = 0 # for rate limiting on sentry reporting
  12. @lastErrorCount = 0
  13. captureException: (attributes, message, level) ->
  14. # handle case of logger.error "message"
  15. if typeof attributes is 'string'
  16. attributes = {err: new Error(attributes)}
  17. # extract any error object
  18. error = attributes.err or attributes.error
  19. # include our log message in the error report
  20. if not error?
  21. error = {message: message} if typeof message is 'string'
  22. else if message?
  23. attributes.description = message
  24. # report the error
  25. if error?
  26. # capture attributes and use *_id objects as tags
  27. tags = {}
  28. extra = {}
  29. for key, value of attributes
  30. tags[key] = value if key.match(/_id/) and typeof value == 'string'
  31. extra[key] = value
  32. # capture req object if available
  33. req = attributes.req
  34. if req?
  35. extra.req =
  36. method: req.method
  37. url: req.originalUrl
  38. query: req.query
  39. headers: req.headers
  40. ip: req.ip
  41. # recreate error objects that have been converted to a normal object
  42. if !(error instanceof Error) and typeof error is "object"
  43. newError = new Error(error.message)
  44. for own key, value of error
  45. newError[key] = value
  46. error = newError
  47. # send the error to sentry
  48. try
  49. @raven.captureException(error, {tags: tags, extra: extra, level: level})
  50. catch
  51. return # ignore any errors
  52. debug : () ->
  53. @logger.debug.apply(@logger, arguments)
  54. info : ()->
  55. @logger.info.apply(@logger, arguments)
  56. log : ()->
  57. @logger.info.apply(@logger, arguments)
  58. error: (attributes, message, args...)->
  59. @logger.error(attributes, message, args...)
  60. if @raven?
  61. MAX_ERRORS = 5 # maximum number of errors in 1 minute
  62. now = new Date()
  63. # have we recently reported an error?
  64. recentSentryReport = (now - @lastErrorTimeStamp) < 60 * 1000
  65. # if so, increment the error count
  66. if recentSentryReport
  67. @lastErrorCount++
  68. else
  69. @lastErrorCount = 0
  70. @lastErrorTimeStamp = now
  71. # only report 5 errors every minute to avoid overload
  72. if @lastErrorCount < MAX_ERRORS
  73. # add a note if the rate limit has been hit
  74. note = if @lastErrorCount+1 is MAX_ERRORS then "(rate limited)" else ""
  75. # report the exception
  76. @captureException(attributes, message, "error#{note}")
  77. err: () ->
  78. @error.apply(this, arguments)
  79. warn: ()->
  80. @logger.warn.apply(@logger, arguments)
  81. fatal: (attributes, message, callback) ->
  82. @logger.fatal(attributes, message)
  83. if @raven?
  84. cb = (e) -> # call the callback once after 'logged' or 'error' event
  85. callback()
  86. cb = () ->
  87. @captureException(attributes, message, "fatal")
  88. @raven.once 'logged', cb
  89. @raven.once 'error', cb
  90. else
  91. callback()
  92. Logger.initialize("default-sharelatex")