Does not write to the log

When I use ActiverRecord to deliberately query a large number of records, fill up memory and open debug, it will show “Allowed memory size of 134217728 bytes exhausted” (tried to allocate 4096 bytes). However, all log information is not written to app. log.

When I read large files directly with file_get_contents(), it will show: Out of memory (allocated 2097152) (tried to allocate 119895576 bytes), you can find this error in app. log.

Yii2 & PHP7.1

Hello @lanrenbulan and welcome to the forum.

You hit the eternal problem of memory resources programmers deal with all the time. There are some ways of conserving the memory though. Let me point you to some topics that might help:

@lanrenbulan
I have tried to reproduce the symptom that you reported.

In debug mode (when YII_DEBUG is defined), both of the errors are displayed on the screen with detailed information, but none of them are written to the log file.

And in production mode (when YII_DEBUG is not defined), both of them are written to the log file with detailed information, and both of them display minimal error message for the end users on the screen.

Would you please double check it?

Order::find()->asArray()->all();// I’m doing Data Export (CSV), about 130,000 orders.

When I tried to read all the data to provide out of memory (PHP Fatal Error - yii base ErrorException Allowed memory size of 1342128 bytes exhausted (tried to allocate 4096 bytes).

Of course, I deliberately exported so much data to see if I would write to the error log.

I want to know why I didn’t log when I ran out of memory. The methods you mentioned are already in use.

I think it’s because you are running your app in debug mode, as I told you.

This may help https://www.yiiframework.com/doc/guide/2.0/en/runtime-logging#flushing-exporting

1 Like

It really works in a product environment, so I have doubts.

I deliberately wrote the code incorrectly. There was log information recorded.

defined(‘YII_DEBUG’) or define(‘YII_DEBUG’, false);
defined(‘YII_ENV’) or define(‘YII_ENV’, ‘prod’);

I think register_shutdown_function is not executed.

When I make the following configuration, log information is recorded, and it’s not written every time(Sometimes, sometimes not.).
‘flushInterval’ => 1,
‘exportInterval’ => 0,