我使用了独白,不知道如何设置最大文件日志大小。
我曾尝试使用MemoryPeakUsageProcessor,但我认为我的代码是错误的:
$formatter = new LineFormatter(
null, // Format of message in log, default [%datetime%] %channel%.%level_name%: %message% %context% %extra%\n
null, // Datetime format
true, // allowInlineLineBreaks option, default false
true // ignoreEmptyContextAndExtra option, default false
);
$logger = new Logger($logName);
$logSize= new Monolog\Processor\MemoryPeakUsageProcessor(20);
$logger->pushProcessor($logSize);
$stream = new RotatingFileHandler(__DIR__.'./../../app.log',1);
$stream->setFormatter($formatter);
$logger->pushHandler($stream);
但未设置最大大小。超出容量时,不会删除或清空我的日志文件
答案 0 :(得分:0)
由于大小限制在所有Monolog处理程序中均不存在,因此我必须编写它。请在下面找到我为Slim 3框架编写的解决方案。我相信您可以根据自己的情况进行调整
Info.plist
下面是数组中Monolog设置的示例
// SLIM 3 framework, from dependencies.php
...
// monolog setup with rotating files according to a size
$container['logger'] = function ($c) {
// get the settings (array, see below)
$settings = $c->get('settings')['logger'];
// rotate log file on size
$logname = $settings['path'];
if (file_exists($logname) && filesize($logname) > $settings['maxSize']) {
$path_parts = pathinfo($logname);
$pattern = $path_parts['dirname']. '/'. $path_parts['filename']. "-%d.". $path_parts['extension'];
// delete last log
$fn = sprintf($pattern, $settings['maxFiles']);
if (file_exists($fn))
unlink($fn);
// shift file names (add '-%index' before the extension)
for ($i = $settings['maxFiles']-1; $i > 0; $i--) {
$fn = sprintf($pattern, $i);
if(file_exists($fn))
rename($fn, sprintf($pattern, $i+1));
}
rename($logname, sprintf($pattern, 1));
}
$stream = new Monolog\Handler\StreamHandler($logname, $settings['level']);
// declare finally the logger
$logger = new Monolog\Logger($logname);
$logger->pushHandler($stream);
return $logger; // ready to log ;)
};
...