简体   繁体   中英

Override console.log|error with winston no longer working

For a while we were using a simple lib to override the default console.log| error with winston, but it no longer works.

This is our little module:

const path = require('path')
const fs = require('fs-extra')
const { createLogger, format, transports } = require('winston')
const { combine, timestamp, label, printf, colorize } = format
const packageJsonPath = path.join(process.cwd(), 'package.json')
const packageObj = fs.readJsonSync(packageJsonPath)
const name = packageObj.name || 'app'

// Custom format of the logs
const myFormat = printf(info => {
  let indent
  if (process.env.ENVIRONMENT && process.env.ENVIRONMENT !== 'production') {
    indent = 2
  }
  const message = JSON.stringify(info.message, false, indent)
  return `[${info.label}] ${info.timestamp} ${info.level}: ${message}`
})

// Custom logging handler
const logger = createLogger({
  format: combine(colorize(), label({ label: name }), timestamp(), myFormat),
  transports: [new transports.Console()],
})

// Override the base console log with winston
console.log = logger.info
console.warn = logger.warn
console.error = logger.error

But the error thrown is:

1|ms-item  | TypeError: self._addDefaultMeta is not a function
1|ms-item  |     at Console.DerivedLogger.(anonymous function) [as log] (/home/carmichael/code/g2/backend/ms_item/node_modules/winston/lib/winston/create-logger.js:80:14)
1|ms-item  |     at Server._src_app__WEBPACK_IMPORTED_MODULE_2__.default.listen (webpack-internal:///./server.js:14:11)
1|ms-item  |     at Object.onceWrapper (events.js:277:13)
1|ms-item  |     at Server.emit (events.js:189:13)
1|ms-item  |     at Server.<anonymous> (/usr/lib/node_modules/pm2/node_modules/@pm2/io/build/main/metrics/httpMetrics.js:147:37)
1|ms-item  |     at emitListeningNT (net.js:1304:10)
1|ms-item  |     at process._tickCallback (internal/process/next_tick.js:63:19)
1|ms-item  |     at Function.Module.runMain (internal/modules/cjs/loader.js:757:11)
1|ms-item  |     at startup (internal/bootstrap/node.js:283:19)
1|ms-item  |     at bootstrapNodeJSCore (internal/bootstrap/node.js:622:3)

Has anyone else encountered the same issue and found a resolution?

This is the best solution written using ES6+ syntax:

console.log = (...args) => logger.info.call(logger, ...args);
console.info = (...args) => logger.info.call(logger, ...args);
console.warn = (...args) => logger.warn.call(logger, ...args);
console.error = (...args) => logger.error.call(logger, ...args);
console.debug = (...args) => logger.debug.call(logger, ...args);

Ok I think i found a stable solution with the apply function from winston:

// Override the base console log with winston
console.log = function(){
  return logger.info.apply(logger, arguments)
}
console.error = function(){
  return logger.error.apply(logger, arguments)
}
console.info = function(){
  return logger.warn.apply(logger, arguments)
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM