Laravel日志用法详解

2025-05-29 0 56

本文实例讲述了Laravel日志用法。分享给大家供大家参考,具体如下:

这里使用的Laravel版本仍是5.2

日志是非常重要的。本地开发可以开启调试模式,但是上线的项目查看日志是非常简洁有效的调试手段。Laravel集成了Monolog日志库以便提供多种功能强大的日志处理器。

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

?

1
'log' => 'daily'

系统默认配置为single

?

1

2
#config/app.php:111

'log' => env('APP_LOG', 'single'),

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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37
#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

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19
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种日志处理注册方式。

?

1

2

3

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。

?

1

2

3

4

5

6

7

8
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程序设计有所帮助。

收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信/支付宝扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。

快网idc优惠网 建站教程 Laravel日志用法详解 https://www.kuaiidc.com/97056.html

相关文章

发表评论
暂无评论