Невозможно распечатать стек ошибок на сервере Winston Node из внешнего интерфейса - PullRequest
0 голосов
/ 08 ноября 2019

Я пытаюсь получить стеки ошибок или сообщения, которые печатаются в console.log моего внешнего интерфейса с помощью winstonjs на моем бэкэнде (node.js).

Вот мой код:

    var appRoot = require('app-root-path');
    var winston = require('winston');
    const { createLogger, format, transports } = require('winston');
    const { combine, timestamp, label, printf, errors, json } = format;


    // instantiate a new Winston Logger with the settings defined above
    var logger = new winston.createLogger({
      format: combine(
        errors({ stack: true }),
        timestamp(),
        json(),
      ),
      transports: [
        new winston.transports.File({ level: 'error',
        filename: `${appRoot}/logs/app.log`,}),
        new winston.transports.Console()
      ],
      exitOnError: false, // do not exit on handled exceptions
    });

    // create a stream object with a 'write' function that will be used by `morgan`
    logger.stream = {
      write: function(message, encoding) {

        logger.error(new Error(message));
      },
    };

    module.exports = logger;

Он может печатать только что-то вроде этого:

{"level":"error","message":"::ffff:127.0.0.1 - - [08/Nov/2019:11:34:35 +0000] \"GET /getDwccDashboardByEd?account_name=testaccount1 HTTP/1.1\" 304 - \"http://localhost:4200/dashboardDwcc\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36\"\n","stack":"Error: ::ffff:127.0.0.1 - - [08/Nov/2019:11:34:35 +0000] \"GET /getDwccDashboardByEd?account_name=testaccount1 HTTP/1.1\" 304 - \"http://localhost:4200/dashboardDwcc\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.87 Safari/537.36\"\n\n at Object.write (D:\\0\\wandx\\saahas\\winston.js:49:18)\n at Array.logRequest (D:\\0\\wandx\\saahas\\node_modules\\morgan\\index.js:130:14)\n at listener (D:\\0\\wandx\\saahas\\node_modules\\on-finished\\index.js:169:15)\n at onFinish (D:\\0\\wandx\\saahas\\node_modules\\on-finished\\index.js:100:5)\n at callback (D:\\0\\wandx\\saahas\\node_modules\\ee-first\\index.js:55:10)\n at ServerResponse.onevent (D:\\0\\wandx\\saahas\\node_modules\\ee-first\\index.js:93:5)\n at ServerResponse.emit (events.js:215:7)\n at onFinish (_http_outgoing.js:686:10)\n at afterWrite (_stream_writable.js:501:3)\n at processTicksAndRejections (internal/process/task_queues.js:81:21)","timestamp":"2019-11-08T11:34:35.244Z"}

, что указывает на logger.error(new Error(message));

Но я хочу такие ошибки, как:

`DashboardDwccComponent.html:231 ERROR TypeError: Cannot read property 'length' of undefined
at Object.eval [as updateDirectives] (DashboardDwccComponent.html:252)
at Object.debugUpdateDirectives [as updateDirectives] (core.js:23911)
at checkAndUpdateView (core.js:23307)
at callViewAction (core.js:23548)
at execEmbeddedViewsAction (core.js:23511)
at checkAndUpdateView (core.js:23308)
at callViewAction (core.js:23548)
at execEmbeddedViewsAction (core.js:23511)
at checkAndUpdateView (core.js:23308)
at callViewAction (core.js:23548)`

Есть ли способ к этому?

...