Halp--PHP logging
-
So I'm writing an api implementation in PHP (I know, , PHP = TRWTF and all that). I've been trying to add debug logging...and the files just aren't showing up anywhere.
Things I've tried:
error_log($log_message, 3, __DIR__ . "/error_log.txt");
file_put_contents($fully_qualified_filename, $message, FILE_APPEND);
In no case do I get any files written, even when I manually trigger those lines. Halp?
-
@Benjamin-Hall Have you checked the regular error log (probably in /var/log)? Have you checked your PHP configuration to make sure that error reporting is turned on for all error types? My first guess would be permissions, but the error log will tell you for sure.
-
@Dragnslcr I'm working on windows (XAMPP stack), but I've checked apache's logs and can't seem to find any default logging location for PHP. There may be a permissions error since my web root is in my development directory (not a subfolder of the apache installation)...
-
@Benjamin-Hall IIRC PHP doesn't log anything to Apache. Apache might log fatal errors, but that's only because PHP had sent back an error code with the error, which Apache's layer caught and logged, as it would have done regardless of what process sent back that error code. PHP has its own logging, probably in the same place you have php.ini and/or the PHP binary.
-
@Benjamin-Hall
The other question I would have is whether the PHP script is even executing - what are your php.ini settings for error reporting? If this is development, might be worthwhile to set it to E_ALL and display_errors = OnIf you have a parse error (yay missing semicolons) then your error_log() function will never get called, and if php.ini isn't set to output that anywhere, all you wind up seeing is a mysterious 500 error going to the client with a blank or partially rendered page of doom.
-
@Benjamin-Hall Create a page that just has
<?php phpinfo(); ?>
and you can see all of the configuration settings.
-
@izzion I have all errors turned on in the php.ini. Checked that a couple times.
That's the funny thing. I'm seeing 200 OK responses and no log even when the only executable instruction is a call to the logging function. Since this is an api, I'm currently calling it from
Invoke-webRequest
in powershell (everything's local right now).
-
@Benjamin-Hall Have you checked the Windows Error Log? Or Windows Event Viewer, to give it the correct name.
-
@RaceProUK I can't find anything in there with the right timestamps or marked as coming from php.
-
I think I made it work. Seems it didn't like my path specifiers. Ended up with
file_put_contents(__DIR__.$filename, $message, FILE_APPEND)
Probably still the wrong way of doing things, but it seems to work now for debug purposes. I'll need to strip it out for production, but...
-
@Benjamin-Hall
Oh, that's easy...file_put_contents(__DIR__.$filename, $message, FILE_APPEND) /* to do - remove this debug in production */
-
@Benjamin-Hall Did your full path use forward slashes? Your
"/error_log.txt"
seems to indicate yes. Are you aware that Windows uses backslashes for the directory separator?
-
@Dragnslcr I'm so used to languages with good path libraries....I tried both though. Didn't seem to make a difference.
-
@Benjamin-Hall said in Halp--PHP logging:
I'm so used to languages with good path libraries....
You'll love PHP ... NOT! Absolutely not!
-
@Dragnslcr said in Halp--PHP logging:
@Benjamin-Hall Did your full path use forward slashes? Your
"/error_log.txt"
seems to indicate yes. Are you aware that Windows uses backslashes for the directory separator?In sane languages, that shouldn't matter.
So obviously that's the problem.
(Or maybe, it's seeing
DIRECTORY\\/file
and escaping the forward slash because PHP!)