首頁 php教程 PHP开发 Laravel日誌用法詳解

Laravel日誌用法詳解

Dec 27, 2016 am 10:09 AM

本文實例講述了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: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);
  }
}
登入後複製

如果自訂Monolog配置,走if條件,預設走else

protected function configureHandlers(Application $app, Writer $log)
{
    $method = &#39;configure&#39;.ucfirst($app[&#39;config&#39;][&#39;app.log&#39;]).&#39;Handler&#39;;
    $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().&#39;/logs/laravel.log&#39;, #存储文件
       $app->make(&#39;config&#39;)->get(&#39;app.log_level&#39;, &#39;debug&#39;) #存储级别
     );
}
登入後複製

這裡useFiles方法是註冊signle檔案日誌處理程序,並設定儲存檔案以及儲存的層級。

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

public function useFiles($path, $level = &#39;debug&#39;) #单一文件
public function useDailyFiles($path, $days = 0, $level = &#39;debug&#39;) #每日生成
public function useSyslog($name = &#39;laravel&#39;, $level = &#39;debug&#39;) #系统日志的方式
public function useErrorLog($level = &#39;debug&#39;, $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程式設計有所幫助。

更多Laravel日誌用法詳解相關文章請關注PHP中文網!

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)