Sagt, wohin der Fehler gehen soll. Folgende Meldungstypen sind möglich:
error_log()-Protokolltypen
0
message wird an das Log-Subsystem von PHP
gesendet, das abhängig von der Einstellung in der
Konfigurationsdirektive
error_log das Errorlogging des
Betriebssystems verwendet oder in eine Datei schreibt. Dies ist die
Standardeinstellung.
1
message wird via E-Mail an die Adresse
gesendet, die im Parameter destination
angegeben wurde. Dies ist der einzige Meldungstyp, bei dem der
vierte Parameter additional_headers
verwendet wird.
2
Diese Option ist nicht mehr verfügbar.
3
message wird an die Datei
destination angefügt. Ein Zeilenumbruch wird
nicht automatisch an das Ende des message-Strings
angehängt.
4
message wird direkt zum SAPI-Logging-Handler
gesendet.
destination
Das Ziel. Die jeweilige Bedeutung hängt wie oben beschrieben vom Parameter
message_type ab.
additional_headers
Die zusätzlichen Kopfzeilen. Dieser Parameter wird verwendet, wenn der
Parameter message_type auf 1
gesetzt ist. Dieser Meldungstyp verwendet dieselbe interne Funktion wie
mail().
Gibt bei Erfolg true zurück. Bei einem Fehler wird false zurückgegeben.
Wenn message_type null ist, gibt diese Funktion immer true zurück,
unabhängig davon, ob der Fehler protokolliert werden konnte oder nicht.
<?php // Sende eine Nachricht an das Serverlog, falls // keine Verbindung zur Datenbank herstellbar ist. if (!Ora_Logon($benutzername, $passwort)) { error_log("Die Oracle-Datenbank ist nicht erreichbar!", 0); }
// Benachrichtige den Administrator per E-Mail, falls kein FOO mehr verfügbar ist. if (!($foo = allocate_new_foo())) { error_log("Wir haben ein Problem: FOO ist alle!", 1, "operator@example.com"); }
// Eine weitere Möglichkeit, error_log() aufzurufen: error_log("Du hast Mist gebaut!", 3, "/var/tmp/meine-fehler.log"); ?>
error_log() ist nicht binär-sicher.
message wird durch ein Null-Zeichen abgeschnitten.
Tipp
message sollte keine Null-Zeichen enthalten. Es ist
zu beachten, dass message an eine Datei, das Syslog,
per Mail usw. gesendet werden kann. Geeignete Konvertierungs/Maskierungs-Funktionen,
base64_encode(), rawurlencode() oder
addslashes(), sind anzuwenden bevor
error_log() aufgerufen wird.
Advice to novices: This function works great along with "tail" which is a unix command to watch a log file live. There are versions of Tail for Windows too, like Tail for Win32 or Kiwi Log Viewer.
Using both error_log() and tail to view the php_error.log you can debug code without having to worry so much about printing debug messages to the screen and who they might be seen by.
Further Note: This works even better when you have two monitors setup. One for your browser and IDE and the other for viewing the log files update live as you go.
DO NOT try to output TOO LARGE texts in the error_log();
if you try to output massive amounts of texts it will either cut of the text at about 8ooo characters (for reasonable massive strings, < 32 K characters) or (for insanely massive strings, about 1.6 million characters) totally crash without even throwing an error or anything (I even put it in a try/catch without getting any result from the catch).
I had this problem when I tried to debug a response from a wp_remote_get(); all of my error_log() worked as they should, except for ONE of them... (-_-) After about a day of debugging I finally found out why & that's why I type this.
Apparently the response contained a body with over 1.6 million chars (or bytes? (whatever strlen() returns)).
If you have a string of unknown length, use this: $start_index = 0; $end_index = 8000; error_log( substr( $output_text , $start_index , $end_index ) );
Beware! If multiple scripts share the same log file, but run as different users, whichever script logs an error first owns the file, and calls to error_log() run as a different user will fail *silently*!
Nothing more frustrating than trying to figure out why all your error_log calls aren't actually writing, than to find it was due to a *silent* permission denied error!
It appears that the system log = stderr if you are running PHP from the command line, and that often stderr = stdout. This means that if you are using a custom error to both display the error and log it to syslog, then a command-line user will see the same error reported twice.
"It appears that the system log = stderr if you are running PHP from the command line"
Actually, it seems that PHP logs to stderr if it can't write to the log file. Command line PHP falls back to stderr because the log file is (usually) only writable by the webserver.
Relative paths are accepted as the destination of message_type 3, but beware that the root directory is determined by the context of the call to error_log(), which can change, so that one instance of error_log () in your code can lead to the creation of multiple log files in different locations.
In a WordPress context, the root directory will be the site's root in many cases, but it will be /wp-admin/ for AJAX calls, and a plugin's directory in other cases. If you want all your output to go to one file, use an absolute path.
You can easily filter messages sent to error_log() using "tail" and "grep" on *nix systems. This makes monitoring debug messages easy to see during development.
Be sure to "tag" your error message with a unique string so you can filter it using "grep":
when using error_log to send email, not all elements of an extra_headers string are handled the same way. "From: " and "Reply-To: " header values will replace the default header values. "Subject: " header values won't: they are *added* to the mail header but don't replace the default, leading to mail messages with two Subject fields.
When logging to apache on windows, both error_log and also trigger_error result in an apache status of error on the front of the message. This is bad if all you want to do is log information. However you can simply log to stderr however you will have to do all message assembly:
Another trick to post "HTML" mail body. Just add "Content-Type: text/html; charset=ISO-8859-1" into extra_header string. Of course you can set charset according to your country or Env or content.
In the case of missing your entries in the error_log file: When you use error_log in a script that does not produce any output, which means that you cannot see anything during the execution of the script, and when you wonder why there are no error_log entries produced in your error_log file, the reasons can be: - you did not configure error_log output in php.ini - the script has a syntax error and did therefore not execute
Note that since typical email is unencrypted, sending data about your errors over email using this function could be considered a security risk. How much of a risk it is depends on how much and what type of information you are sending, but the mere act of sending an email when something happens (even if it cannot be read) could itself imply to a sophisticated hacker observing your site over time that they have managed to cause an error.
Of course, security through obscurity is the weakest kind of security, as most open source supporters will agree. This is just something that you should keep in mind.
And of course, whatever you do, make sure that such emails don't contain sensitive user data.
If you have a problem with log file permission *silently* it's best to leave error_log directive unset so errors will be written in your Apache log file for current VirtualHost.
When error_log() unexpectedly uses stdout, you should check if the php.ini value for error_log is empty in your CLI environment. Something as simple as this might restore expected behavior: