snorpey
snorpey

Reputation: 2347

Filter command line logs from built-in PHP server

PHP has a built in file server for testing and development purposes.

After a page was opened in a web browser, the console output of the server looks like this:

$ 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

How can I filter out all lines that include [200], as I'm only interested in warnings and errors?

I have unsucessfully tried the following:

Both commands don't filter the lines, they just log the following:

[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

What am I missing here?

Upvotes: 0

Views: 181

Answers (1)

Sorin
Sorin

Reputation: 5415

Logs of php -S ... are printed to stderr not stdout, you need to filter stderr also. Without filtering:

$ 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

Redirecting stderr to stdout:

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

Test file:

cat test.php
<?php

error_log('this is an error');

Upvotes: 1

Related Questions