问题
i was searching solution for custom Log file name. i got a solution that was working in 5.6 version project. but after that means more than 5.6 it is not working. i try to used many solution but not working. every time file name generating laravel.log. but i want to get it like laravel-{vendor_code}-{date}.log
i used solution that code is below.
CustomLogFile.php
<?php
namespace App\Logging;
use Illuminate\Http\Request;
use Monolog\Handler\RotatingFileHandler;
class CustomLogFile
{
/**
* Customize the given logger instance.
*
* @param \Illuminate\Log\Logger $logger
* @return void
*/
public function __invoke($logger )
{
$code = 'NA';
$headers = apache_request_headers();
if( isset( $headers['DBAuth'] ))
{
$code = dnc($headers['DBAuth']) ;
}
elseif ( isset($_REQUEST['code']) )
{
$code = $_REQUEST['code'];
}
foreach ($logger->getHandlers() as $handler) {
if ($handler instanceof RotatingFileHandler) {
$sapi = php_sapi_name();
$handler->setFilenameFormat("{filename}-$code-{date}", 'Y-m-d');
}
}
}
}
Config/ logging.php
'default' => env('LOG_CHANNEL', 'stack'),
......
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['daily'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'tap' => [App\Logging\CustomLogFile::class],
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
here this code is working as my condition in 5.6 v of Laravel but not more than 5.6 v.
i try to inspect the $logger->getHandlers() . then i am getting stream array is null. so can it be reason of this.
回答1:
may be its answer will be like this in env file
LOG_CHANNEL=daily
LOG_LEVEL=debug
Also Change day setting in Config/logging.php
'daily' => [
'driver' => 'daily',
'tap' => [App\Logging\CustomLogFile::class],
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 1, // 14----> 1
because in 6.2 version i was getting that result. so answer here i will confirm soon with 8.* version.
来源:https://stackoverflow.com/questions/64478010/how-to-customize-file-name-of-log-in-laravel-8-0