简体   繁体   中英

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

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\":[]}\n","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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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