error_log

(PHP 4, PHP 5, PHP 7, PHP 8)

error_log β€” ΠžΡ‚ΠΏΡ€Π°Π²Π»ΡΠ΅Ρ‚ сообщСниС ΠΎΠ± ошибкС Π·Π°Π΄Π°Π½Π½ΠΎΠΌΡƒ ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊΡƒ ошибок

ОписаниС

function error_log(
Β Β Β Β string $message,
Β Β Β Β int $message_type = 0,
Β Β Β Β ?string $destination = null,
Β Β Β Β ?string $additional_headers = null
): bool

Ѐункция отправляСт сообщСниС ΠΎΠ± ошибкС Π² ΠΆΡƒΡ€Π½Π°Π» ошибок Π²Π΅Π±-сСрвСра ΠΈΠ»ΠΈ Π² Ρ„Π°ΠΉΠ».

Бписок ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ΠΎΠ²

message

Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ ΠΎΠ± ошибкС, ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠ΅ трСбуСтся Π·Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ.

message_type

ΠžΠΏΡ€Π΅Π΄Π΅Π»ΡΠ΅Ρ‚, ΠΊΡƒΠ΄Π° ΠΎΡ‚ΠΏΡ€Π°Π²Π»ΡΡ‚ΡŒ ΠΎΡˆΠΈΠ±ΠΊΡƒ. ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ ΡΠ»Π΅Π΄ΡƒΡŽΡ‰ΠΈΠ΅ Ρ‚ΠΈΠΏΡ‹ сообщСний ΠΎΠ± ΠΎΡˆΠΈΠ±ΠΊΠ°Ρ…:

Π’ΠΈΠΏΡ‹ ΠΆΡƒΡ€Π½Π°Π»ΠΎΠ² Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ error_log()
0 Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ message отправляСтся Π² систСмный рСгистратор PHP, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ пСрСнаправляСт ΠΎΡˆΠΈΠ±ΠΊΡƒ Π² ΠΌΠ΅Ρ…Π°Π½ΠΈΠ·ΠΌ систСмного логирования ΠΎΠΏΠ΅Ρ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΉ систСмы ΠΈΠ»ΠΈ записываСт Π² Ρ„Π°ΠΉΠ», Π² зависимости ΠΎΡ‚ значСния Π΄ΠΈΡ€Π΅ΠΊΡ‚ΠΈΠ²Ρ‹ error_log Π² ΠΊΠΎΠ½Ρ„ΠΈΠ³ΡƒΡ€Π°Ρ†ΠΈΠΎΠ½Π½ΠΎΠΌ Ρ„Π°ΠΉΠ»Π΅. ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ это Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠΎ ΡƒΠΌΠΎΠ»Ρ‡Π°Π½ΠΈΡŽ.
1 Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ message отправляСтся ΠΏΠΎ элСктронной ΠΏΠΎΡ‡Ρ‚Π΅ Π½Π° адрСс, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ установили Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π΅ destination. Π­Ρ‚ΠΎ СдинствСнный Ρ‚ΠΈΠΏ сообщСния, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹ΠΉ ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ Ρ‡Π΅Ρ‚Π²Ρ‘Ρ€Ρ‚Ρ‹ΠΉ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ β€” additional_headers.
2 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€ большС Π½Π΅ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°Π΅Ρ‚ это Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅.
3 Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ message добавляСтся Π² Ρ„Π°ΠΉΠ», ΠΏΡƒΡ‚ΡŒ ΠΊ ΠΊΠΎΡ‚ΠΎΡ€ΠΎΠΌΡƒ ΡƒΠΊΠ°Π·Π°Π»ΠΈ Π² ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π΅ destination. Π‘ΠΈΠΌΠ²ΠΎΠ» Π½ΠΎΠ²ΠΎΠΉ строки Π½Π΅ добавляСтся автоматичСски Π² ΠΊΠΎΠ½Π΅Ρ† строки сообщСния message.
4 Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ message отправляСтся нСпосрСдствСнно Π² SAPI-ΠΎΠ±Ρ€Π°Π±ΠΎΡ‚Ρ‡ΠΈΠΊ сообщСний, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ трСбуСтся Π·Π°ΠΏΠΈΡΠ°Ρ‚ΡŒ Π² ΠΆΡƒΡ€Π½Π°Π».
destination

НазначСниС. Π—Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ зависит ΠΎΡ‚ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° message_type.

additional_headers

