2

I want to log the console logs whether the expect failing or any exception coming into the log file. How can i do that without using try and catch. In LOG4j for java there is rootlogger which automatically logs all the system information logs into the file. Can same be done for log4JS for javascript

log4js.configure({
  appenders: [
    {
      type: "clustered",
      appenders: [
        {
          type: "dateFile",
          filename: "log/access.log",
          pattern: "-yyyy-MM-dd",
          category: "http"
        },
        {
          type: "file",
          filename: "log/app.log",
          maxLogSize: 10485760,
          numBackups: 3
        },
        {
          type: "logLevelFilter",
          level: "ERROR",
          appender: {
            type: "file",
            filename: "log/errors.log"
          }
        }
      ]
    }
  ]
})
anuvrat singh
  • 411
  • 1
  • 7
  • 20

1 Answers1

5

You can override the default console.log functions to point to your logger e.g.

const logger = log4js.getLogger('cheese');
console.log = (msg) => logger.trace(msg);

You can do similar things for other functions e.g. console.debug etc.

That said. You would be better off calling logger explicitly.

basarat
  • 261,912
  • 58
  • 460
  • 511
  • So using console.log = (msg) => logger.trace(msg) it will print whatever we are printing like console.log("My Name") but what about the stacktrace which is not part of the script but thrown by the driver/jasmine – anuvrat singh Jun 20 '17 at 07:57