Illuminate/Foundation/Bootstrap/ConfigureLogging.php
<?php namespace Illuminate\Foundation\Bootstrap;
use Illuminate\Log\Writer;
use Monolog\Logger as Monolog;
use Illuminate\Contracts\Foundation\Application;
class ConfigureLogging {
/**
* Bootstrap the given application.
* 引导给定的应用程序。
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return void
*/
public function bootstrap(Application $app)
{
// 读取 app.log 配置,调用对应的的日志处理方法
$this->configureHandlers($app, $this->registerLogger($app));
// Next, we will bind a Closure that resolves the PSR logger implementation
// as this will grant us the ability to be interoperable with many other
// libraries which are able to utilize the PSR standardized interface.
// 绑定日志处理实例为 Monolog。
$app->bind('Psr\Log\LoggerInterface', function($app)
{
return $app['log']->getMonolog();
});
}
/**
* Register the logger instance in the container.
* 在容器中注册日志处理实例。
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @return \Illuminate\Log\Writer
*/
protected function registerLogger(Application $app)
{
$app->instance('log', $log = new Writer(
new Monolog($app->environment()), $app['events'])
);
return $log;
}
/**
* Configure the Monolog handlers for the application.
* 读取 app.log 配置,调用对应的的日志处理方法。
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureHandlers(Application $app, Writer $log)
{
$method = "configure".ucfirst($app['config']['app.log'])."Handler";
$this->{$method}($app, $log);
}
/**
* Configure the Monolog handlers for the application.
* 单文件日志处理。
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureSingleHandler(Application $app, Writer $log)
{
$log->useFiles($app->storagePath().'/logs/laravel.log');
}
/**
* Configure the Monolog handlers for the application.
* 每日记录,允许通过 app.log_max_files 设置“最大日志保存天数”(默认 5 天)。
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureDailyHandler(Application $app, Writer $log)
{
$log->useDailyFiles(
$app->storagePath().'/logs/laravel.log',
$app->make('config')->get('app.log_max_files', 5)
);
}
/**
* Configure the Monolog handlers for the application.
* 并入系统日志,并将标识符设置为 laravel。
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureSyslogHandler(Application $app, Writer $log)
{
$log->useSyslog('laravel');
}
/**
* Configure the Monolog handlers for the application.
* 并入系统错误日志。
*
* @param \Illuminate\Contracts\Foundation\Application $app
* @param \Illuminate\Log\Writer $log
* @return void
*/
protected function configureErrorlogHandler(Application $app, Writer $log)
{
$log->useErrorLog();
}
}