我只回答这个问题吧。我如何找到上述问题的解决方案。
我在 App\Logging 文件夹中创建了一个像这样的点击文件
namespace App\Logging;
use Monolog\Handler\RotatingFileHandler;
use Monolog\Handler\FormattableHandlerInterface;
use Monolog\Formatter\LineFormatter;
class CustomLogFilenames
{
/**
* Customize the given logger instance.
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger)
{
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof RotatingFileHandler) {
$userId = auth()->user()->id;
$handler->setFilenameFormat("{filename}-$userId-{date}", 'Y-m-d');
}
if ($handler instanceof FormattableHandlerInterface) {
$handler->setFormatter( new LineFormatter("[%datetime%]: %message% \n", 'Y-m-d H:i:s', true));
}
}
}
}
并在 config\logging 中添加以下配置
'userTracker' => [
'driver' => 'daily',
'tap' => [App\Logging\CustomLogFilenames::class],
'path' => storage_path('logs/user-id.log'),
'level' => 'debug',
],
我在控制器中添加了这个
\Log::channel('userTracker')->info('Edit the Page');
它创建了一个名为 user-id-100-2020-02-26.log 的文件