Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/node.js/36.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使Pino logger传输功能与node.js上CLI应用程序中的Inquirer.js库一起工作_Node.js_Logging_Command Line Interface - Fatal编程技术网

使Pino logger传输功能与node.js上CLI应用程序中的Inquirer.js库一起工作

使Pino logger传输功能与node.js上CLI应用程序中的Inquirer.js库一起工作,node.js,logging,command-line-interface,Node.js,Logging,Command Line Interface,Inquirer.js处理用户对控制台的输入。当将Pino logger传输功能与Inquirer.js结合使用时,无法重定向提供给stdout的输出流 应用程序有一个index.js文件: 'use strict'; const inquirer = require('inquirer'); const { getLogger } = require(`./logger`); const logger = getLogger(); const test = function (answe

Inquirer.js处理用户对控制台的输入。当将Pino logger传输功能与Inquirer.js结合使用时,无法重定向提供给stdout的输出流

应用程序有一个index.js文件:

'use strict';

const inquirer = require('inquirer');
const { getLogger } = require(`./logger`);

const logger = getLogger();

const test = function (answer) {
    logger.info(JSON.parse(answer));
};

const questions = [
    {
        type: 'input',
        name: 'question',
        message: 'question to user',
        default: function () {
            return '';
        },
    },
];

inquirer.prompt(questions).then((answer) => {
    test(JSON.stringify(answer, null, '  '));
});
文件logger.js:

'use strict';

let n = 0;
const logger = require(`pino`)({
    name: `test-pino`,
    level: process.env.LOG_LEVEL || `info`,
    mixin() {
        return { line: ++n };
    },
});

module.exports = {
    logger,
    getLogger(options = {}) {
        return logger.child(options);
    },
};
和文件transport.js

'use strict';

const split = require('split2');
const pump = require('pump');
const through = require('through2');

const myTransport = through.obj(function (chunk, enc, cb) {
    // do the necessary
    console.log(chunk);
    cb();
});

pump(process.stdin, split(JSON.parse), myTransport);

在控制台中,我使用命令
LOG_LEVEL=debug node index.js | node transport.js
node index.js | node transport.js
。因此,transport.js中的传输功能无法工作。但如果您不使用Inquirer.js,那么一切都很好:

index.js:

'use strict';

const { getLogger } = require(`./logger`);

const logger = getLogger();

const test = function () {
    logger.info('it works');
};

test();
控制台输出:

{
  level: 30,
  time: 1602263541760,
  pid: 14248,
  hostname: 'DESKTOP-9IG2DCD',
  name: 'test-pino',
  line: 1,
  msg: 'it works'
}
如何让pino和Inquirer.js一起工作