因为laravel5.5日志文件固定在storage/logs/中,不能自定义日志路径,想要实现路径可配置需要自定义配置
增加日志处理类
<?php
/**
* 自定义日志,用来方便配置日志文件
*
*/
namespace App\Extensions;
use Illuminate\Log\Writer;
use Illuminate\Contracts\Foundation\Application;
class ConfigureLogging
{
/**
* 设置应用的Monolog处理程序
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
public function configureHandlers(Application $app, Writer $log)
{
$method = 'configure'.ucfirst($app['config']['app.log']).'Handler';
$this->{$method}($app, $log);
}
/**
* 设置应用single模式下的Monolog处理程序
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
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);
}
/**
* 设置应用daily模式下的Monolog处理程序
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
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)
);
}
/**
* 设置应用syslog模式下的Monolog处理程序
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureSyslogHandler(Application $app, Writer $log)
{
$log->useSyslog($app->make('config')->get('app.log_name'));
}
/**
* 设置应用errorlog模式下的Monolog处理程序
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureErrorlogHandler(Application $app, Writer $log)
{
$log->useErrorLog();
}
}
增加日志配置参数
在 config/app.php 中增加配置
# 日志路径
'log_path' => env('APP_LOG_PATH', storage_path('logs')),
# 日志文件名
'log_name' => env('APP_LOG_NAME', 'labs'),
增加环境变量
APP_LOG_PATH = log_data
APP_LOG_NAME = labs
应用自定义日志处理类
使用 configureMonologUsing 方法来配置应用程序对 Monolog 的完全控制。在 $app 变量返回之前,在 bootstrap/app.php 文件中调用此方法:
// 自定义 Monolog 配置
$app->configureMonologUsing(function($monolog) use ($app) {
$log = new Illuminate\Log\Writer( $monolog );
$configureLogging = new App\Extensions\ConfigureLogging();
$configureLogging->configureHandlers($app, $log);
});
测试日志是否有效
Log::info('log test');
网友评论