-
Notifications
You must be signed in to change notification settings - Fork 2
/
Target.php
130 lines (110 loc) · 3.69 KB
/
Target.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
<?php
/**
* @link http://www.yiiframework.com/
* @copyright Copyright (c) 2008 Yii Software LLC
* @license http://www.yiiframework.com/license/
*/
namespace apollo11\logger;
use yii\helpers\ArrayHelper;
use yii\helpers\VarDumper;
/**
* @inheritdoc
*/
abstract class Target extends \yii\log\Target
{
/**
* @author Zura Sekhniashvili <[email protected]>
* @var array Config array which should be configured in `->prepareConfig` method and used in `->sendMessage` method
*/
protected $config = [];
/**
* @author Zura Sekhniashvili <[email protected]>
* @var bool Whether to send messages to target asynchronously
*/
public $async = true;
/**
* @author Zura Sekhniashvili <[email protected]>
* @var bool
*/
public $consoleAppPath = false;
/**
* @author Zura Sekhniashvili <[email protected]>
* @var string php executable
*/
public $phpExecPath = 'php';
/**
* @var
*/
public $excludeKeys = [];
/**
* Generates the context information to be logged.
* The default implementation will dump user information, system variables, etc.
* @return string the context information. If an empty string, it means no context information.
*/
protected function getContextMessage()
{
$context = ArrayHelper::filter($GLOBALS, $this->logVars);
$result = [];
foreach ($context as $key => $value) {
$value = $this->replaceMustExcludeKeys($value);
$result[] = "\${$key} = " . VarDumper::dumpAsString($value);
}
return implode("\n\n", $result);
}
/**
* @param $key
* @return bool
*/
private function mustBeExcluded($key)
{
foreach ($this->excludeKeys as $excludeKey) {
$formattedExcludeKey = $this->clearFromStars($excludeKey);
$loweredKey = strtolower($key);
if ($excludeKey[0] === '*' && substr($excludeKey, -1) === '*' && strpos($loweredKey, $formattedExcludeKey) !== false
|| $excludeKey[0] === '*' && preg_match('/' . $formattedExcludeKey . '$/', $loweredKey)
|| substr($excludeKey, -1) === '*' && preg_match('/^' . $formattedExcludeKey . '/', $loweredKey)
|| $loweredKey === $formattedExcludeKey) {
return true;
}
}
return false;
}
public function replaceMustExcludeKeys($data)
{
foreach ($data as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->replaceMustExcludeKeys($value);
} else if ($this->mustBeExcluded($key)) {
$data[$key] = str_repeat("*", strlen($value));
}
}
return $data;
}
public function getFormatMessage()
{
$messages = array_map([$this, 'formatMessage'], $this->messages);
return wordwrap(implode("\n", $messages), 70);
}
public function clearFromStars($excludeKey)
{
return strtolower(str_replace("*", "", $excludeKey));
}
/**
* Exports log [[messages]] to a specific destination.
* Child classes must implement this method.
*/
public function export()
{
$this->prepareConfig();
$this->messages = [];
if ($this->async === true && $this->consoleAppPath !== false) {
$param = base64_encode(serialize($this));
$cmd = $this->phpExecPath . ' ' . $this->consoleAppPath . " async/handle $param > /dev/null 2>/dev/null &";
exec($cmd);
} else {
$this->sendMessage();
}
}
abstract public function sendMessage();
abstract protected function prepareConfig();
}