kopitar/laravel-teams-logger 问题修复 & 功能扩展

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

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

kopitar/laravel-teams-logger

Composer 安装命令:

composer require kopitar/laravel-teams-logger

包简介

Laravel package for sending configurable messages to Microsoft Teams via the incoming webhook url

README 文档

README

Latest Version on Packagist GitHub Tests Action Status Total downloads

Log handler for Laravel for sending log messages to Microsoft Teams with the Teams Incoming Webhook Connector.

Tested on:

  • Laravel 10.x (PHP 8.3, 8.2, 8.1)
  • Laravel 11.x (PHP 8.3, 8.2)

Features

  • send simple styled log messages
  • send card styled *log messages
  • include facts in card log messages
  • include actions in card log messages
  • configurable content and visuals (colors, avatars)

Installation

Require this package with composer.

$ composer require kopitar/laravel-teams-logger

Configuration

After installing the package using composer, create a new custom channel in config/logging.php:

'teams' => [
    'driver'  => 'custom',
    'via'     => \Kopitar\LaravelTeamsLogger\TeamsLoggerFactory::class,
    'level'   => 'debug',
    'url'     => env('TEAMS_WEBHOOK_URL')
],

Copy teams_logger.php config file from this package to your Laravel config folder with the following command:

$ php artisan vendor:publish --tag=teams

Add TEAMS_WEBHOOK_URL variable to your .env file with the URL provided by your Microsoft Teams Connector. (See MS Teams Documentation for more information on where to get your webhook URL).

There are additional .env variables for this package that are optional and have default values. The names of these variables can be found in config/teams_logger.php and on the list bellow. These optional .env variables provide a way for you to change number of retries, disable avatars, set a default message type etc.

  • TEAMS_LOG_NAME (name of logger, also used as title in card type messages)
  • TEAMS_LOG_TYPE (sets one of three types of log messages)
  • TEAMS_RETRIES (number of retries if request to Teams webhook fails)
  • TEAMS_MARKDOWN (disable or enable markdown in card type messages)
  • TEAMS_AVATAR (disable or enable avatar image in messages)

To change colors or avatar images you need to replace the values found for each log level in config/teams_logger.php.

Usage

Simple type:

To send a simple style log message to Teams use the following code (assuming type is configured to simple):

Log::channel('teams')->info('Neque porro quisquam est qui dolorem!');

Result:

Screenshot

Card type:

To send a card style log message to Teams use the following code (assuming type is configured to card and use_avatar is set to true):

Log::channel('teams')->debug('Neque porro quisquam est qui dolorem!');

Result:

Screenshot

Json type:

To send any style log messages to Teams you can also simply use JSON formatted data (assuming type is configured to json):

Log::channel('teams')->info('{"text":"Neque porro quisquam est qui dolorem!","themeColor":"#df0087"}');

This example would produce a simple log message with a themeColor of #df0087

Result:

Screenshot

Advanced Usage

1. Override configuration

Almost any configuration value can be overriden on any log message by defining a new value in the second parameter. The simplest example would be overriding the type setting. If your config says to use simple type for your log messages but you want to send a card type at a specific place in your code you can do this like in the example below:

Log::channel('teams')->info(
    'Neque porro quisquam est qui dolorem!', ['type' => 'card']
);

Other possible configuration overrides are listed in the example below:

Log::channel('teams')->debug(
    'Neque porro **quisquam** est qui dolorem! [Markdown disabled](http://example.net)',
    [
        'type' => 'card',
        'title' => 'Alternative title',
        'themeColor' => '#0000ff',
        'avatar' => false,
        'markdown' => false
    ]
);

This sets type to 'card', activityTitle to 'Alternative title', sets the themeColor to blue and disables avatar image and markdown.

Result:

Screenshot

If sending messages of simple type the only config value you are able to override is themeColor. Markdown is used by default and cannot be disabled!

Log::channel('teams')->debug('Neque porro **quisquam** est qui dolorem!', ['themeColor' => '#0000ff']);

2. Facts

When using 'card' type you can also pass a facts parameter which needs to be an array. The contents of this array are then rendered as a key:value list in the card message.

Log::channel('teams')->critical(
    'Neque porro quisquam est qui dolorem!',
    [
        'type' => 'card',
        'facts' => [
            'happened at' => now()->toDayDateTimeString(),
            'file' => __FILE__,
            'severity' => 'Critical'
        ],
    ]
);

Result:

Screenshot

3. Actions

When using 'card' type you can also pass an actions parameter which needs to be an array. Actions provide a way for you to include a potentialAction property in your message which is an array of actions which add interactive actions in your messages like adding comments, changing statuses, opening links etc.

Read Actions documentation on learn.microsoft.com for more information!

        Log::channel('teams')->info("Neque porro quisquam est qui dolorem!", [
           'type' => 'card',
           'actions' => [[
                '@type' => 'ActionCard',
                'name' => 'Add a comment',
                'inputs' => [
                    [
                        '@type' => 'TextInput',
                        'id' => 'comment',
                        'isMultiline' => false,
                        'title' => 'Add a comment here for this task'
                    ]
                ],
                'actions' => [
                    [
                        '@type' => 'HttpPOST',
                        'name' => 'Add comment',
                        'target' => 'https =>//learn.microsoft.com/outlook/actionable-messages'
                    ]
                ],
            ]]
        ]);

Result:

Screenshot

Preview (simple type)

Screenshot

Preview (card type)

Screenshot

Changelog

Please see CHANGELOG for more information on what has changed recently.

License

This laravel-teams-logging package is available under the MIT license. See LICENSE.md file for more info.

kopitar/laravel-teams-logger 适用场景与选型建议

kopitar/laravel-teams-logger 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 1.4k 次下载、GitHub Stars 达 2, 最近一次更新时间为 2023 年 08 月 06 日, 在 PHP 生态内属于活跃度较高的组件。

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

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

围绕 kopitar/laravel-teams-logger 我们能提供哪些服务?
定制开发 / 二次开发

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

BUG 修复 & 性能优化

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

项目外包 & 长期维护

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

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

统计信息

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

GitHub 信息

  • Stars: 2
  • Watchers: 1
  • Forks: 1
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-08-06