douke9545 2019-05-05 06:50
浏览 209
已采纳

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

my PHP call from node.js is :

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

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\":[]}
","time":"2019-05-05T06:38:26.147Z","v":0}

using Bunyan , how can I format this message to be more readable

PHP monolog formatter :

 $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.

\"level_name\":\"ERROR\" . for example so I will find the "ERROR" string

3- How can I fit between the log level in node and php. if my node log level is "error", I won't be able to print the stdout logs because my code is. "this.logger.error()" . I need something more dynamic then hardcoded logs

  • 写回答

1条回答 默认 最新

  • dongxiaoyan4388 2019-05-05 06:55
    关注

    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:

     var phplog = JSON.parse(data);
     console.log(phplog.level_name)
    // will print ERROR
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 写uniapp时遇到的问题
  • ¥15 matlab有限元法求解梁带有若干弹簧质量系统的固有频率
  • ¥15 找一个网络防御专家,外包的
  • ¥100 能不能让两张不同的图片md5值一样,(有尝)
  • ¥15 informer代码训练自己的数据集,改参数怎么改
  • ¥15 请看一下,学校实验要求,我需要具体代码
  • ¥50 pc微信3.6.0.18不能登陆 有偿解决问题
  • ¥20 MATLAB绘制两隐函数曲面的交线
  • ¥15 求TYPCE母转母转接头24PIN线路板图
  • ¥100 国外网络搭建,有偿交流