23

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Bryan Estrito
  • 673
  • 2
  • 6
  • 17

6 Answers6

32

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.

Cameron
  • 995
  • 10
  • 16
thenickdude
  • 1,640
  • 1
  • 17
  • 18
  • This was helpful, but I needed to additionally add `log_errors = On` to the ini file. – Progrock Nov 29 '17 at 18:46
  • 2
    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. – Progrock Nov 29 '17 at 18:48
6

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Pupil
  • 23,834
  • 6
  • 44
  • 66
  • i forgot to mention i'm using mac os – Bryan Estrito Nov 25 '14 at 09:00
  • 2
    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. – Pupil Feb 26 '16 at 06:34
  • 5
    Your answer missed that the OP wanted this for PHP's *built-in web server*, you posted the log locations for various Apache webservers. – thenickdude Apr 12 '16 at 08:16
  • @thenickdude, yes you are right. This is the way SO works. Thanks for the comment. – Pupil Apr 12 '16 at 08:35
  • 3
    the question is where is the error log for when the webserver is started with something like `php -S localhost:8000` – santiago arizti Dec 15 '16 at 02:08
2

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

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
kenorb
  • 155,785
  • 88
  • 678
  • 743
1

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
-1

To update for macOS v10.13.5 (High Sierra): this worked for me with no need to change any PHP defaults. Just use

error_log('*** Notice this ***');

and tail the error log:

tail -f /var/log/apache2/error_log
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
dhc
  • 647
  • 8
  • 17
-3

If you are using macOS or Ubuntu, this the way you can easily look into the error log:

tail -f /var/log/apache2/error_log

This will give you the error log in realtime or you can use this, which will give the last error log entries:

tail /var/log/apache2/error_log
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Balaji.J.B
  • 618
  • 7
  • 14