saasscaleup/laravel-log-alarm 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

saasscaleup/laravel-log-alarm

Composer 安装命令:

composer require saasscaleup/laravel-log-alarm

包简介

Laravel log Alarm help you to set up alarm when errors occur in your system and send you a notification via Slack and email

README 文档

README

Main Window two

Real-time Log Monitoring and Error Detection for Your Laravel Applications

Youtube · Twitter · Facebook · Buy Me a Coffee

Latest Stable Version Total Downloads License

Log Alarm for Laravel

Log Alarm is a robust and easy-to-use Laravel package designed to enhance your application's logging capabilities. Whether you're running a small web app or a large enterprise system, Log Alarm helps you stay on top of your logs by providing real-time monitoring, error detection, and instant notifications via Slack and email.

✨ Features

  • Real-time Log Monitoring: Continuously listen for log events and keep track of your application's health.
  • Error Detection: Automatically detect and respond to error log events.
  • Customizable Notifications: Send instant notifications to Slack and email when errors occur.
  • Frequency Control: Set thresholds and delays to avoid notification flooding.
  • Cache-based Alert System: Efficiently track error occurrences without the overhead of database operations.
  • Flexible Configuration: Easily configure settings and specify error conditions with custom strings.

Why Choose Log Alarm?

In today's fast-paced digital environment, timely responses to application issues are crucial. Log Alarm ensures that you and your team are promptly informed about critical errors, reducing downtime and improving your application's reliability. Perfect for developers, system administrators, and DevOps teams looking to enhance their monitoring capabilities.

Requirements

  • PHP >= 7
  • Laravel >= 5

⬇️ Installation

Install the package via Composer:

composer require saasscaleup/laravel-log-alarm

For Laravel < 5.5

Add Service Provider to config/app.php in providers section

Saasscaleup\LogAlarm\LogAlarmServiceProvider::class,

Publish package's config file

Publish package's config, migration and view files by running below command:

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

Usage

The package will automatically start listening to your application's log events. Customize the settings in the config/log-alarm.php file to match your requirements. For Example:

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"

In order to trigger the Log Alarm with the current defualt setting, All you need to do is

  1. Add your LA_SLACK_WEBHOOK_URL and LA_NOTIFICATION_EMAIL
  2. Print Error log 5 times in tinker or in your Controller

Telegram Notification Configuration

Add LA_TELEGRAM_BOT_TOKEN and LA_TELEGRAM_CHAT_ID to your .env

Getting a Bot Token:

  1. Open Telegram and search for @BotFather
  2. Start a chat with BotFather and send /newbot
  3. Follow the prompts:
  • Provide a name for your bot
  • Provide a username for your bot (must end in 'bot'). Example: MyTelegramBot or my_telegram_bot
  1. BotFather will give you a bot token that looks like this: 123456789:ABCdefGHIjklMNOpqrsTUVwxyz

Getting the Chat ID:

Method 1 (For Direct Messages):

  1. Start a chat with your new bot
  2. Send it any message
  3. Visit this URL in your browser (replace with your bot token):
https://api.telegram.org/bot<YourBotTokenHere>/getUpdates
  1. Look for the "chat" -> "id" field in the JSON response. It will be a number like 123456789.
"chat": {
    "id": 123456789,
},

Method 2 (For Groups):

  1. Add your bot to the group
  2. Send a message in the group
  3. Use the same /getUpdates URL method above
  4. Look for "chat" -> "id" in the response. For groups, it will be a negative number like -123456789

Important Tips:

Keep your bot token secret - anyone with it can control your bot For groups, make sure to give the bot permission to read messages If you don't see any updates in the /getUpdates response, send a new message to the bot or group The chat ID is permanent for that chat or group unless the bot is removed and re-added

Tinker example:

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

After the fifth error log message, email and slack notification will be sent! (Email example)

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

🔧 Configuration

Update your .env file with the following environment variables:

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"

Here's the full configuration file content for 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" => (int)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" => (int)env('LA_LOG_PER_TIME_FRAME', 5),

    // delay between alarms in minutes - How many minutes to delay between alarms
    'delay_between_alarms' => (int)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

Contribution

We welcome contributions! Please feel free to submit a Pull Request or open an Issue on GitHub.

License

This package is open-sourced software licensed under the MIT license.

Support 🙏😃

If you Like the tutorial and you want to support my channel so I will keep releasing amzing content that will turn you to a desirable Developer with Amazing Cloud skills... I will realy appricite if you:

  1. Subscribe to our youtube
  2. Buy me A coffee ❤️

Thanks for your support :)

Enhance your Laravel application's monitoring capabilities today with Log Alarm. Get started by installing the package and experience improved error management and faster response times.

saasscaleup/laravel-log-alarm 适用场景与选型建议

saasscaleup/laravel-log-alarm 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 28.93k 次下载、GitHub Stars 达 269, 最近一次更新时间为 2024 年 06 月 20 日, 在 PHP 生态内属于活跃度较高的组件。

它主要适用于以下技术方向: 「log」 「logging」 「email」 「error」 「monitoring」 「notification」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。

我们在过去多个企业项目中使用过 saasscaleup/laravel-log-alarm 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 saasscaleup/laravel-log-alarm 我们能提供哪些服务?
定制开发 / 二次开发

基于 saasscaleup/laravel-log-alarm 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 28.93k
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 269
  • 点击次数: 32
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 269
  • Watchers: 3
  • Forks: 17
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2024-06-20