Π”ΠΎΠΏΠΎΠ»Π½ΠΈΡ‚Π΅Π»ΡŒΠ½Ρ‹Π΅ Π·Π°Π³ΠΎΠ»ΠΎΠ²ΠΊΠΈ. Ѐункция ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€, ΠΊΠΎΠ³Π΄Π° Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ ΠΏΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Π° message_type ΡƒΡΡ‚Π°Π½Π°Π²Π»ΠΈΠ²Π°ΡŽΡ‚ Ρ€Π°Π²Π½Ρ‹ΠΌ 1. Π’ΠΈΠΏ сообщСния ΠΈΡΠΏΠΎΠ»ΡŒΠ·ΡƒΠ΅Ρ‚ Ρ‚Ρƒ ΠΆΠ΅ Π²Π½ΡƒΡ‚Ρ€Π΅Π½Π½ΡŽΡŽ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ, Ρ‡Ρ‚ΠΎ ΠΈ функция mail().

Π’ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅ΠΌΡ‹Π΅ значСния

Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ true, Ссли Π²Ρ‹ΠΏΠΎΠ»Π½ΠΈΠ»Π°ΡΡŒ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ, ΠΈΠ»ΠΈ false, Ссли Π²ΠΎΠ·Π½ΠΈΠΊΠ»Π° ошибка. Ѐункция Π²ΠΎΠ·Π²Ρ€Π°Ρ‰Π°Π΅Ρ‚ логичСскоС Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ true нСзависимо ΠΎΡ‚ Ρ€Π΅Π·ΡƒΠ»ΡŒΡ‚Π°Ρ‚Π° логирования ошибки, Ссли Ρ‚ΠΈΠΏ сообщСния message_type Ρ€Π°Π²Π΅Π½ 0.

Бписок измСнСний

ВСрсия ОписаниС
8.0.0 ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρ‹ destination ΠΈ additional_headers Ρ‚Π΅ΠΏΠ΅Ρ€ΡŒ ΠΏΡ€ΠΈΠ½ΠΈΠΌΠ°ΡŽΡ‚ Π·Π½Π°Ρ‡Π΅Π½ΠΈΠ΅ null.

ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹

ΠŸΡ€ΠΈΠΌΠ΅Ρ€ #1 ΠŸΡ€ΠΈΠΌΠ΅Ρ€Ρ‹ использования Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ error_log()

<?php

// ΠžΡ‚ΠΏΡ€Π°Π²Π»ΡΠ΅Ρ‚ ΡƒΠ²Π΅Π΄ΠΎΠΌΠ»Π΅Π½ΠΈΠ΅ Ρ‡Π΅Ρ€Π΅Π· Π»ΠΎΠ³ сСрвСра, Ссли Π½Π΅ получаСтся
// ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡ΠΈΡ‚ΡŒΡΡ ΠΊ Π±Π°Π·Π΅ Π΄Π°Π½Π½Ρ‹Ρ…
if (!Ora_Logon($username, $password)) {
error_log("Π‘Π°Π·Π° Π΄Π°Π½Π½Ρ‹Ρ… Oracle нСдоступна!", 0);
}

// УвСдомляСт администратора ΠΏΠΎ элСктронной ΠΏΠΎΡ‡Ρ‚Π΅, Ссли Π·Π°ΠΊΠΎΠ½Ρ‡ΠΈΠ»ΠΈΡΡŒ FOO
if (!($foo = allocate_new_foo())) {
error_log("Π‘ΠΎΠ»ΡŒΡˆΠ°Ρ Π±Π΅Π΄Π°, Ρƒ нас Π·Π°ΠΊΠΎΠ½Ρ‡ΠΈΠ»ΠΈΡΡŒ FOO!", 1,
"operator@example.com");
}

// Π”Ρ€ΡƒΠ³ΠΎΠΉ способ Π²Ρ‹Π·Π²Π°Ρ‚ΡŒ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΡŽ error_log():
error_log("Π’Ρ‹ ошиблись!", 3, "/var/tmp/my-errors.log");

?>

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ

Π’Π½ΠΈΠΌΠ°Π½ΠΈΠ΅

Ѐункция error_log() Π½Π΅ Π±ΠΈΠ½Π°Ρ€Π½ΠΎ-бСзопасна. Π‘ΠΎΠΎΠ±Ρ‰Π΅Π½ΠΈΠ΅ message обрСзаСтся ΠΏΠΎ null-символу.

Подсказка

