最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Override console.log|error with winston no longer working - Stack Overflow

programmeradmin0浏览0评论

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?

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?

Share Improve this question asked May 12, 2019 at 8:48 user1037355user1037355
Add a comment  | 

2 Answers 2

Reset to default 25

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)
}
发布评论

评论列表(0)

  1. 暂无评论