This is consistent with what we're pretty much already doing, and saves us from making the request during code review. These were all autofixed and most of them seem easier to read. Some in the legacy services should be rewritten in more legible forms during refactor (ie using intermediate variables, or using request’s qs option). There are some in helper functions and elsewhere that should get rewritten separately. I don't want to change them in this PR because the changes will get lost in this diff, though we could identify them here and fix them before or just after.
62 lines
1.3 KiB
JavaScript
62 lines
1.3 KiB
JavaScript
'use strict'
|
|
const Raven = require('raven')
|
|
const throttle = require('lodash.throttle')
|
|
|
|
const listeners = []
|
|
|
|
// Zero-pad a number in a string.
|
|
// eg. 4 becomes 04 but 17 stays 17.
|
|
function pad(string) {
|
|
string = String(string)
|
|
return string.length < 2 ? `0${string}` : string
|
|
}
|
|
|
|
// Compact date representation.
|
|
// eg. 0611093840 for June 11, 9:38:40 UTC.
|
|
function date() {
|
|
const date = new Date()
|
|
return (
|
|
pad(date.getUTCMonth() + 1) +
|
|
pad(date.getUTCDate()) +
|
|
pad(date.getUTCHours()) +
|
|
pad(date.getUTCMinutes()) +
|
|
pad(date.getUTCSeconds())
|
|
)
|
|
}
|
|
|
|
module.exports = function log(...msg) {
|
|
const d = date()
|
|
listeners.forEach(f => f(d, ...msg))
|
|
console.log(d, ...msg)
|
|
}
|
|
|
|
const throttledConsoleError = throttle(console.error, 10000, {
|
|
trailing: false,
|
|
})
|
|
|
|
module.exports.error = function error(...msg) {
|
|
const d = date()
|
|
listeners.forEach(f => f(d, ...msg))
|
|
Raven.captureException(msg, sendErr => {
|
|
if (sendErr) {
|
|
throttledConsoleError(
|
|
'Failed to send captured exception to Sentry',
|
|
sendErr.message
|
|
)
|
|
}
|
|
})
|
|
console.error(d, ...msg)
|
|
}
|
|
|
|
module.exports.addListener = function addListener(func) {
|
|
listeners.push(func)
|
|
}
|
|
|
|
module.exports.removeListener = function removeListener(func) {
|
|
const index = listeners.indexOf(func)
|
|
if (index < 0) {
|
|
return
|
|
}
|
|
listeners.splice(index, 1)
|
|
}
|