If you are trying to capture PHP errors to a text file on IIS ensure that two things are set.
1) Only one error log option is set. IE:
; Log errors to specified file.
error_log = "c:\php\errorlog.txt"
; Log errors to syslog (Event Log on NT, not valid in Windows 95).
;error_log = syslog
2) The IUSR account has write and modify permissions to e rrorlog.txt .
error_log
(PHP 4, PHP 5)
error_log — Trimite undeva un mesaj de eroare
Descrierea
Trimite un mesaj de eroare în log-ul de erori al server-ului web, spre un port TCP, sau într-un fişier.
Parametri
- message
-
Mesajul de eroare ce trebuie logat.
- message_type
-
Indică unde ar trebui să fie trimisă eroarea. Tipurile posibile ale mesajului sunt următoarele:
Tipurile de logări error_log() 0 message este transmis log-ului de sistem al PHP utilizând mecanismul de logare al Sistemului de Operare, sau un fişier, în dependenţă de setările directivei de configurare error_log. Aceasta este opţiunea implicită. 1 message este transmis prin email spre adresa indicată în parametrul destination . Acesta este unicul tip de mesaj, unde se utilizează al patrulea parametru extra_headers . 2 Această opţiune nu mai există deja. 3 message este adăugat la sfârşitul fişierului destination . Sfârşitul de linie nu este adăugat automat la string-ul message . 4 message este transmis direct spre funcţia de logare SAPI. - destination
-
Destinaţia. Semnificaţia acesteia depinde de valoarea parametrului message_type descris mai sus.
- extra_headers
-
Antete suplimentare. Acestea sunt utilizate în caz că parametrul message_type are valoarea 1. Acest tip de mesaj utilizează aceeaşi funcţie internă ca şi mail().
Valorile întroarse
Întoarce valoarea TRUE în cazul succesului sau FALSE în cazul eşecului.
Istoria schimbărilor
| Versiunea | Descriere |
|---|---|
| 5.2.7 | A fost adăugată valoarea 4 pentru message_type . |
Exemple
Example #1 Exemple error_log()
<?php
// Trimite o înştiinţare prin intermediul log-ului server-ului în caz că nu este
// posibilă conectarea la baza de date.
if (!Ora_Logon($username, $password)) {
error_log("Baza de date Oracle nu este disponibilă!", 0);
}
// Înştiinţează administratorul prin email dacă rămânem fără FOO
if (!($foo = allocate_new_foo())) {
error_log("Nevoie mare, nu mai avem FOO!", 1,
"operator@example.com");
}
// Un alt mod de a apela error_log():
error_log("Ai dat-o în bară!", 3, "/var/tmp/my-errors.log");
?>
error_log
16-Nov-2008 04:20
28-Oct-2008 04:03
<?php
//Multiline error log class
// ersin güvenç 2008 eguvenc@gmail.com
//For break use "\n" instead '\n'
Class log {
//
const USER_ERROR_DIR = '/home/site/error_log/Site_User_errors.log';
const GENERAL_ERROR_DIR = '/home/site/error_log/Site_General_errors.log';
/*
User Errors...
*/
public function user($msg,$username)
{
$date = date('d.m.Y h:i:s');
$log = $msg." | Date: ".$date." | User: ".$username."\n";
error_log($log, 3, self::USER_ERROR_DIR);
}
/*
General Errors...
*/
public function general($msg)
{
$date = date('d.m.Y h:i:s');
$log = $msg." | Date: ".$date."\n";
error_log($msg." | Tarih: ".$date, 3, self::GENERAL_ERROR_DIR);
}
}
$log = new log();
$log->user($msg,$username); //use for user errors
//$log->general($msg); //use for general errors
?>
17-Jun-2008 05:37
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:
LogToApache($Message) {
$stderr = fopen('php://stderr', 'w');
fwrite($stderr,$Message);
fclose($stderr);
}
16-Feb-2008 11:32
Be carefull. Unexpected PHP dies when 2GByte of file log reached (on systems having upper file size limit).
A work aorund is rotate logs :)
01-Jan-2008 02:16
"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.
11-Oct-2007 10:00
On a Mac running OS X, for the error logging to work I needed to put this in my php.ini:
error_log = /tmp/php_errors.log
Attempting to put the log in other locations did not work, probably due to permission issues.
27-Jun-2007 01:05
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.
02-Nov-2006 11:28
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!
28-Aug-2006 10:33
Hi !
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.
EG: Error_log("<html><h2>stuff</h2></html>",1,"eat@joe.com","subject :lunch\nContent-Type: text/html; charset=ISO-8859-1");
Enjoy !
26-Aug-2005 08:52
Beware the size of your custom error_log!
Once it exceeds 2GB the function errors, ending your script at the error_log() line. I'm sure this differs from OS to OS, but I have seen it die writing to ext2 under modern Linux systems.
25-Jul-2005 09:04
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.
21-Jul-2005 05:39
It appears that error_log() only logs the first line of multi-line log messages. To log a multi-line message, either log each line individually or write the message to another file.
20-Apr-2005 04:21
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
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.
<?php
error_log("sometext", 1, "zigzag@my.domain",
"Subject: Foo\nFrom: Rizzlas@my.domain\n");
?>
---------------%<-----------------------
To: zigzag@my.domain
Envelope-to: zigzag@my.domain
Date: Fri, 28 Mar 2003 13:29:02 -0500
From: Rizzlas@my.domain
Subject: PHP error_log message
Subject: Foo
Delivery-date: Fri, 28 Mar 2003 13:29:03 -0500
sometext
---------------%<---------------------
quoth the docs: "This message type uses the same internal function as mail() does."
mail() will also fail to set a Subject field based on extra_header data - instead it takes a seperate argument to specify a "Subject: " string.
php v.4.2.3, SunOS 5.8
