bitbirddev/microsoft-teams-monolog-handler 问题修复 & 功能扩展

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

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

bitbirddev/microsoft-teams-monolog-handler

Composer 安装命令:

composer require bitbirddev/microsoft-teams-monolog-handler

包简介

Monolog handler sending Microsoft Teams notifications with an Incoming Webhook

README 文档

README

Package Version

A PHP package that defines custom Monolog handler to send Microsoft Teams notifications with an Incoming Webhook. The package aims to provide global messaging & log system that uses Microsoft Teams "MessageCard" notification and uses Monolog logging library.

Features

  • Monolog wiring with Microsoft Teams channel
  • Application error notifying
  • Simple messaging

Install

$ composer require bitbirddev/microsoft-teams-monolog-handler

Please consider running composer suggest command to install required and missing dependencies related to framework you use (ex. Symfony):

$ composer suggest
bitbirddev/microsoft-teams-monolog-handler suggests:
 - symfony/monolog-bundle: The MonologBundle provides integration of the Monolog library into the Symfony framework.

Microsoft Teams Webhook setting

Follow these steps to set up new Webhook:

  • In Microsoft Teams, choose More options (⋯) next to the channel name and then choose 'Connectors'
  • Find in the list of Connectors the 'Incoming Webhook' option, and choose 'Add'
  • Provide required information for the new Webhook
  • Copy the Webhook url - that information will be used to configure the package with MICROSOFT_TEAMS_WEBHOOK_URL

Symfony configuration

Place the code below in .env file:

###> bitbirddev/microsoft-teams-monolog-handler ###
MICROSOFT_TEAMS_WEBHOOK_URL=webhook_url (without https://)
###< bitbirddev/microsoft-teams-monolog-handler ###

Register MicrosoftTeamsMonologHandler.php as a new service with the code below:

// config\services.yaml

services:
    ...

    # MICROSOFT TEAMS MONOLOG HANDLER
+    ms_teams_monolog_handler:
+        class: bitbirddev\MicrosoftTeamsNotifier\Handler\MicrosoftTeamsHandler
+        arguments:
+            $webhookDsn: '%https://env(MICROSOFT_TEAMS_WEBHOOK_URL)%'
+            $level: 'error'
+            $title: 'Message title'
+            $subject: 'Message subject'
+            $emoji:  '&#x1F6A8'
+            $color: '#fd0404'
+            $format: '[%%datetime%%] %%channel%%.%%level_name%%: %%message%%'

$webhookDsn:
Microsoft Teams webhook url

$level:
the minimum level for handler to be triggered and the message be logged in the channel (Monolog/Logger class: ‘error’ = 400)

$title (nullable):
title of Microsoft Teams Message

$subject (nullable):
subject of Microsoft Teams Message

$emoji (nullable):
emoji of Microsoft Teams Message (displayed next to the message title). Value needs to reflect the pattern: ‘&#x<EMOJI_HEX_CODE>’

$color (nullable):
hexadecimal color value for Message Card color theme

$format (nullable):
every handler uses a Formatter to format the record before logging it. This attribute can be set to overwrite default log message (available options: %datetime% | %extra.token% | %channel% | %level_name% | %message%).

Modify your Monolog settings that will point from now to the new handler:

// config\packages\dev\monolog.yaml
// config\packages\prod\monolog.yaml

monolog:
    handlers:
        ...

        # MICROSOFT TEAMS HANDLER
+        teams:
+            type: service
+            id: ms_teams_monolog_handler

type:
handler type (in our case this references custom notifier service)

id:
notifier service class \bitbirddev\MicrosoftTeamsNotifier\LogMonolog

Laravel configuration

Place the code below in .env file:

###> bitbirddev/microsoft-teams-monolog-handler ###
MICROSOFT_TEAMS_WEBHOOK_URL=webhook_url (without https://)
###< bitbirddev/microsoft-teams-monolog-handler ###

Modify your Monolog logging settings that will point to the new handler:

Att: definition of ALL parameters is compulsory - please use NULL value for attributes you want to skip.

// config\logging.php

<?php

use Monolog\Handler\NullHandler;
use Monolog\Handler\StreamHandler;
use Monolog\Handler\SyslogUdpHandler;

return [
    'channels' => [
        'stack' => [
            'driver' => 'stack',
-            'channels' => ['single'],
+            'channels' => ['single', 'custom'],
            'ignore_exceptions' => false
        ],

         # MICROSOFT TEAMS MONOLOG HANDLER
+       'custom' => [
+            'driver' => 'custom',
+            'via' => \bitbirddev\MicrosoftTeamsNotifier\LogMonolog::class,
+            'webhookDsn' => 'https://env('MICROSOFT_TEAMS_WEBHOOK_URL)',
+            'level'  => env('LOG_LEVEL', 'debug'), // or simply 'debug'
+            'title'  => 'Message Title', // can be NULL
+            'subject'  => 'Message Subject', // can be NULL
+            'emoji'  => '&#x1F3C1', // can be NULL
+            'color'  => '#fd0404', // can be NULL
+            'format' => '[%datetime%] %channel%.%level_name%: %message%' // can be NULL
+        ],

...

driver:
is a crucial part of each channel that defines how and where the log message is recorded. The ‘custom’ driver calls a specified factory to create a channel.

via:
factory class which will be invoked to create the Monolog instance

webhookDsn:
Microsoft Teams webhook url

level:
the minimum level for handler to be triggered and the message be logged in the channel (Monolog/Logger class: ‘debug’ = 100)

title (nullable):
title of Microsoft Teams Message

subject (nullable):
subject of Microsoft Teams Message

emoji (nullable):
emoji of Microsoft Teams Message (displayed next to the message title). Value needs to reflect the pattern: ‘&#x<EMOJI_HEX_CODE>’

color (nullable):
hexadecimal color value for Message Card color theme

format (nullable):
message template - available options: %datetime% | %extra.token% | %channel% | %level_name% | %message%

Usage

Correctly configured service in Symfony/Laravel will raise Logs in Microsoft Teams automatically accordingly to level assigned to.

Symfony - manual messaging

// LoggerInterface $logger
$logger->info('Info message with custom Handler');
$logger->error('Error message with custom Handler');

Laravel - manual messaging

// Illuminate\Support\Facades\Log
Log::channel('custom')->info('Info message with custom Handler');
Log::channel('custom')->error('Error message with custom Handler');

License

The code is available under the MIT license. See the LICENSE file for more info.

bitbirddev/microsoft-teams-monolog-handler 适用场景与选型建议

bitbirddev/microsoft-teams-monolog-handler 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 3.77k 次下载、GitHub Stars 达 0, 最近一次更新时间为 2023 年 12 月 31 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 bitbirddev/microsoft-teams-monolog-handler 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 0
  • Watchers: 0
  • Forks: 5
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-12-31