/
LogTarget.php
58 lines (51 loc) · 1.27 KB
/
LogTarget.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
<?php
/**
* @link https://github.com/nfedoseev/yii2-external-target
* @copyright Copyright (c) 2016 Nikita Fedoseev
* @license https://github.com/nfedoseev/yii2-external-target/blob/master/LICENSE.md
*/
namespace nfedoseev\yii2\ExternalTarget;
use Yii;
use yii\log\Target;
/**
* Class LogTarget collect and sent logs
*
* @package nfedoseev\yii2\ExternalTarget
*/
class LogTarget extends Target
{
/**
* @var array Statuses for ignoring
*/
public $ignore_statuses = [];
/**
* @var bool
*/
private static $send = false;
/**
* Exports log [[messages]] to a specific destination.
* Child classes must implement this method.
*/
public function export()
{
if (self::$send == false) {
if (!in_array(Yii::$app->getResponse()->statusCode, $this->ignore_statuses)) {
Client::get()->sent($this->exportMessages(), LogmanClient::SOURCE_TARGET);
self::$send = true;
}
}
}
/**
* Export messages
* @return array
*/
private function exportMessages()
{
$data = [];
foreach ($this->messages as $msg) {
$message = new Message($msg);
$data[] = $message->getData();
}
return $data;
}
}