/
DingTalkTarget.php
65 lines (60 loc) · 1.61 KB
/
DingTalkTarget.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
/**
* @link http://www.tintsoft.com/
* @copyright Copyright (c) 2012 TintSoft Technology Co. Ltd.
* @license http://www.tintsoft.com/license/
*/
namespace xutl\dingtalk;
use yii\di\Instance;
use yii\log\Target;
/**
* DingTalkTarget sends selected log messages to the specified dingTalk.
*
* etc.:
*
* ```php
* 'components' => [
* 'log' => [
* 'targets' => [
* [
* 'class' => 'xutl\dingtalk\DingTalkTarget',
* 'dingTalk' => 'dingTalk',
* 'levels' => ['error', 'warning'],
* ],
* ],
* ],
* ],
* ```
*
* In the above `dingTalk` is ID of the component that sends message and should be already configured.
*
* @author Qiang Xue <qiang.xue@gmail.com>
* @since 2.0
*/
class DingTalkTarget extends Target
{
/**
* @var DingTalk|array|string the dingTalk object or the application component ID of the dingTalk object.
* After the DingTalkTarget object is created, if you want to change this property, you should only assign it
* with a dingTalk object.
*/
public $dingTalk = 'dingTalk';
/**
* @inheritdoc
*/
public function init()
{
parent::init();
$this->dingTalk = Instance::ensure($this->dingTalk, 'xutl\dingtalk\DingTalk');
}
/**
* Sends log messages to specified dingTalk.
* @throws \yii\httpclient\Exception
*/
public function export()
{
$messages = array_map([$this, 'formatMessage'], $this->messages);
$body = wordwrap(implode("\n", $messages), 70);
$this->dingTalk->sendText($body);
}
}