GithubHelp home page GithubHelp logo

huozi1024 / work-wechat-robot Goto Github PK

View Code? Open in Web Editor NEW
5.0 2.0 2.0 54 KB

企业微信机器人sdk 支持作为monolog的通道来发送日志

License: MIT License

PHP 100.00%
monolog wxwork wechat-robot monolog-handler robot

work-wechat-robot's Introduction

work-wechat-robot

企业微信机器人sdk

0.安装

composer require huo-zi/work-wechat-robot

php < 8 || laravel < 9

composer require huo-zi/work-wechat-robot:~2.0

1.使用

1.1 直接使用

$robot = new WorkWechatRobot($robotKey);
$robot->text($content); // 文本消息
$robot->markdown($content); // markdown消息
$robot->image($filename); // 图片消息 支持本地图片和网络图片
$robot->news($title, $url, $desc, $picurl); // 图文消息
$robot->file($filename); // 发送上传文件

1.2 v2.1.0之后支持创建消息对象发送消息:

$messsage = new Text();     // new Markdwon(); new Image()...
$messsage->content('文本消息');
$messsage->send($robotKey); // 或使用 $robot->message($messsage);

2.作为Monolog的通道使用

2.1 配置通道

  • laravel框架 在配置文件logging.phpchannels数组中增加:
'wxwork_robot' => [  
    'driver' => 'monolog',  
    'level' => 'notice',  
    'handler' => \Huozi\WorkWechat\Monolog\Handler\RobotHandler::class,  
    'handler_with' => [  
        'robotKey' => 'your_wxwork_robot_key',  
    ],  
],

然后修改channels节点stack,在channels中增加wxwork_robot

'stack' => [
    'driver' => 'stack',
    'channels' => ['single', ... , 'wxwork_robot'],
    'ignore_exceptions' => false,
],

详见laravel高度自定义Monolog通道

  • 其他框架
$logger = new \Monolog\Logger($name);
$logger->pushHandler(new RobotHandler($robotKey));

2.2 日志格式化

提供了TextFormatterMarkdownFormatter格式化原始日志,使日志内容方便阅读

  • laravel框架,修改logging.php, 增加formatter
'wxwork_robot' => [
    'driver' => 'monolog',
    'level' => 'notice',
    'handler' => \Huozi\WorkWechat\Monolog\Handler\RobotHandler::class,
    'handler_with' => [
        'robotKey' => 'your_wxwork_robot_key',
    ],
    'formatter' => \Huozi\WorkWechat\Monolog\Formatter\MarkdownFormatter::class,```
 ],

TextFormatterMarkdownFormatter都提供了默认的格式化结构,如果需要自定义可以:

    'formatter' => \Huozi\WorkWechat\Monolog\Formatter\TextFormatter::class,
    'formatter_with' => [
        'messageFormat' => '{level_name}:{message} \n {extra.file}:{extra.line}'</b>
    ]
  • 其他框架
$messageFormat = '{level_name}:{message} \n {extra.file}:{extra.line}';
$formatter = new TextFormatter($messageFormat);
$logger->pushHandler((new RobotHandler($robotKey))->setFormatter($formatter));

License

Licensed under The MIT License (MIT).

work-wechat-robot's People

Contributors

huozi1024 avatar lio990527 avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

Forkers

huo-zi marunrun

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.