Laravel 自定义日志文件名称和位置
因为laravel5.5日志文件固定在storage/logs/中,不能自定义日志路径,想要实现路径可配置需要自定义配置
<?php
namespace App\Extensions;
use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
class ConfigureLogging
{
public function configureHandlers(Application $app, Writer $log)
{
$method = 'configure' . ucfirst($app['config']['app.log']) . 'Handler';
$this->{$method}($app, $log);
}
protected function configureSingleHandler(Application $app, Writer $log)
{
$config = $app->make('config');
$filename = $config->get('app.log_path') . '/' . $config->get('app.log_name') . '.log';
$log->useFiles($filename);
}
protected function configureDailyHandler(Application $app, Writer $log)
{
$config = $app->make('config');
$filename = $config->get('app.log_path') . '/' . $config->get('app.log_name') . '.log';
$log->useDailyFiles(
$filename,
$app->make('config')->get('app.log_max_files', 5)
);
}
protected function configureSyslogHandler(Application $app, Writer $log)
{
$log->useSyslog($app->make('config')->get('app.log_name'));
}
protected function configureErrorlogHandler(Application $app, Writer $log)
{
$log->useErrorLog();
}
}
$app->configureMonologUsing(function($monolog) use ($app) {
$log = new Illuminate\Log\Writer( $monolog );
$configureLogging = new App\Extensions\ConfigureLogging();
$configureLogging->configureHandlers($app, $log);
});
'log_path' => '/data/laravel/storage/logs/' . env('APP_NAME', 'laravel'),
'log_name' => env('APP_LOG_NAME', 'laravel'),
|