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

6

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.

Michalmichalak answered 25/11, 2014 at 8:53 Comment(0)
C
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.

Caves answered 12/4, 2016 at 9:50 Comment(2)
This was helpful, but I needed to additionally add log_errors = On to the ini file.Waaf
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.Waaf
A
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

Amery answered 25/11, 2014 at 8:58 Comment(5)
i forgot to mention i'm using mac osMichalmichalak
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.Amery
Your answer missed that the OP wanted this for PHP's built-in web server, you posted the log locations for various Apache webservers.Caves
@thenickdude, yes you are right. This is the way SO works. Thanks for the comment.Amery
the question is where is the error log for when the webserver is started with something like php -S localhost:8000Goaltender
A
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 ).

Albacore answered 21/11, 2017 at 16:52 Comment(0)
T
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.

Transect answered 24/6, 2019 at 14:23 Comment(0)
M
-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
Munos answered 2/7, 2018 at 21:21 Comment(1)
The PHP built in server doesn't write to an apache error log.Above
F
-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
Forelimb answered 1/8, 2018 at 19:26 Comment(2)
This is not about php's built in web server. but about apache2.Tara
I hope the question is asked for php-buiit in server and I hope this should work.Forelimb

© 2022 - 2024 — McMap. All rights reserved.