15

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?

2
  • 2
    Can you provide an example? Are you sure it's not being limited by error_log()? Commented Sep 2, 2014 at 10:33
  • Probably a limitation of logger. Why not print the array at the interesting stuff? Commented Sep 2, 2014 at 10:35

5 Answers 5

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');
Sign up to request clarification or add additional context in comments.

2 Comments

Seems reasonable! I posted another question about log_errors_max_len not working here: stackoverflow.com/questions/25622760/… Maybe you can help me twice today :)
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.
21

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.

1 Comment

This works perfectly, it MUST be the correct answer
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>';

2 Comments

Still truncated
The max length limit can be easily increased, which you almost linked. Having errors included in the HTML output is not good practice. How will you ever debug a layout issue with error output cluttering the content? You never want this in a production environment. I think it's a oversight that PHP has not deprecated the display-errors .ini setting, and probably a mistake to even have it in the first place.
2

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)));

Comments

1

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.

1 Comment

This is utter madness. But I like it. Actually could be useful. Each error log line contains a date-time stamp at the start. Many times that has got in the way of diffing logs. Perhaps one for every line could help.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.