使用Winston记录Moleculer服务中未处理的错误

时间:2019-09-22 17:46:29

标签: winston moleculer

这是我的测试服务和molterr配置,在这里我试图以不同的传输方式插入Winston logger,并尝试记录未处理的异常,但是没有运气。我同时尝试了#if canImport(UIKit) // Code specific to platforms where UIKit is available #endif 和特殊运输方式

exceptionHandlers

handleExceptions: true, humanReadableUnhandledException: true

moleculer.config.js

const config = require('config') const winston = require('winston') const { extend } = require('moleculer').Logger module.exports = { transporter: config.get('transporter.nats.uri'), serializer: config.get('transporter.serializer'), namespace: config.get('namespace'), logger: bindings => extend(winston.createLogger({ format: winston.format.combine( winston.format.label({ label: bindings }), winston.format.timestamp(), winston.format.json() ), transports: [ //new winston.transports.Console({ // handleExceptions: true, // humanReadableUnhandledException: true //}), new winston.transports.File({ name: 'service-log', filename: '/tmp/iproc.log', timestamp: true, colorize: true, prettyPrint: true, json: true, maxsize: 5242880 }) ], exceptionHandlers: [ new winston.transports.File({ name: 'service-log', filename: '/tmp/iproc-error.log', timestamp: true, colorize: true, prettyPrint: true, json: true, maxsize: 5242880 }) ] })) } 脚本部分

package.json

"scripts": { "dev": "NODE_ENV=dev moleculer-runner service.js --hot" },

services.js

启动服务

    module.exports = {
        name: "iproc",
        version: '1.0.0',
        actions: {
            test (ctx) {
                this.logger.info('hello from test: ')

                throw new Error('Some error')
            },
        }
    }

连接到Molecularr控制台

yarn dev

然后在Molecularr控制台中运行

moleculer connect nats://localhost:4222 --ns dev

预计call iproc.test 中会出现“某些错误”,但是没有。

Moleculer可以自行处理未处理的异常吗?如何使用所选的logger + transport记录这些错误?

请注意,当我在一个简单的nodejs项目中测试Winston时,记录未处理的异常可以按预期工作。

0 个答案:

没有答案