-
Notifications
You must be signed in to change notification settings - Fork 0
/
LoggingTrait.php
150 lines (133 loc) · 4.02 KB
/
LoggingTrait.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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
<?php
namespace mgcode\commandLogger;
use mgcode\helpers\TimeHelper;
/**
* Class LoggingTrait
* Usually this trait is used for CLI commands.
* You can simply disable message output by calling in class: $this->msgLoggingEnabled = false
* @property bool $msgLoggingEnabled
* @property string $loggingCategory the category of logging messages.
*/
trait LoggingTrait
{
/** @var bool Whether to output logging messages */
protected $_msgLoggingEnabled = true;
/** @var string */
protected $_loggingCategory = 'application';
public function setLoggingCategory($value)
{
$this->_loggingCategory = $value;
}
/**
* @return bool
*/
public function getLoggingCategory()
{
return $this->_loggingCategory;
}
/**
* @return bool
*/
public function getMsgLoggingEnabled()
{
return $this->_msgLoggingEnabled;
}
/**
* You can simply disable message output by calling in class: $this->msgLoggingEnabled = false
* @param bool $value
*/
public function setMsgLoggingEnabled($value)
{
$this->_msgLoggingEnabled = (bool) $value;
}
/**
* Send message to client
* @param string $message
* @param array $params
*/
public function msg($message, $params = [])
{
if (!$this->getMsgLoggingEnabled()) {
return;
}
$message = $this->_buildMessage($message, $params);
$memory = round(memory_get_usage() / 1024 / 1024, 1);;
echo '['.TimeHelper::getTime().']'.' ['.$memory.'MB] '.$message."\r\n";
}
/**
* Sleep for some time
* @param $seconds
* @param null|int $secondsTo If set, will be used random between numbers.
*/
public function sleep($seconds, $secondsTo = null)
{
if ($secondsTo) {
$seconds = mt_rand($seconds, $secondsTo);
}
$this->msg('Sleep for {s} seconds', ['s' => $seconds]);
sleep($seconds);
}
/**
* Logs and outputs error
* Usually needed for CLI commands, when execution should continue after some error.
* @param string $msg
* @param array $params
*/
public function logError($msg, $params = [])
{
$msg = get_class($this).' Error: '.$this->_buildMessage($msg, $params);
\Yii::error($msg, $this->_loggingCategory);
$this->msg($msg);
}
/**
* Logs and outputs exception
* Usually needed for CLI commands, when execution should continue after some error.
* @param \Exception $exception
* @param boolean $includeTrace
*/
public function logException($exception, $includeTrace = true)
{
$msg = get_class($this).' exception: '.$this->getMsgFromException($exception, $includeTrace);
\Yii::error($msg, $this->_loggingCategory);
$this->msg($msg);
}
/**
* Generates error message from exception.
* @param \Exception $exception
* @param bool $includeTrace
* @return string
*/
public function getMsgFromException(\Exception $exception, $includeTrace = false)
{
$msg = $exception->getMessage().' ('.$exception->getFile().':'.$exception->getLine().')';
if ($includeTrace) {
$msg .= PHP_EOL.$exception->getTraceAsString();
}
return $msg;
}
/**
* Returns memory usage string.
* @return string
*/
public function getMemoryUsageMsg()
{
$peakUsage = round(memory_get_peak_usage() / 1024 / 1024, 1);
$memoryUsage = round(memory_get_usage() / 1024 / 1024, 1);
return "Memory usage: {$memoryUsage}MB (peak: {$peakUsage}MB) ";
}
/**
* Internal function that builds message.
* @param $message
* @param array $params
* @return string
*/
private function _buildMessage($message, $params = [])
{
$p = [];
foreach ((array) $params as $name => $value) {
$p['{'.$name.'}'] = $value;
}
$message = ($p === []) ? $message : strtr($message, $p);
return $message;
}
}