nolikein / better-laravel-mattermost-logger
适用于 Laravel 应用程序的更详细的 Mattermost 记录器
Requires
- php: ^8.0
- monolog/monolog: ^3.3
- thibaud-dauce/laravel-mattermost-logger: ^1.8
Requires (Dev)
- friendsofphp/php-cs-fixer: ^3.16
- phpstan/phpstan: ^1.10
- romanzipp/php-cs-fixer-config: ^3.1
README
此库扩展了 thibaud-dauce/laravel-mattermost-logger,并允许更好地自定义它。顺便说一下,我发现默认添加一个用于转储异常上下文的表格是一个好主意。但你可以更改表格,甚至重新排列整个消息构建,如果你愿意的话。
安装
使用 Monolog 3(最新版)
您需要使用 composer 安装库。
composer require nolikein/better-laravel-mattermost-logger ^2.0
使用 Monolog 2
composer require nolikein/better-laravel-mattermost-logger ^1.0
Laravel 命令
然后添加模型迁移
artisan make:http-history-entry-migration http_history_entries
最后,运行迁移
artisan migrate
如何记录日志
为了在您的 Laravel 应用程序中使其工作,开始更改 config/logger.php
文件。
use Nolikein\BetterLaravelMattermostLogger\MattermostLogger;
'channels' => [
// If your LOG_CHANNEL env variable is set to 'stack', so add 'mattermost' to the list of stack channels
'stack' => [
'driver' => 'stack',
'channels' => ['single', 'mattermost'],
'ignore_exceptions' => false,
],
// And add this part
'mattermost' => [
'driver' => 'custom',
'via' => MattermostLogger::class,
'webhook' => env('MATTERMOST_WEBHOOK', 'PLEASE SET YOUR MATTERMOST WEBHOOK URL'),
'channel' => 'channelShortName',
'title' => "An exception occured.",
'mentions' => ['@channel'],
],
],
注意,channel
键必须与您的 Mattermost 频道的简称相对应。这意味着如果您有一个名为 "[我的频道]" 的频道,您必须将 "my channel" 作为频道名称。
然后,如上述频道所述,您必须添加 MATTERMOST_WEBHOOK
环境变量并将其设置为 webhook 链接。
MATTERMOST_WEBHOOK=https://set-your-webhook-url-instead-of-me
注意,您可以从知道自己在做什么的那一刻开始更改 env 数据名称:
可用的选项
如以下链接所述,您可以在 config/logging.php
中的 driver
和 via
键之后放置选项。请参阅 基础文档。以下为添加的选项
- title (An exception occured): 日志标题
存储您的请求和异常数据
由于模型是从 服务容器 中作为单例检索的,因此您在每个请求中只创建一次模型。因此,很容易在另一个地方获取相同的对象并将其存储起来。为了存储 HTTP 入口,我建议您使用中间件。
要生成我们的中间件,运行以下命令
artisan make:http-history-middleware HttpHistoryMiddleware
要使中间件在您的应用程序中生效,打开 App\Http\Kernel.php
文件并将 $middleware
数组填写完整
protected $middleware = [
// ...
\App\Http\Middleware\HistoricLoggerMiddleware::class,
];
修改
这部分可能有点复杂,但我尽力简化它了!所以请 RTFM
更改表格内容
表格内容是一个将内容转储为数组的 Eloquent 模型。最好的做法是创建一个新模型并添加/删除/重命名您想要记录的属性!
首先,创建您的自定义模型!
最简单的方法是运行以下命令
artisan make:http-history-entry-model HttpHistoryEntry
当 HttpHistoryEntry 是要生成的模型的名称时。
在提供者中覆盖您的模型!
在服务提供者的 boot 方法中,添加以下内容
use Nolikein\BetterLaravelMattermostLogger\Contracts\HttpHistoryEntryInterface;
use Illuminate\Foundation\Application;
use App\Models\MyCustomModel;
use Illuminate\Http\Request;
// Generate a new Http History entry that is the same for the current request
$this->app->singleton(HttpHistoryEntryInterface::class, function (Application $app, array $arguments) {
return MyCustomModel::createFromRequest(Request::capture(), $arguments['exception']);
});
将 MyCustomModel 替换为您刚刚创建的类。
完全自定义消息内容
我允许您更好地自定义消息内容。但,“消息内容”是什么,什么不是?消息内容是 Thibaud Dauce 制作的并被我覆盖的类。即 ThibaudDauce\MattermostLogger\Message
类。
这个类与我的库兼容,因为我使用的是需要从该类继承的Mattermost发送器。
第一步,创建一个自定义的消息类!
创建一个基本的PHP类,该类扩展了ThibaudDauce\MattermostLogger\Message
类。或者从我的Nolikein\BetterLaravelMattermostLogger\MattermostMessage
类开始(扩展)。
之后,你应该实现Nolikein\BetterLaravelMattermostLogger\Contracts\HttpHistoryEntryInterface
接口,该接口强制你重新实现fromArrayAndOptions
方法。在Thibaud Dauce的库中,这个静态方法创建一个self
实例,这强制任何扩展类重新定义该方法(参见晚期静态绑定),因为self关键字引用的是创建方法的类,而不是调用方法的类。但如果你扩展我的自定义消息类,你只需返回父方法即可,因为我使用了static
而不是self。
namespace App\Logging;
use Nolikein\BetterLaravelMattermostLogger\MattermostMessage as MessageWithTable;
use Nolikein\BetterLaravelMattermostLogger\Contracts\MattermostMessageInterface;
class CustomMattermostMessage extends MessageWithTable implements MattermostMessageInterface
{
public static function fromArrayAndOptions($record, $options): \ThibaudDauce\Mattermost\Message
{
return parent::fromArrayAndOptions($record, $options);
}
}
然后在一个提供者中添加你的自定义消息类!
在服务提供者的 boot 方法中,添加以下内容
use Nolikein\BetterLaravelMattermostLogger\Contracts\MattermostMessageInterface;
use App\Logging\CustomMattermostMessage;
// Generate a new Mattermost message
$this->app->bind(MattermostMessageInterface::class, function (Application $app, array $arguments) {
return CustomMattermostMessage::fromArrayAndOptions($arguments['record'], $arguments['options']);
});
将CustomMattermostMessage
更改为你刚刚创建的类。
常见问题解答(FAQ)
如果我必须使用代理怎么办?
在你的logging.php配置中,添加如下代理选项。
'mattermost' => [
// ...
'proxy' => [
'http' => env('MATTERMOST_PROXY_URL'), // Use this proxy with "http"
'https' => env('MATTERMOST_PROXY_URL'), // Use this proxy with "https",
'no' => ['localhost'], // Don't use a proxy when the error is caused by localhost
],
],
然后在你的.env
文件中添加MATTERMOST_PROXY_URL
,并设置一个URL,如https://:8125
。
注意,此配置使用Guzzle代理配置。有关更多信息,请参阅Guzzle官方文档https://docs.guzzlephp.org/en/stable/request-options.html#proxy
如果我不想使用模型来表示我的HTTP数据怎么办?
你可以使用任何实现了HttpHistoryEntryInterface的类。此接口将提供你的类必须实现以供库使用的方法。
如何指定属性的顺序?
由于模型是一个容器,在幕后是一个数组,当你通过createFromRequest
方法添加属性时,你填充属性的顺序定义了键的顺序。因此,如果你想首先显示IP地址然后是用户ID,首先填充IP地址属性,然后是用户ID属性。
为什么使用中间件,而不是将其包含在日志记录器中?
因为这不是日志记录器的角色。日志记录器将请求记录到某个地方,而中间件拦截响应然后执行一些操作。
贡献
感谢Matrisis关于注册请求、将它们记录到Mattermost然后存储的想法!