【问题标题】:Filter command line logs from built-in PHP server从内置 PHP 服务器过滤命令行日志
【发布时间】:2020-05-25 06:43:56
【问题描述】:

PHP 有一个内置的file server 用于测试和开发目的。

在网络浏览器中打开页面后,服务器的控制台输出如下所示:

$ php -S localhost:8000
PHP 7.2.8 Development Server started at Sun Feb  9 23:09:17 2020
Listening on http://localhost:8000
Document root is /_PATHTOFILE_
Press Ctrl-C to quit.
[Sun Feb  9 23:09:19 2020] ::1:57697 [200]: /
[Sun Feb  9 23:09:19 2020] ::1:57698 [404]: /doesnt-exist.js - No such file or directory

我只对警告和错误感兴趣,如何过滤掉所有包含 [200] 的行?

我尝试了以下方法失败:

  • $ php -S localhost:8000 | grep --line-buffered --invert-match "200" | awk '{print $3}'
  • $ php -S localhost:8000 | awk '!/200/{print $3}'

这两个命令都不过滤行,它们只是记录以下内容:

[Sun Feb  9 23:09:19 2020] ::1:57697 [200]: /
[Sun Feb  9 23:09:19 2020] ::1:57698 [404]: /doesnt-exist.js - No such file or directory

我在这里错过了什么?

【问题讨论】:

  • 你试过了吗:grep -v "\[200\]";我不知道php 是否将所有消息a 发送到stdout 或stdout 和stderr 的混合;如果是后者:php -S localhost:8000 2>&1 | grep -v "\[200\]" ... ?
  • 鉴于您的php 调用的示例输出,您希望看到什么输出?您的awk 示例只是打印出匹配行的第三个字段...这是您真正想要显示的内容(只是第三个字段)?
  • @markp,谢谢!在做了更多 research 之后,我想我需要在 PHP 服务器仍在运行时使用 stdbufunbuffer 来处理 php 输出。

标签: php bash server terminal console


【解决方案1】:

php -S ... 的日志打印到标准错误而不是标准输出,您还需要过滤标准错误。 不过滤:

$ php -S localhost:8888
PHP 5.6.40-0+deb8u7 Development Server started at Sun Feb 23 12:58:30 2020
Listening on http://localhost:8888
Document root is /home/sorin/tmp
Press Ctrl-C to quit.
[Sun Feb 23 12:58:33 2020] this is an error
[Sun Feb 23 12:58:33 2020] 127.0.0.1:52825 [200]: /test.php
^C

将标准错误重定向到标准输出:

php -S localhost:8888 2>&1 | grep -v '\[200\]'
        [Sun Feb 23 12:59:41 2020] this is an error

测试文件:

cat test.php
<?php

error_log('this is an error');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-11-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    相关资源
    最近更新 更多