当前位置:首页 > PHP教程 > PHP常见问题

Laravel日志用法详解

本文实例讲述了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 = [ 'illuminatefoundationbootstrapdetectenvironment', 'illuminatefoundationbootstraploadconfiguration', 'illuminatefoundationbootstrapconfigurelogging', 'illuminatefoundationbootstraphandleexceptions', 'illuminatefoundationbootstrapregisterfacades', 'illuminatefoundationbootstrapregisterproviders', 'illuminatefoundationbootstrapbootproviders', ]; <?php namespace illuminatefoundationbootstrap; use illuminatelogwriter; use monologlogger as monolog; use illuminatecontractsfoundationapplication; class configurelogging { /** * bootstrap the given application. * * @param illuminatecontractsfoundationapplication $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 illuminatecontractsfoundationapplication $app * @param illuminatelogwriter $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相关内容感兴趣的读者可查看本站专题:《laravel框架入门与进阶教程》、《php优秀开发框架总结》、《smarty模板入门基础教程》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家基于laravel框架的php程序设计有所帮助。



【说明】本文章由站长整理发布,文章内容不代表本站观点,如文中有侵权行为,请与本站客服联系(QQ:)!