4

This is what I tell php to do:

<?php
error_reporting(E_ALL);
ini_set('display_errors', '0');
ini_set('log_errors', 1);
ini_set('error_log', 'errors.log');
ini_set('error_append_string', 'APP');
ini_set('error_prepend_string', 'PRE');
?>

Hoping that I would get all errors reported in one file, with "PRE" before the message and "APP" after the message. (My goal is to make PRE and APP into /n /r or something...)

But my error reporting log looks like this:

[14-May-2013 00:16:26] PHP Notice: Undefined variable: nonexistentvariable in /home/www/dir/index.php on line 14[14-May-2013 00:16:28] PHP Notice: Undefined variable: nonexistentvariable in /home/www/dir/index.php on line 14

No PRE, no APP...

How do I get it in there, so that my error reporting file will have each error on a newline, rather than a loooong string of errors that's not helpful to me?

EDIT: So I noticed that with ini_set('display_errors', '1'); the "APP" and "PRE" strings are displayed on the page, in the error message.

That means I really need to find out where I can change the format for how php logs the errors. For example this whole timestamp thing - where is that specified?

EDIT: It is not possible to change the way php logs errors, you have to make your own error handler.

ANSWER: To truly achieve what I was going to, I asked a different question (to be more precise). How to change the way PHP writes errors in the error log file?

Community
  • 1
  • 1
oliver_siegel
  • 1,666
  • 3
  • 22
  • 37

2 Answers2

5

As the documentation says nothing to that, I have tested your example. I realized that error_append/prepend_string only works for displayed but not logged errors

My goal is to make PRE and APP into /n /r or something...

You should use a regex for that. Should be possible, as every log messages starts with nearly same prefix. Will prepare one....

hek2mgl
  • 152,036
  • 28
  • 249
  • 266
  • as for your realization: Yes, I had the same results. The log message is not effected by append_string. – oliver_siegel May 13 '13 at 22:48
  • Are you suggesting to rewrite the error log file and add newlines after each page load or something like that? – oliver_siegel May 13 '13 at 22:52
  • and do what with the expression? – oliver_siegel May 13 '13 at 23:14
  • 1
    I have currently something unfinished like this: `'/^\[.*?\] PHP (Notice|Parse error):/m'` .. Of course the list `Notice|Parse error` must be extended. But it looks like finding the right regex can be tricky here (but it is possible)... maybe google finds something as well – hek2mgl May 13 '13 at 23:16
  • I appreciate your effort, thank you! I am just wondering if once you have the regex, are you going to iterate over the log file with it, or what will you use it for? – oliver_siegel May 13 '13 at 23:18
  • I had `preg_split()` in mind. But *clever* iterating might be faster in this situation as the log file is expected large – hek2mgl May 13 '13 at 23:19
  • Can I ask why you need this? (maybe there are better solutions) – hek2mgl May 13 '13 at 23:20
  • Well I want any php errors to be logged in a file which I can access, but no users have access to (security and such). This way I can detect issues on the site... And the reason why I would like each error in a new line is, because otherwise its a long blurb that's hard to read. would be much easier to process if each error were on a separate line. – oliver_siegel May 13 '13 at 23:24
  • The better solution would be if it were possible to tell PHP how to log errors. maybe also add not only a timestamp, but also an IP address for example, or session data, or something... – oliver_siegel May 13 '13 at 23:25
  • I agree that using a preg_split may be challenging in case the log file is large... What is "clever" iterating? – oliver_siegel May 13 '13 at 23:27
  • 1
    I expect that there are tools for that (don't actually having one in mind). But check this for example, maybe it enlights you :) : http://serverfault.com/questions/53894/colorize-monitoring-of-logs – hek2mgl May 13 '13 at 23:40
  • I ended up putting a new question out for this, as my original question had somewhat changed. http://stackoverflow.com/questions/16547879/how-to-change-the-way-php-writes-errors-in-the-error-log-file Thanks for all your help! – oliver_siegel May 14 '13 at 18:10
-1

check phpinfo(); after ini_sets

Michal Hatak
  • 797
  • 1
  • 9
  • 21