Seldaek / monolog

Sends your logs to files, sockets, inboxes, databases and various web services
https://seldaek.github.io/monolog/
MIT License
21.02k stars 1.9k forks source link

glob() not finding the existing log -> Log rotation doesn't delete files #1784

Open Defcon0 opened 1 year ago

Defcon0 commented 1 year ago

Monolog version 1|2|3? it's version 2

Related to https://github.com/Seldaek/monolog/issues/204#issuecomment-19757534

I use the drupal monolog module which is using this monolog library.

In the following line I get an empty array:

https://github.com/Seldaek/monolog/blob/2.8.0/src/Monolog/Handler/RotatingFileHandler.php#L142

for the path "private://logs/debug.log" because glob can't compute the right paths. The files definitely exist. If I replace the path with an absolute one, it works, but then I'd have to adjust the paths for each and every environment which is tedious :-/

Maybe we can think about using something else than glob() here? Or is there something I can do?

See here as well: https://www.drupal.org/project/monolog/issues/3326496

Thanks in advance!

Seldaek commented 1 year ago

OK I see that glob has no support for stream wrappers, that sucks. We'd need to convert glob + getGlobPattern into something like:

$baseDir = $this->computeBaseDir($this->filename);
$directory = new RecursiveDirectoryIterator($baseDir);
$iterator = new RecursiveIteratorIterator($directory);
$regex = new RegexIterator($iterator, $globPattern);

computeBaseDir has to be done with caution tho, if you use something like setFilenameFormat('{date}.log', 'Y/m/d') then you end up with $this->filename being /path/to/log/2023/01/05.log so it shouldn't take the full dirname as base dir, but needs to be smarter about identifying what path is the real base path.

Also would need to make sure there is no open_basedir regression regarding https://github.com/Seldaek/monolog/issues/204 as this failed with the GlobIterator previously. I'm not sure if other directory iterators have similar issues.

All in all sounds like quite a bit of work for a fairly fringe use case so I'm unlikely to spend time on this tbh. Just wrote down as much info as I could if anyone is interested in tackling this.

Otherwise I would recommend using logrotate on prod systems it's usually a better option than RotatingFileHandler IMO.