Laravel框架基于monolog库构建日志系统。
每个日志系统都定义了一套相似但又有略微差异的日志级别,monolog中,日志级别有如下几种,按照级别由高到低排序:
Laravel中,和日志相关的配置写在config/logging.php
中。
<?php
use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;
return [
/*
|--------------------------------------------------------------------------
| Default Log Channel
|--------------------------------------------------------------------------
|
| This option defines the default log channel that gets used when writing
| messages to the logs. The name specified in this option should match
| one of the channels defined in the "channels" configuration array.
|
*/
'default' => env('LOG_CHANNEL', 'stack'),
/*
|--------------------------------------------------------------------------
| Log Channels
|--------------------------------------------------------------------------
|
| Here you may configure the log channels for your application. Out of
| the box, Laravel uses the Monolog PHP logging library. This gives
| you a variety of powerful log handlers / formatters to utilize.
|
| Available Drivers: "single", "daily", "slack", "syslog",
| "errorlog", "monolog",
| "custom", "stack"
|
*/
'channels' => [
'stack' => [
'driver' => 'stack',
'channels' => ['single'],
'ignore_exceptions' => false,
],
'single' => [
'driver' => 'single',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
],
'daily' => [
'driver' => 'daily',
'path' => storage_path('logs/laravel.log'),
'level' => 'debug',
'days' => 14,
],
'slack' => [
'driver' => 'slack',
'url' => env('LOG_SLACK_WEBHOOK_URL'),
'username' => 'Laravel Log',
'emoji' => ':boom:',
'level' => 'critical',
],
'papertrail' => [
'driver' => 'monolog',
'level' => 'debug',
'handler' => SyslogUdpHandler::class,
'handler_with' => [
'host' => env('PAPERTRAIL_URL'),
'port' => env('PAPERTRAIL_PORT'),
],
],
'stderr' => [
'driver' => 'monolog',
'handler' => StreamHandler::class,
'formatter' => env('LOG_STDERR_FORMATTER'),
'with' => [
'stream' => 'php://stderr',
],
],
'syslog' => [
'driver' => 'syslog',
'level' => 'debug',
],
'errorlog' => [
'driver' => 'errorlog',
'level' => 'debug',
],
'null' => [
'driver' => 'monolog',
'handler' => NullHandler::class,
],
'emergency' => [
'path' => storage_path('logs/laravel.log'),
],
],
];
我们可以发现,其中定义了很多channel
,每一个通道其实是一种日志处理方式,比如single
代表单文件日志,daily
代表按天划分的日志文件等。例如single
的配置,其中path
指定日志文件的路径(一般都是指定在storage/logs
下),level
指定日志级别。
通道类型 | 说明 |
---|---|
stack | 整合日志通道 |
single | 单文件日志 |
daily | 按天划分的日志 |
slack | 发送日志到slack(一款国外类似企业微信的应用) |
papertrail | 发送日志到papertrail服务 |
stderr | 发送日志到系统标准错误输出 |
syslog | 发送日志到系统syslog |
errorlog | 发送日志到应用服务器日志 |
null | 不输出日志 |
这里我们注意,stack
是一个整合型的通道,它用于集成一个或多个其它的日志通道。平时开发的小网站中,我们基本都是使用single
或是daily
这两种日志形式。较大型的系统中,则一般有专门处理日志的系统,需要通过消息队列来写异步入日志,这里就不多介绍了。
我们可以使用如下方法写入各个级别的日志信息:
Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);
下面是一些例子。
Log::info('系统出错!', ['username' => 'tom']);
输出的日志内容例子如下。
[2020-07-23 07:13:53] local.INFO: 系统出错! {"username":"tom"}
第一个参数为日志内容,第二个参数是可选的,为携带的数据,可以传入一个关联数组。