azify-logger
Version:
Azify Logger Client - Centralized logging for OpenSearch
75 lines (62 loc) • 2.76 kB
JavaScript
if (process.env.AZIFY_LOGGER_DISABLE === '1') {
module.exports = {}
} else {
const Module = require('module')
const originalRequire = Module.prototype.require
Module.prototype.require = function(id) {
const result = originalRequire.call(this, id)
if (id === '@nestjs/common') {
if (result && result.Logger) {
const originalLoggerConstructor = result.Logger
function PatchedLogger(context) {
const instance = new originalLoggerConstructor(context)
instance.log = function(message, context) {
const { getRequestContext } = require('./store')
const ctx = getRequestContext()
if (ctx && ctx.traceId) {
return originalLoggerConstructor.prototype.log.call(this, message, context)
}
return originalLoggerConstructor.prototype.log.call(this, message, context)
}
instance.error = function(message, trace, context) {
const { getRequestContext } = require('./store')
const ctx = getRequestContext()
if (ctx && ctx.traceId) {
return originalLoggerConstructor.prototype.error.call(this, message, trace, context)
}
return originalLoggerConstructor.prototype.error.call(this, message, trace, context)
}
instance.warn = function(message, context) {
const { getRequestContext } = require('./store')
const ctx = getRequestContext()
if (ctx && ctx.traceId) {
return originalLoggerConstructor.prototype.warn.call(this, message, context)
}
return originalLoggerConstructor.prototype.warn.call(this, message, context)
}
instance.debug = function(message, context) {
const { getRequestContext } = require('./store')
const ctx = getRequestContext()
if (ctx && ctx.traceId) {
return originalLoggerConstructor.prototype.debug.call(this, message, context)
}
return originalLoggerConstructor.prototype.debug.call(this, message, context)
}
instance.verbose = function(message, context) {
const { getRequestContext } = require('./store')
const ctx = getRequestContext()
if (ctx && ctx.traceId) {
return originalLoggerConstructor.prototype.verbose.call(this, message, context)
}
return originalLoggerConstructor.prototype.verbose.call(this, message, context)
}
return instance
}
Object.setPrototypeOf(PatchedLogger, originalLoggerConstructor)
Object.assign(PatchedLogger, originalLoggerConstructor)
result.Logger = PatchedLogger
}
}
return result
}
}