這篇文章主要介紹了Laravel日誌用法,結合實例形式較為詳細的分析了Laravel日誌的功能、定義、使用方法與相關注意事項,需要的朋友可以參考下
本文實例講述了Laravel日誌用法。分享給大家供大家參考,具體如下:
這裡使用的Laravel版本仍是5.2
日誌是非常重要的。本機開發可以開啟調試模式,但是上線的項目查看日誌是非常簡潔有效的調試手段。 Laravel整合了Monolog日誌庫以便提供多種強大的日誌處理器。
Laravel支援日誌方法single, daily, syslog 和 errorlog。例如,如果你想要日誌檔案按日產生而不是產生單一文件,你應該在設定檔config/app.php中設定log值如下:
##
'log' => 'daily'
#config/app.php:111 'log' => env('APP_LOG', 'single'),
#vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:36 protected $bootstrappers = [ 'Illuminate\Foundation\Bootstrap\DetectEnvironment', 'Illuminate\Foundation\Bootstrap\LoadConfiguration', 'Illuminate\Foundation\Bootstrap\ConfigureLogging', 'Illuminate\Foundation\Bootstrap\HandleExceptions', 'Illuminate\Foundation\Bootstrap\RegisterFacades', 'Illuminate\Foundation\Bootstrap\RegisterProviders', 'Illuminate\Foundation\Bootstrap\BootProviders', ]; <?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) { $log = $this->registerLogger($app); // If a custom Monolog configurator has been registered for the application // we will call that, passing Monolog along. Otherwise, we will grab the // the configurations for the log system and use it for configuration. if ($app->hasMonologConfigurator()) { call_user_func( $app->getMonologConfigurator(), $log->getMonolog() ); } else { $this->configureHandlers($app, $log); } }
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', #存储文件 $app->make('config')->get('app.log_level', 'debug') #存储级别 ); }
public function useFiles($path, $level = 'debug') #单一文件 public function useDailyFiles($path, $days = 0, $level = 'debug') #每日生成 public function useSyslog($name = 'laravel', $level = 'debug') #系统日志的方式 public function useErrorLog($level = 'debug', $messageType = ErrorLogHandler::OPERATING_SYSTEM) #等同于php的error_log方式
八種日誌等級:emergency, alert, critical, error,warning, notice, info 和 debug。
Log::emergency($error); Log::alert($error); Log::critical($error); Log::error($error); Log::warning($error); Log::notice($error); Log::info($error); Log::debug($error);
#
以上是關於Laravel的日誌用法的詳細內容。更多資訊請關注PHP中文網其他相關文章!