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

Wrap bunyan logger to send logs to CloudTrace #3875

Closed
wants to merge 15 commits into from
57 changes: 57 additions & 0 deletions creator-node/src/logging.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const cluster = require('cluster')
const shortid = require('shortid')

const config = require('./config')
const { tracing } = require('./tracer')

// taken from: https://github.com/trentm/node-bunyan/issues/194#issuecomment-347801909
// since there is no official support for string-based "level" values
Expand All @@ -25,6 +26,62 @@ function RawStdOutWithLevelName() {
}
}

function tracerMixin(klass) {
const origFunc = klass.prototype.createLogger
const childProto = klass.prototype.child
klass.prototype.createLogger = function () {
const theLogger = origFunc.apply(this, arguments)
const logDebug = klass.prototype.debug
const logInfo = klass.prototype.info
const logWarn = klass.prototype.warn
const logError = klass.prototype.error
theLogger.debug = function () {
tracing.debug(...arguments)
logDebug.apply(theLogger, arguments)
}
theLogger.info = function () {
tracing.info(...arguments)
logInfo.apply(theLogger, arguments)
}
theLogger.warn = function () {
tracing.warn(...arguments)
logWarn.apply(theLogger, arguments)
}
theLogger.error = function () {
tracing.error(...arguments)
logError.apply(theLogger, arguments)
}

return theLogger
}
klass.prototype.child = function (options) {
const childLogger = childProto.apply(this, options)
const logDebug = bunyan.prototype.debug
const logInfo = bunyan.prototype.info
const logWarn = bunyan.prototype.warn
const logError = bunyan.prototype.error
childLogger.debug = function () {
tracing.debug(...arguments)
logDebug.apply(childLogger, arguments)
}
childLogger.info = function () {
tracing.info(...arguments)
logInfo.apply(childLogger, arguments)
}
childLogger.warn = function () {
tracing.warn(...arguments)
logWarn.apply(childLogger, arguments)
}
childLogger.error = function () {
tracing.error(...arguments)
logError.apply(childLogger, arguments)
}

return childLogger
}
}

tracerMixin(bunyan)
const logLevel = config.get('logLevel') || 'info'
const logger = bunyan.createLogger({
name: 'audius_creator_node',
Expand Down