Kali_Linux_防火墙配置指南
2026/4/28 11:45:25
Log::info('email.sent', ['to' => $user->email, 'type' => 'welcome']);是 Laravel 中结构化日志(Structured Logging)的典型用法。它不只是“写一行日志”,而是通过上下文数据实现可搜索、可聚合、可告警的日志体系。
Log::info()Psr\Log\LoggerInterfaceapp('log')->info('email.sent',['to'=>'...']);'email.sent'(消息模板)['to' => ..., 'type' => ...](上下文数据)WHERE to = 'a@example.com'查询stack(组合通道)single(写入storage/logs/laravel.log)daily+errorlog或monolog第三方驱动LogRecord对象:['level'=>200,// INFO'message'=>'email.sent','context'=>['to'=>'a@example.com','type'=>'welcome'],'extra'=>[...],// 请求 ID、时间等]LineFormatter):[2025-06-15 10:00:00] production.INFO: email.sent {"to":"a@example.com","type":"welcome"} []{"level":"info","message":"email.sent","context":{"to":"a@example.com","type":"welcome"},"datetime":"2025-06-15T10:00:00+00:00","channel":"production"}Log::info("Email sent to{$user->email}(type: welcome)");// 输出: Email sent to a@example.com (type: welcome)to = 'a@example.com'Log::info('email.sent',['to'=>$user->email,'type'=>'welcome']);context.to:"a@example.com"COUNT BY context.typecontext.type:welcom(拼写错误检测)user_id而非userId// ❌ 危险:字段名动态变化Log::info('event',[$dynamicKey=>$value]);// ✅ 安全:固定字段Log::info('event',['key'=>$dynamicKey,'value'=>$value]);// config/logging.php'processors'=>[newMonolog\Processor\MaskProcessor(['password','token']),],password字段自动替换为***request_id(用于链路追踪)user_id(若已认证)Log::build()->pushContext('tenant_id',$tenantId)->info('email.sent',$context);# filebeat.ymlprocessors:-decode_json_fields:fields:["message"]target:""context.to:"a@example.com"context.type:* → Visualize → Pie Chartcontext.type:"welcome" AND context.to.keyword NOT EXISTS| 工具 | 用途 | 日志级别 |
|---|---|---|
| Laravel Log | 业务事件追踪(如邮件发送) | info,notice |
| Sentry | 异常监控(如 500 错误) | error,critical |
✅协同使用:
Log::info()记录预期行为Sentry::captureException()记录非预期错误
| 维度 | 关键点 |
|---|---|
| 本质 | 结构化事件日志,非文本记录 |
| 核心价值 | 机器可读 + 可聚合 + 可告警 |
| 生产要求 | JSON 格式 + 字段规范 + 敏感过滤 |
| 反模式 | 字符串拼接 + 动态键名 + 敏感数据明文 |
日志不是“给人看的备忘录”,
而是“给系统分析的数据流”。
用Log::info('event', $context),
你写的不是日志,
而是可观测性的基石。