ΠŸΠ°Ρ€Π°ΠΌΠ΅Ρ‚Ρ€Ρƒ message нСльзя ΡΠΎΠ΄Π΅Ρ€ΠΆΠ°Ρ‚ΡŒ null-символ. ΠžΠ±Ρ€Π°Ρ‚ΠΈΡ‚Π΅ Π²Π½ΠΈΠΌΠ°Π½ΠΈΠ΅, сообщСниС message Ρ€Π°Π·Ρ€Π΅ΡˆΠ°Π΅Ρ‚ΡΡ ΠΎΡ‚ΠΏΡ€Π°Π²Π»ΡΡ‚ΡŒ Π² Ρ„Π°ΠΉΠ», ΠΏΠΎ ΠΏΠΎΡ‡Ρ‚Π΅, Π² syslog ΠΈ Ρ‚. Π΄. ΠŸΠ΅Ρ€Π΅Π΄ Π²Ρ‹Π·ΠΎΠ²ΠΎΠΌ Ρ„ΡƒΠ½ΠΊΡ†ΠΈΠΈ error_log() сообщСниС ΠΏΡ€Π΅ΠΎΠ±Ρ€Π°Π·ΠΎΠ²Ρ‹Π²Π°ΡŽΡ‚ ΠΈΠ»ΠΈ ΡΠΊΡ€Π°Π½ΠΈΡ€ΡƒΡŽΡ‚ функциями, ΠΊΠΎΡ‚ΠΎΡ€Ρ‹Π΅ ΡΠΎΠΎΡ‚Π²Π΅Ρ‚ΡΡ‚Π²ΡƒΡŽΡ‚ Ρ‚ΠΈΠΏΡƒ сообщСния: base64_encode(), rawurlencode() ΠΈΠ»ΠΈ addslashes().

οΌ‹Π”ΠΎΠ±Π°Π²ΠΈΡ‚ΡŒ

ΠŸΡ€ΠΈΠΌΠ΅Ρ‡Π°Π½ΠΈΡ ΠΏΠΎΠ»ΡŒΠ·ΠΎΠ²Π°Ρ‚Π΅Π»Π΅ΠΉ 18 notes

up
199
kevindougans at gmail dot com ΒΆ
16 years ago
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.
up
41
Sion ΒΆ
7 years ago
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 ) );
up
52
roychri at php dot net ΒΆ
16 years ago
There is a limit on the maximum length that you can pass as the $message.

The default seem to be 1024 but can be changed by adjusting the value of the runtime configuration value of 'log_errors_max_len'.

More details here:
http://www.php.net/manual/en/errorfunc.configuration.php
up
28
frank at booksku dot com ΒΆ
19 years ago
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!
up
20
i dot buttinoni at intandtel dot com ΒΆ
18 years ago
Be carefull. Unexpected PHP dies when 2GByte of file log reached (on systems having upper file size limit). 
A work aorund is rotate logs :)
up
11
SJL ΒΆ
18 years ago
"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.
up
5
russ at russtanner dot com ΒΆ
6 years ago
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":

In your code:

error_log("DevSys1 - FirstName: $FirstName - LastName: $Lastname");

On your command line:

tail -f /var/log/httpd/error_log | grep DevSys1

In this example, we pipe apache log output to grep (STDIN) which filters it for you only showing messages that contain "DevSys1".

The "-f" option means "follow" which streams all new log entries to your terminal or to any piped command that follows, in this case "grep".
up
14
php at kennel17 dot NOSPAM dot co dot uk ΒΆ
20 years ago
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.
up
7
Matthew Swift ΒΆ
6 years ago
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.
up
11
Anonymous ΒΆ
23 years ago
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
up
5
p dot lhonorey at nospam-laposte dot net ΒΆ
19 years ago
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 !
up
5
paul dot chubb at abs dot gov dot au ΒΆ
17 years ago
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); 
}
up
3
franz at fholzinger dot com ΒΆ
21 years ago
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
up
2
stepheneliotdewey at GmailDotCom ΒΆ
18 years ago
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.
up
0
deanna+php at doesnotcompute dot co dot uk ΒΆ
8 days ago
Some versions of PHP (7.2 in my case) with FPM/proxy_fcgi, will concatenate all messages onto a single line log entry which makes it difficult to read and identify those of note.

<?php
error_log("error 1");
error_log("error 2");
error_log("error 3");
?>

[Tue Jun 02 22:50:46.376449 2026] [proxy_fcgi:error] [pid 3095819:tid 140179394254592] [client 1.555.123.456:63222] AH01071: Got error 'PHP message: error 1\nPHP message: error 2\nPHP message: error 3\n'

This is mentioned in PHP issue 10890, but no real solution other than switching to FCGI.
https://github.com/php/php-src/issues/10890
up
0
eguvenc at gmail dot com ΒΆ
17 years ago
<?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
?>
up
-2
daniel dot fukuda at gmail dot com ΒΆ
16 years ago
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.
up
-5
Robert Chapin ΒΆ
7 years ago
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:

<?php ini_set('error_log', 'error_log'); ?>