Laravel日志用法詳解
本文實(shí)例講述了Laravel日志用法。分享給大家供大家參考,具體如下:
這里使用的Laravel版本仍是5.2
日志是非常重要的。本地開發(fā)可以開啟調(diào)試模式,但是上線的項(xiàng)目查看日志是非常簡潔有效的調(diào)試手段。Laravel集成了Monolog日志庫以便提供多種功能強(qiáng)大的日志處理器。
Laravel支持日志方法single, daily, syslog 和 errorlog。例如,如果你想要日志文件按日生成而不是生成單個(gè)文件,應(yīng)該在配置文件config/app.php中設(shè)置log值如下:
'log' => 'daily'
系統(tǒng)默認(rèn)配置為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條件,默認(rèn)走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文件日志處理程序,并設(shè)置存儲文件以及存儲的級別。
下面是初始化日志時(shí)的4種日志處理注冊方式。
public function useFiles($path, $level = 'debug') #單一文件 public function useDailyFiles($path, $days = 0, $level = 'debug') #每日生成 public function useSyslog($name = 'laravel', $level = 'debug') #系統(tǒng)日志的方式 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);
更多關(guān)于Laravel相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《Laravel框架入門與進(jìn)階教程》、《php優(yōu)秀開發(fā)框架總結(jié)》、《smarty模板入門基礎(chǔ)教程》、《php日期與時(shí)間用法總結(jié)》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫操作入門教程》及《php常見數(shù)據(jù)庫操作技巧匯總》
希望本文所述對大家基于Laravel框架的PHP程序設(shè)計(jì)有所幫助。
相關(guān)文章
PHP常量DIRECTORY_SEPARATOR原理及用法解析
這篇文章主要介紹了PHP常量DIRECTORY_SEPARATOR原理及用法解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-11-11
windows的文件系統(tǒng)機(jī)制引發(fā)的PHP路徑爆破問題分析
這篇文章主要介紹了windows的文件系統(tǒng)機(jī)制引發(fā)的PHP路徑爆破問題分析,需要的朋友可以參考下2014-07-07
Thinkphp 框架擴(kuò)展之標(biāo)簽庫驅(qū)動原理與用法分析
這篇文章主要介紹了Thinkphp 框架擴(kuò)展之標(biāo)簽庫驅(qū)動,結(jié)合實(shí)例形式分析了Thinkphp標(biāo)簽庫驅(qū)動擴(kuò)展相關(guān)概念、原理、用法及操作注意事項(xiàng),需要的朋友可以參考下2020-04-04
ThinkPHP入口文件設(shè)置及相關(guān)注意事項(xiàng)分析
這篇文章主要介紹了ThinkPHP入口文件設(shè)置及相關(guān)注意事項(xiàng),以注釋的形式詳細(xì)分析了入口文件設(shè)置時(shí)相關(guān)設(shè)置項(xiàng)的含義與設(shè)置技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下2014-12-12
Laravel5.1 框架模型一對一關(guān)系實(shí)現(xiàn)與使用方法實(shí)例分析
這篇文章主要介紹了Laravel5.1 框架模型一對一關(guān)系實(shí)現(xiàn)與使用方法,結(jié)合實(shí)例形式分析了laravel5.1框架模型一對一關(guān)系的原理、定義與使用方法,需要的朋友可以參考下2020-01-01
phpstudy本地環(huán)境搭建超詳細(xì)圖文教程
這篇文章主要給大家介紹了關(guān)于phpstudy本地環(huán)境搭建超詳細(xì)圖文教程的相關(guān)資料,phpStudy是集安全、高效、功能于一體且完全免費(fèi)的一鍵服務(wù)器環(huán)境搭建軟件,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-07-07

