14

I'm not experienced in PHP and I'm, using:

error_log("big array:" . print_r($bigArray, true));

to look at what's inside a big array but it looks like the output is cut off before I get to the interesting stuff in the output like so:

...
           [4] => Array
            (
                [id] => 100039235
                [start] => 11:00
                [end] => 19:00
                [punches] => Array
                    (
                        [0] => Array
                            (
                                [id] => 6319
                                [comment] => 

Is this expected? Are there other ways or workarounds to get more of the array logged out?

h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
Cotten
  • 8,787
  • 17
  • 61
  • 98

5 Answers5

23

If you check the error INI options in PHP you'll notice there's a log_errors_max_len option:

Set the maximum length of log_errors in bytes. In error_log information about the source is added. The default is 1024 and 0 allows to not apply any maximum length at all. This length is applied to logged errors, displayed errors and also to $php_errormsg.

When an integer is used, the value is measured in bytes. Shorthand notation, as described in this FAQ, may also be used.

Hence, if you want to use error_log to output these huge messages, make sure you change log_errors_max_len to a large number (or 0 for unlimited length).

// Append to the start of your script
ini_set('log_errors_max_len', '0');
MontrealDevOne
  • 1,034
  • 3
  • 17
  • 30
h2ooooooo
  • 39,111
  • 8
  • 68
  • 102
  • Seems reasonable! I posted another question about log_errors_max_len not working here: http://stackoverflow.com/questions/25622760/changing-log-errors-max-len-has-no-effect Maybe you can help me twice today :) – Cotten Sep 02 '14 at 11:53
  • 5
    I suppose this answer was accurate at one time. But now `log_errors_max_len` does not change the limit for entries created via the `error_log()` function, as stated in the documentation, and pointed out in [Mike's answer](https://stackoverflow.com/a/52100034/3303195). – faintsignal Aug 09 '19 at 00:07
18

This answer is a bit out of date.

The output from error_log will still get truncated, even if log_errors_max_len is set to 0 (for unlimited output).

As per the PHP documentation for log_errors_max_len:

This length is applied to logged errors, displayed errors and also to $php_errormsg, but not to explicitly called functions such as error_log().

The only way to get your error_log() statements to truly output large amounts of information is to specify a custom log file.

error_log("big array:" . print_r($bigArray, true), 3, '/path/to/custom.log');

The second parameter (3) indicates the message type (external log) as per the error_log documentation.

Mike Cavaliere
  • 670
  • 6
  • 10
2

As scrowler mentions its error_log that's limiting the output.

The error_log will log to syslog by default and in your code, the length of which is limited by the runtime setting log_errors_max_len and which is 1024 by default.

See the following for further details on these functions and settings -

http://php.net/manual/en/function.error-log.php http://php.net/manual/en/errorfunc.configuration.php#ini.log-errors-max-len

What you probably want to do is just call print_r ($bigArray) to have it output directly, or if you want to see something a bit fancier in a browser use

echo '<pre>' . print_r ($bigArray, TRUE) . '</pre>';
Andy Burton
  • 566
  • 6
  • 12
1

If a string in the array contains a null character (byte with value zero), the output will also be truncated at that point. From https://secure.php.net/manual/en/function.error-log.php#refsect1-function.error-log-notes:

Warning error_log() is not binary safe. message will be truncated by null character.

To work around this you could use str_replace to eliminate null characters:

error_log("big array:" . str_replace("\0", '\\0', print_r($bigArray, true)));
Jake
  • 948
  • 8
  • 19
0

You can beat the system by calling error_log multiple times, since the character limit only seems to be applied to a single call:

function multiline_error_log($val) {
  $lines = preg_split("/[\r]?[\n]/", print_r($val, 1));
  foreach ($lines as $line) error_log($line);
}

This will work as long as no single textual line of the output of print_r exceeds the character limit. Note it may be worth incorporating Jake's answer into how you define multiline_error_log if you are worried about null character issues.

Gershom Maes
  • 7,358
  • 2
  • 35
  • 55