简体   繁体   English

使用exec从节点js中的stdout(php - monolog)收集和格式化日志

[英]collect and format logs from stdout(php - monolog) in node js with exec

my PHP call from node.js is : 我从node.js调用的PHP是:

const process = exec('php ' + phpScriptPath, (err, phpResponse, stderr) => {

            if (err){
                this.logger.error('failed:' , err);
            }
        });

        process.stdout.on('data', (data) => {
            this.logger.info(data.toString());
        });

I am sending the logs from the PHP script using monolog like following 我正在使用monolog从PHP脚本发送日志,如下所示

the logs look like the following : 日志如下所示:

{"name":"node","environment":"development","hostname":"local","level":50,"msg":"{\"message\":\"log message\",\"context\":{\"error\":\"error message\"},\"level\":400,\"level_name\":\"ERROR\",\"extra\":[]}\n","time":"2019-05-05T06:38:26.147Z","v":0}

using Bunyan , how can I format this message to be more readable 使用Bunyan,我如何格式化此消息以使其更具可读性

PHP monolog formatter : PHP monolog格式化程序:

 $formatter = new \Monolog\Formatter\JsonFormatter();
        $streamHandler = new \Monolog\Handler\StreamHandler('php://stdout', \Monolog\Logger::DEBUG);
        $streamHandler->setFormatter($formatter);
        $log->pushHandler($streamHandler);

2- Php has a diffrent level of logs, how can I check what is the level of logs in node, hoe can I format the message in stdout and get the log level. 2- Php具有不同级别的日志,如何检查节点中的日志级别,我可以在stdout中格式化消息并获取日志级别。

\\"level_name\\":\\"ERROR\\ " . \\"level_name\\":\\"ERROR\\ ”。 for example so I will find the "ERROR" string 例如,我会找到“ERROR”字符串

3- How can I fit between the log level in node and php. 3-我如何适应节点和php中的日志级别。 if my node log level is "error", I won't be able to print the stdout logs because my code is. 如果我的节点日志级别是“错误”,我将无法打印stdout日志,因为我的代码是。 "this.logger.error()" . "this.logger.error()" I need something more dynamic then hardcoded logs 我需要一些更加动态的硬编码日志

ok for my second question I found the solution: if I get PHP log as a JSON string I just can use the following from stdout: 好的第二个问题我找到了解决方案:如果我将PHP日志作为JSON字符串我可以使用以下来自stdout:

 var phplog = JSON.parse(data);
 console.log(phplog.level_name)
// will print ERROR

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM