Bryan Estrito
Bryan Estrito

Reputation: 673

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

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.

Upvotes: 23

Views: 35409

Answers (6)

3lit3h4XX0r666
3lit3h4XX0r666

Reputation: 11

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.

Upvotes: 1

Balaji.J.B
Balaji.J.B

Reputation: 636

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

Upvotes: -3

dhc
dhc

Reputation: 657

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

Upvotes: -1

kenorb
kenorb

Reputation: 166389

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

Upvotes: 2

Pupil
Pupil

Reputation: 23958

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

Upvotes: 6

thenickdude
thenickdude

Reputation: 1660

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.

Upvotes: 32

Related Questions