国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Laravel日志用法詳解

2024-05-04 23:50:45
字體:
來源:轉載
供稿:網友

本文實例講述了Laravel日志用法。分享給大家供大家參考,具體如下:

這里使用的Laravel版本仍是5.2

日志是非常重要的。本地開發可以開啟調試模式,但是上線的項目查看日志是非常簡潔有效的調試手段。Laravel集成了Monolog日志庫以便提供多種功能強大的日志處理器。

Laravel支持日志方法single, daily, syslog 和 errorlog。例如,如果你想要日志文件按日生成而不是生成單個文件,應該在配置文件config/app.php中設置log值如下:

'log' => 'daily'

系統默認配置為single

#config/app.php:111'log' => env('APP_LOG', 'single'),

下面我們看下Laravel是如何配置日志的。

#vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php:36protected $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',];<?phpnamespace 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);  }}

如果自定義Monolog配置,走if條件,默認走else

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') #存儲級別     );}

這里useFiles方法是注冊signle文件日志處理程序,并設置存儲文件以及存儲的級別。

下面是初始化日志時的4種日志處理注冊方式。

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方式

日志初始化信息基本上就是上面這些。

你可以使用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程序設計有所幫助。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 天门市| 静安区| 五家渠市| 元江| 蒲城县| 衡水市| 南阳市| 汤原县| 高邮市| 合作市| 林芝县| 法库县| 铜陵市| 东丰县| 安吉县| 阳高县| 三江| 韶山市| 新密市| 太原市| 漳浦县| 凯里市| 大关县| 嫩江县| 徐闻县| 孝感市| 乃东县| 山丹县| 乌拉特中旗| 五华县| 渝北区| 施秉县| 阿合奇县| 集贤县| 乐平市| 呼玛县| 汝南县| 建湖县| 临江市| 新宁县| 东明县|