2014-02-17 09:54:20 -05:00
|
|
|
bunyan = require('bunyan')
|
|
|
|
|
|
|
|
module.exports = Logger =
|
|
|
|
initialize: (name) ->
|
|
|
|
@logger = bunyan.createLogger
|
|
|
|
name: name
|
|
|
|
serializers: bunyan.stdSerializers
|
|
|
|
return @
|
2016-03-29 07:53:16 -04:00
|
|
|
|
2015-12-08 05:30:18 -05:00
|
|
|
initializeErrorReporting: (sentry_dsn, options) ->
|
2015-06-16 11:50:18 -04:00
|
|
|
raven = require "raven"
|
2015-12-08 05:30:18 -05:00
|
|
|
@raven = new raven.Client(sentry_dsn, options)
|
2016-03-29 07:53:16 -04:00
|
|
|
|
|
|
|
captureException: (attributes, message, level) ->
|
|
|
|
error = attributes.err or attributes.error or message
|
|
|
|
req = attributes.req
|
|
|
|
if error?
|
|
|
|
tags = {}
|
|
|
|
extra = {}
|
|
|
|
# capture attributes and use *_id objects as tags
|
|
|
|
for key, value of attributes
|
|
|
|
tags[key] = value if key.match(/_id/) and typeof value == 'string'
|
|
|
|
extra[key] = value
|
|
|
|
# capture req object if available
|
|
|
|
if req?
|
|
|
|
extra.req =
|
|
|
|
method: req.method
|
|
|
|
url: req.originalUrl
|
|
|
|
query: req.query
|
|
|
|
headers: req.headers
|
|
|
|
ip: req.ip
|
|
|
|
# recreate error objects that have been converted to a normal object
|
|
|
|
if !(error instanceof Error) and typeof error is "object"
|
|
|
|
newError = new Error(error.message)
|
|
|
|
for own key, value of error
|
|
|
|
newError[key] = value
|
|
|
|
error = newError
|
|
|
|
# send the error to sentry
|
|
|
|
@raven.captureException(error, {tags: tags, extra: extra, level: level})
|
|
|
|
|
2014-02-17 09:54:20 -05:00
|
|
|
info : ()->
|
|
|
|
@logger.info.apply(@logger, arguments)
|
|
|
|
log : ()->
|
|
|
|
@logger.info.apply(@logger, arguments)
|
2016-03-29 07:53:16 -04:00
|
|
|
error: (attributes, message, args...)->
|
|
|
|
@logger.error(attributes, message, args...)
|
|
|
|
@captureException(attributes, message, "error") if @raven?
|
|
|
|
err: () ->
|
|
|
|
@error.apply(this, arguments)
|
2014-02-17 09:54:20 -05:00
|
|
|
warn: ()->
|
|
|
|
@logger.warn.apply(@logger, arguments)
|
2016-03-29 07:53:16 -04:00
|
|
|
fatal: (attributes, message, callback) ->
|
|
|
|
@logger.fatal(attributes, message)
|
|
|
|
if @raven?
|
|
|
|
cb = (e) -> # call the callback once after 'logged' or 'error' event
|
|
|
|
callback()
|
|
|
|
cb = () ->
|
|
|
|
@captureException(attributes, message, "fatal")
|
|
|
|
@raven.once 'logged', cb
|
|
|
|
@raven.once 'error', cb
|
|
|
|
else
|
|
|
|
callback()
|
2014-02-17 09:54:20 -05:00
|
|
|
|
|
|
|
Logger.initialize("default-sharelatex")
|