PHP code example of buexplain / alarm

1. Go to this page and download the library: Download buexplain/alarm library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

buexplain / alarm example snippets




declare(strict_types=1);

use Monolog\Logger;
use Monolog\Formatter\LineFormatter;
use Monolog\Handler\RotatingFileHandler;

return [
    'default' => [
        'handlers'=>[
            //默认的文件日志处理器
            [
                'class' => RotatingFileHandler::class,
                'constructor' => [
                    'filename' => BASE_PATH . '/runtime/logs/hyperf.log',
                    'level'=>Level::Error,
                ],
                'formatter' => [
                    'class' => LineFormatter::class,
                    'constructor' => [
                        'format' => null,
                        'dateFormat' => null,
                        'allowInlineLineBreaks' => true,
                    ],
                ],
            ],
            //告警日志处理器
            [
                'class' => \Alarm\Handler::class,
                'constructor' => [
                    //此处的handler对应的正是config/autoload/alarm.php配置的key值
                    'handlers'=>[
                        'dingTalk',
                        'weChat',
                        'feiShu',
                    ],
                    //接收的日志级别
                    'level'=>\Monolog\Level::Error,
                ],
            ],
        ],
    ],
    'alarm' => [  // 这是另外一种使用方式 \Hyperf\Utils\ApplicationContext::getContainer()->get(\Hyperf\Logger\LoggerFactory::class)->get('xxxxx' , 'alarm')->error('tips');
        'handlers' => [
            //告警日志处理器
            [
                'class' => \Alarm\Handler::class,
                'constructor' => [
                    //此处的handler对应的正是config/autoload/alarm.php配置的key值
                    'handlers'=>[
                        'feiShu',
                    ],
                    //接收的日志级别
                    'level'=>\Monolog\Level::Error,
                ],
            ],
        ],
        'formatter' => [
            'class'       => Monolog\Formatter\LineFormatter::class,
            'constructor' => [
                'format'                => null,
                'dateFormat'            => 'Y-m-d H:i:s.u',
                'allowInlineLineBreaks' => true,
            ],
        ],
    ],
];

$logger = \Hyperf\Utils\ApplicationContext::getContainer()->get(\Hyperf\Logger\LoggerFactory::class)->get();
//at群内用户需要提供手机号(飞书不支持)
$logger->error('at一个用户', ['@'=>'135xxxxxxx1']); // 注意: 目前飞书不支持 @指定用户提醒 , 只能群呼
$logger->error('at两个用户', ['@'=>['135xxxxxxx1', '135xxxxxxx2']]); 
$logger->error('at所有人', ['@'=>'all']);