saasscaleup/laravel-log-alarm

Laravel日志警报器帮助您在系统发生错误时设置警报,并通过Slack和电子邮件发送通知

v1.0.2 2024-07-10 02:25 UTC

This package is auto-updated.

Last update: 2024-09-02 19:32:30 UTC


README

Main Window two

为您的Laravel应用程序提供实时日志监控和错误检测

Youtube · Twitter · Facebook · Buy Me a Coffee

Latest Stable Version Total Downloads License

Laravel日志警报器

日志警报器是一个强大且易于使用的Laravel包,旨在增强您的应用程序的日志功能。无论您是在运行一个小型Web应用还是大型企业系统,Log Alarm都能通过提供实时监控、错误检测和通过Slack和电子邮件发送的即时通知来帮助您保持对日志的掌控。

✨ 特性

  • 实时日志监控:持续监听日志事件并跟踪您应用程序的健康状况。
  • 错误检测:自动检测并响应错误日志事件。
  • 自定义通知:当发生错误时,将即时通知发送到Slack和电子邮件。
  • 频率控制:设置阈值和延迟以避免通知洪水。
  • 基于缓存的警报系统:高效跟踪错误发生次数,无需数据库操作的开销。
  • 灵活配置:轻松配置设置并使用自定义字符串指定错误条件。

为什么选择日志警报器?

在当今快节奏的数字环境中,对应用程序问题的及时响应至关重要。日志警报器确保您和您的团队能够及时了解关键错误,减少停机时间并提高应用程序的可靠性。非常适合希望增强其监控能力的开发人员、系统管理员和DevOps团队。

要求

  • PHP >= 7
  • Laravel >= 5

⬇️ 安装

通过Composer安装包

composer require saasscaleup/laravel-log-alarm

对于Laravel < 5.5

将服务提供者添加到config/app.php中的providers部分

Saasscaleup\LogAlarm\LogAlarmServiceProvider::class,

发布包的配置文件

通过运行以下命令发布包的配置、迁移和视图文件

php artisan vendor:publish --provider="Saasscaleup\LogAlarm\LogAlarmServiceProvider"

使用方法

该包将自动开始监听您应用程序的日志事件。通过在config/log-alarm.php文件中自定义设置来满足您的需求。例如

LA_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your/webhook/url
LA_NOTIFICATION_EMAIL=your-email@example.com
LA_NOTIFICATION_EMAIL_SUBJECT="Log Alarm Notification"

为了使用当前默认设置触发日志警报器,您只需做以下操作

  1. 添加您的LA_SLACK_WEBHOOK_URLLA_NOTIFICATION_EMAIL
  2. tinker或您的Controller中打印错误日志5次

示例

php artisan tinker
\Log::error('Log alarm');
\Log::error('Log alarm');
\Log::error('Log alarm');
\Log::error('Log alarm');
\Log::error('Log alarm');

在第五条错误日志消息之后,将发送电子邮件和Slack通知!(电子邮件示例)

From: Laravel <hello@example.com>
To: admin@example.com, admin2@example.com
Subject: Log Alarm Notification


The Error was occurred 5 times in the last 1 minutes:

LOG_LEVEL: error | LOG_MESSAGE: Log alarm

🔧 配置

将以下环境变量添加到您的.env文件中

LA_ENABLED=true
LA_LOG_TYPE=error
LA_LOG_TIME_FRAME=1
LA_LOG_PER_TIME_FRAME=5
LA_DELAY_BETWEEN_ALARMS=5
LA_SLACK_WEBHOOK_URL=https://hooks.slack.com/services/your/webhook/url
LA_NOTIFICATION_EMAIL=your-email@example.com
LA_NOTIFICATION_EMAIL_SUBJECT="Log Alarm Notification"

这是config/log-alarm.php的完整配置文件内容

<?php

return [

    // enable or disable LOG ALARM
    'enabled' => env('LA_ENABLED', true),

    // log listener for specific log type
    'log_type' => env('LA_LOG_TYPE', 'error'), // also possible: 'error,warning,debug'

    // log time frame - log time frame to listen - in minutes
    "log_time_frame" => env('LA_LOG_TIME_FRAME', 1),

    // log per time frame - How many log to count per time frame until alarm trigger 
    "log_per_time_frame" => env('LA_LOG_PER_TIME_FRAME', 5),

    // delay between alarms in minutes - How many minutes to delay between alarms
    'delay_between_alarms' => env('LA_DELAY_BETWEEN_ALARMS', 5),

    // log listener for specific word inside log messages
    'specific_string' => env('LA_SPECIFIC_STRING', ''), // also possible: 'table lock' or 'foo' or 'bar' or leave empty '' to enable any word

    // notification message for log alarm
    'notification_message' => env('LA_NOTIFICATION_MESSAGE', 'Log Alarm got triggered!'),
    
    // Slack webhook url for log alarm
    'slack_webhook_url' => env('LA_SLACK_WEBHOOK_URL', ''),

    // notification email address for log alarm
    'notification_email' => env('LA_NOTIFICATION_EMAIL', 'admin@example.com,admin2@example.com'),

    // notification email subject for log alarm
    'notification_email_subject' => env('LA_NOTIFICATION_EMAIL_SUBJECT', 'Log Alarm Notification'),
];

banner

贡献

我们欢迎贡献!请随时提交Pull Request或打开GitHub上的Issue。

许可

此包是开源软件,许可协议为MIT

支持 🙏😃

如果您喜欢这个教程,并希望支持我的频道,那么我会继续发布精彩内容,帮助您成为一个拥有出色云技能的理想开发者...如果您能这样做,我将非常感激

  1. 订阅我们的YouTube频道
  2. 为我买一杯咖啡 ❤️

感谢您的支持 :)

今天就开始提升您Laravel应用的监控能力,使用Log Alarm。通过安装此包,体验改进的错误管理和更快的响应时间。