31

扩展包推荐 —— Laravel Log 增强 - PHP / Laravel / 全栈 - SegmentFault 思否

 4 years ago
source link: https://segmentfault.com/a/1190000020866033
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

扩展包推荐 —— Laravel Log 增强

发布于 2019-10-31

1460000020866036

文章转发自专业的Laravel开发者社区,原始链接:https://learnku.com/laravel/t...

Laravel Log Enhancer 是 Laravel 5.6  的一个扩展包,可以在 Laravel 日志中添加额外的数据。 得益于 Laravel 5.6 中日志的更新,这个包利用这些特性扩展日志记录并添加数据,例如请求头、内存使用情况、会话信息和其它数据 。

你可以通过修改 config/logging.php 中的 tap 属性,来添加  LogEnhancer 到你的日志通道:

'production_stack' => [
    'driver' => 'stack',
    'tap' => [Freshbitsweb\LaravelLogEnhancer\LogEnhancer::class],
    'channels' => ['daily', 'slack'],
],

配置日志通道后,下面是一条包含默认额外信息的日志示例:

[2018-03-30 05:07:23] local.INFO: Testing log {"inputs":[],"session":{"_token":"bZXCPViCzmaULIO6GsdLBbveC1yd3BbyH31zfT8c","_previous":{"url":"http://log-enhancer-demo.test"},"_flash":{"old":[],"new":[]}},"url":"/","ip":"127.0.0.1","http_method":"GET","server":"","referrer":null}

下面是增强日志记录的配置选项(包含了默认配置选项的信息):

<?php

return [
    'log_request_details' => true,
    'log_input_data' => true,
    'log_request_headers' => false,
    'log_session_data' => true,
    'log_memory_usage' => false,
    'log_git_data' => false,
    // 你可以明确不记录到日志中的用户输入信息
    'ignore_input_fields' => ['password', 'confirm_password']
];

特别注意 ignore_input_fields 选项,避免日志中记录客户的敏感信息!!!例如用户密码或信用卡账号。

你需要运行交互模式命令 artisan vendor:publish , 以激活上面的自定义配置:

php artisan vendor:publish --tag=laravel-log-enhancer-config

在一个基于 Laravel 5.6 的项目中,你可以使用如下命令利用 composer 来安装这个扩展包:

composer require freshbitsweb/laravel-log-enhancer

由于 Laravel 的包自动发现 机制,你只需要安装这个包并根据你的程序需要进行配置就可以了。

详情请查看该包的 GitHub 官方仓库。需要注意的是,这个扩展包依赖于 Laravel 5.6 的新日志系统,所以你只能在该版本或者更新版本中使用它。

它首先出现在 Laravel News 上。


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK