What is the PHP built-in server error log location?

30,501

Solution 1

The built-in webserver doesn't log anywhere by default, so you need to provide a php.ini for it to customise this. For example, if you created a file called php.ini with this content:

error_log = /Users/me/test.log
log_errors = on
date.timezone = UTC

Then you can start PHP's built-in webserver like this:

php -S 127.0.0.1:8080 -c php.ini

And error_log() calls will be logged to the file you've specified.

Solution 2

Yes, PHP has built-in error log functionality. PHP logs errors to this file automatically.

If you want to log errors, use the function error_log().

The file's location changes depending upon the environment.

E.g., in Ubuntu 12.04 (Precise Pangolin), it’s

var/log/php_errors.log

In XAMPP Windows,

\xampp\php\logs\php_errors.log

In Mac OS X,

/var/log/apache2/php_errors.log

Solution 3

When using PHP's built-in server on macOS, you need to specify error_log in your php.ini configuration file (php -i | grep php.ini).

If you decide with syslog (instead of a log file) such as:

error_log = syslog

Then to dump the logs, you can use log command on macOS, e.g.

log stream --predicate 'processImagePath contains "php"'

Otherwise, use some specific file path for the error log (e.g. /usr/local/var/log/php-error.log ).

Solution 4

I primarily know Linux, but AFAIK this works the same on whatever system you can run PHP.

I recently spent an unreasonable amount of time getting Linux to run on a "obsolete" Mac Mini from 2009. I don't know how anyone tolerates that manufactured obsolescence. Android is just is bad, hiding behind that "based on Linux" nonsense. I wonder how many lawyers it took to figure that one out? Of course, GPL doesn't say anything about the hardware!

Thanks to them, when mobile devices inevitably replace laptops and desktops over the next decade, the threat of the ideas behind free software will have been mostly eliminated. They should put that kind of engineering to work in consumer smoke alarm technology.

Anyway, enough about that. I'll attempt to return to the topic and teach you how to do this in a way that will give you the tools to complete similar different tasks in the future. It's like that old saying goes, "Teach a man to fish and won't be able to sell him anymore fish"

I always like to begin with a man [command] or [command] --help . If --help or -h doesn't work, try to pass it invalid input. That will usually get it talking. Be careful at this step; it's easy to get stuck reading man pages for several hours. Try not to forget any time obligations you might be under.

php --help

Find the option to set ini variables:

-d foo[=bar]     Define INI entry foo with value 'bar'

Reading an example php.ini, we find the settings of interest.

; error_reporting
;   Default Value: E_ALL & ~E_NOTICE & ~E_STRICT & ~E_DEPRECATED
;   Development Value: E_ALL
;   Production Value: E_ALL & ~E_DEPRECATED & ~E_STRICT

///

; Log errors to specified file. PHP's default behavior is to leave this value
; empty.
; http://php.net/error-log
; Example:
;error_log = php_errors.log
; Log errors to syslog (Event Log on Windows).
;error_log = syslog

All the other defaults look ok so... let's try:

php -d error_reporting=E_ALL -d error_log=/desired/path/to/error.log -S 0.0.0.0:9999

You might notice the log printing to standard error from here. Alternatively, you could redirect that by adding 2> /path/to/error.log to the end of the above command. Simpler, but then you wouldn't have learned about the -d options to set values from file php.ini and -c to use a custom file, but you'd have learned about output redirection which I'd say is a far more important concept with countless applications.

Share:
30,501

Related videos on Youtube

Bryan Estrito
Author by

Bryan Estrito

No comment.

Updated on July 09, 2022

Comments

  • Bryan Estrito
    Bryan Estrito almost 2 years

    I don't know if there's any. But does the PHP built in a web server also save its error logs in a file? For tailing purposes, like when creating virtual host in Apache.

    I'm using Mac OS X.

  • Bryan Estrito
    Bryan Estrito over 9 years
    i forgot to mention i'm using mac os
  • Pupil
    Pupil over 8 years
    Downvoters, your downvoting is most welcome. I am happy that my answer needs improvements/corrections. But, please post comments about my mistakes. You are my true teachers.
  • thenickdude
    thenickdude about 8 years
    Your answer missed that the OP wanted this for PHP's built-in web server, you posted the log locations for various Apache webservers.
  • Pupil
    Pupil about 8 years
    @thenickdude, yes you are right. This is the way SO works. Thanks for the comment.
  • santiago arizti
    santiago arizti over 7 years
    the question is where is the error log for when the webserver is started with something like php -S localhost:8000
  • Progrock
    Progrock over 6 years
    This was helpful, but I needed to additionally add log_errors = On to the ini file.
  • Progrock
    Progrock over 6 years
    Additionally I tried a relative path in the php.ini for the error log. When the built in server uses the -t directory flag, the path is relative to there, not the php.ini.
  • jrswgtr
    jrswgtr almost 6 years
    This is not about php's built in web server. but about apache2.
  • Balaji.J.B
    Balaji.J.B almost 6 years
    I hope the question is asked for php-buiit in server and I hope this should work.
  • John Hunt
    John Hunt over 5 years
    The PHP built in server doesn't write to an apache error log.