Laravel日志允许记录日志信息到文件,到系统错误日志。
Laravel日志基于channel,每个channel代表一个具体的记录日志消息的方式,简单来说,single会把日志记录到单独的文件里,slack通道会发送日志信息到slack,基于重要程度,日志可能会写入到多个通道中。
Monolog库,为各种强大的日志处理提供支持。允许以混合和匹配的方式,自定义程序日志管理。
stack 该通道有助于创建多通道的包装器
single 单个基于文件或路径的记录器通道(StreamHandler)
daily 一个基于 RotatingFileHandler 的每日循环的Monolog驱动程序
monolog 可以使用任何支持的Monolog处理程序的Monolog驱动程序
syslog 基于SyslogHandler的Monolog驱动程序
errorlog 基于ErrorLogHandler的Monolog驱动程序
emergency alert critical error warning notice info debug
记录级别
会根据使用的信息等级来返回到stack对应的信息等级的channel。
use Illuminate\Support\Facades\Log;
Log::emergency($message);
Log::alert($message);
Log::critical($message);
Log::error($message);
Log::warning($message);
Log::notice($message);
Log::info($message);
Log::debug($message);
可以将一组上下文数据传递给日志方法。此上下文数据将被格式化并与日志消息一起显示:
use Illuminate\Support\Facades\Log;
Log::info('User failed to login.', ['id' => $user->id]);
写入特定频道使用channel,写入的内容是config/logging.php中的channel名字。
还可以直接现成创建channel,使用build:
use Illuminate\Support\Facades\Log;
Log::build([
'driver' => 'single',
'path' => storage_path('logs/custom.log'),
])->info('Something happened!');