-
-
Notifications
You must be signed in to change notification settings - Fork 6.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
problem with the size of the log[yii2/log/FileTarget] #19259
Comments
Hmm, does it work if you set |
thanks! Under Windows, same result, and when rename, there is an error. [yii\log\Dispatcher::dispatch] Unable to send log via yii\log\FileTarget: Exception (Invalid Configuration) 'yii\base\InvalidConfigException' with message 'Unable to append to log file: F:/codes/szy/ ycs/runtime/logs/open.log' |
What about file permissions? Are those log files writable by PHP? |
Permissions are Windows default permissions. These logs are written by php |
Then no idea. The owners of the repo need to check everything. |
ok,thanks! |
just try lock after clear cache and get file size if ($this->enableRotation) {
// clear stat cache to ensure getting the real current file size and not a cached one
// this may result in rotating twice when cached file size is used on subsequent calls
clearstatcache(false, $this->logFile);
$fileSize = filesize($this->logFile) ?: 0;
}
$fp = fopen($this->logFile, 'a');
if ($fp === false) {
throw new InvalidConfigException("Unable to append to log file: {$this->logFile}");
}
flock($fp, LOCK_EX);
if ($this->enableRotation && $fileSize > ($this->maxFileSize * 1024)) { and clear file by 'w' mode private function clearLogFile($rotateFile)
{
$fp = fopen($rotateFile, 'w');
if ($fp !== false) {
fclose($fp);
}
} |
Thank you, after testing, there are still some problems. I tested a rotateByCopy=true before, and the data in the source code was changed to:
It was changed to prevent windows from reporting errors.
It seems possible. |
@stu2162583 you must unlock file before do something with him |
What is this for? Are you afraid of affecting the file handle? |
@stu2162583 could you point the change in 2.0.16 that is the cause of that? |
2.0.16 https://github.com/yiisoft/yii2/blob/2.0.16/framework/log/FileTarget.php |
So the only thing is that previously file was rotated and then lock was released, after the change it's the other way around. And the change is here made because of that comment by @nadirvishun |
@stu2162583 need unlock file before do something with him. |
Yes, he mentioned that the file must be renamed and then closed, but only if it is in a windows environment and rotateByCopy=false. so, what to do next |
Sorry, I don't know much about lock, what is the reason for doing this |
Looks like we could adjust the process based on the selected options if this is only a matter of type of rotating and the OS. |
It looks like this, after your testing, make sure. Waiting for good news. We are now self-adjusting to no longer limit the number of logs, and we have changed the lock position, and now the log appears to be full. |
@stu2162583 See this note in the PHP manual on flock():
For the difference check e.g. this:
That's why the behavior is different on Linux/Windows. IMO we should check the OS and use the apropriate logic accordingly. |
thanks, i'll take a look |
@stu2162583 Is this a site with heavy load? If that's the case then it's probably another (parallel) process that locks the file immediately after your current process released the lock. That's why the file can neither be moved nor truncated. |
Ok I would say that we should never release any lock before rotation. This will always cause concurreny issues. A fix should probably:
|
After more analysis I would go so far and say that there's no easy fix. What we basically want is a mutex here. We try to re-implement it with file based locks. But that's a problem if the file that is used for locking is moved, which happens by default during rotation. In any case we have to keep the mutex-lock until all file processing is done. I see only 2 reliable fixes:
Using the |
@mikehaertl solution from PHP manual:
or by using
|
That was my second suggestion above.
This has nothing to do with it (also see here). We already block execution until the lock file is released by the other process. It's the logic when/what to lock/unlock that is flawed. |
why? we try re-lock file if |
See the linked SO answer that I linked in my last comment above. And we already use the default of blocking mode. That's fine because we want a 2nd process to wait a (hopefully) short amount of time until the 1st process will release the lock. |
Yes, there is still some load. |
In addition, this problem can also be reproduced in the linux production environment, but I am using the windows development environment, and I do not have a linux test environment locally. Maybe the problem on linux can be solved by modifying the lock position. |
Both suggested fixes come with a cost (BC break). But I would vote for option 2:
Option 1 of using a separate lock file (e.g. @samdark @bizley What do you think? It's a bit surprising that this bug exists for many years now. It's also a somewhat sensitive part of the framework so I'm a bit hesitant with any changes there. |
I agree with option 2 as well. @samdark ? |
@samdark since this would introduce BC-break we need your approval. |
Dropping |
…ateByCopy mandatory
What steps will reproduce the problem?
The log is full and concurrent
What is the expected result?
The log should be full
What do you get instead?
some logs are too small
Additional info
same as #19079
hello, I'm back again, after our testing, The problem is due to rotateFiles and concurrency
config
code
test with ab
You will see that some logs are very small
By the way, I don't think it's a good idea to attach an application log when logvar is not empty, preferably something that can be controlled.
The text was updated successfully, but these errors were encountered: