Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 992
John Avatar asked May 12 '19 08:05

John


People also ask

How do you override a console?

Overriding console. To override a console method, we just need to redefine how the method is executed. You'll need to wrap your code to prevent the access of other functions to the private (original) method. Note: The previous code would do the final trick. You can use it to override other properties.

How do I change the log level in Winston?

You can change the logging level in runtime by modifying the level property of the appropriate transport: var log = new (winston. Logger)({ transports: [ new (winston. transports.

Can you delete console log?

Use the short cut Ctrl + L to clear the console. Use the clear log button on the top left corner of the chrome dev tools console to clear the console.

How do I view Winston logs?

Go to the example. log file in the logs folder to view the log. Winston allows you to implement multiple logging transports, i.e., a log can be recorded to a file, console, or database. The Logger configuration below logs to a console and a file.


2 Answers

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);
like image 60
Neilor Caldeira Avatar answered Sep 20 '22 22:09

Neilor Caldeira


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)
}
like image 34
John Avatar answered Sep 17 '22 22:09

John