1
|
|
|
<?php
|
2
|
|
|
/**
|
3
|
|
|
* @author Alexey Tatarinov <[email protected]>
|
4
|
|
|
* @link https://github.com/shogodev/argilla/
|
5
|
|
|
* @copyright Copyright © 2003-2015 Shogo
|
6
|
|
|
* @license http://argilla.ru/LICENSE
|
7
|
|
|
*/
|
8
|
|
|
Yii::import('frontend.share.formatters.*');
|
9
|
|
|
|
10
|
|
|
class ConsoleFileLogger extends CFileLogRoute
|
11
|
|
|
{
|
12
|
|
|
public $showLog = true;
|
13
|
|
|
|
14
|
|
|
private $timers = array();
|
15
|
|
|
|
16
|
|
|
/**
|
17
|
|
|
* @var CFileLogRoute $logger
|
18
|
|
|
*/
|
19
|
|
|
private $logger;
|
20
|
|
|
|
21
|
|
|
public function __construct($fileName)
|
22
|
|
|
{
|
23
|
|
|
$this->logger = new CLogger();
|
|
|
|
|
24
|
|
|
$this->logger->autoFlush = 20;
|
25
|
|
|
$this->logger->attachEventHandler('onFlush', array($this, 'onFlush'));
|
26
|
|
|
|
27
|
|
|
$this->setLogFile($fileName);
|
28
|
|
|
$this->init();
|
29
|
|
|
}
|
30
|
|
|
|
31
|
|
|
/**
|
32
|
|
|
* @param string $message
|
33
|
|
|
* @param bool $writeNow
|
34
|
|
|
* @param bool $writeMemoryUsage
|
35
|
|
|
*/
|
36
|
|
View Code Duplication |
public function log($message, $writeNow = false, $writeMemoryUsage = false)
|
|
|
|
|
37
|
|
|
{
|
38
|
|
|
if( $writeMemoryUsage )
|
39
|
|
|
$this->addMemoryUsage($message);
|
40
|
|
|
|
41
|
|
|
$this->logger->log($message, 'info', 'console');
|
42
|
|
|
|
43
|
|
|
if( $writeNow )
|
44
|
|
|
$this->flush();
|
45
|
|
|
|
46
|
|
|
if( $this->showLog )
|
47
|
|
|
echo $message.PHP_EOL;
|
48
|
|
|
}
|
49
|
|
|
|
50
|
|
|
/**
|
51
|
|
|
* @param string $message
|
52
|
|
|
* @param bool $writeNow
|
53
|
|
|
* @param bool $writeMemoryUsage
|
54
|
|
|
*/
|
55
|
|
View Code Duplication |
public function warning($message, $writeNow = false, $writeMemoryUsage = false)
|
|
|
|
|
56
|
|
|
{
|
57
|
|
|
if( $writeMemoryUsage )
|
58
|
|
|
$this->addMemoryUsage($message);
|
59
|
|
|
|
60
|
|
|
$this->logger->log($message, 'warning', 'console');
|
61
|
|
|
|
62
|
|
|
if( $writeNow )
|
63
|
|
|
$this->flush();
|
64
|
|
|
|
65
|
|
|
if( $this->showLog )
|
66
|
|
|
echo $message.PHP_EOL;
|
67
|
|
|
}
|
68
|
|
|
|
69
|
|
|
/**
|
70
|
|
|
* @param string $message
|
71
|
|
|
* @param bool $writeNow
|
72
|
|
|
* @param bool $writeMemoryUsage
|
73
|
|
|
*/
|
74
|
|
View Code Duplication |
public function error($message, $writeNow = true, $writeMemoryUsage = true)
|
|
|
|
|
75
|
|
|
{
|
76
|
|
|
if( $writeMemoryUsage )
|
77
|
|
|
$this->addMemoryUsage($message);
|
78
|
|
|
|
79
|
|
|
$this->logger->log($message, 'error', 'console');
|
80
|
|
|
|
81
|
|
|
if( $writeNow )
|
82
|
|
|
$this->flush();
|
83
|
|
|
|
84
|
|
|
if( $this->showLog )
|
85
|
|
|
echo $message.PHP_EOL;
|
86
|
|
|
}
|
87
|
|
|
|
88
|
|
|
/**
|
89
|
|
|
* Сохраняет лог в файл с очисткой сообщений из памяти
|
90
|
|
|
*/
|
91
|
|
|
public function flush()
|
92
|
|
|
{
|
93
|
|
|
$this->logger->flush();
|
94
|
|
|
}
|
95
|
|
|
|
96
|
|
|
public function __destruct()
|
97
|
|
|
{
|
98
|
|
|
$this->logger->flush();
|
99
|
|
|
}
|
100
|
|
|
|
101
|
|
|
/**
|
102
|
|
|
* Сохраняет лог в файл, сообщения остаются в памяти при прямом вызове метода
|
103
|
|
|
*/
|
104
|
|
|
public function onFlush()
|
105
|
|
|
{
|
106
|
|
|
$this->collectLogs($this->logger, true);
|
107
|
|
|
}
|
108
|
|
|
|
109
|
|
|
public function addMemoryUsage(&$message)
|
110
|
|
|
{
|
111
|
|
|
$message .= PHP_EOL.'Использовано памяти: '.Yii::app()->format->formatSize(memory_get_usage());
|
112
|
|
|
$message .= ', пик: '.Yii::app()->format->formatSize(memory_get_peak_usage());
|
113
|
|
|
$load = sys_getloadavg();
|
114
|
|
|
$message .= sprintf(", la: %.2f, %.2f, %.2f", $load[0], $load[1], $load[2]).PHP_EOL;
|
115
|
|
|
}
|
116
|
|
|
|
117
|
|
|
public function formatTime($time)
|
118
|
|
|
{
|
119
|
|
|
return sprintf("%d мин. %d с.", $time / 60, $time % 60);
|
120
|
|
|
}
|
121
|
|
|
|
122
|
|
|
public function startTimer($timerId)
|
123
|
|
|
{
|
124
|
|
|
if( isset($this->timers[$timerId]) )
|
125
|
|
|
throw new Exception("Таймер в с ".$timerId." уже создан");
|
126
|
|
|
|
127
|
|
|
$this->timers[$timerId] = microtime(true);
|
128
|
|
|
}
|
129
|
|
|
|
130
|
|
|
public function finishTimer($timerId, $message = '')
|
131
|
|
|
{
|
132
|
|
|
if( !isset($this->timers[$timerId]) )
|
133
|
|
|
throw new Exception("Таймер в с ".$timerId." не был создан");
|
134
|
|
|
|
135
|
|
|
$time = microtime(true) - $this->timers[$timerId];
|
136
|
|
|
|
137
|
|
|
$message .= $this->formatTime($time);
|
138
|
|
|
|
139
|
|
|
return $message;
|
140
|
|
|
}
|
141
|
|
|
} |
Our type inference engine has found an assignment to a property that is incompatible with the declared type of that property.
Either this assignment is in error or the assigned type should be added to the documentation/type hint for that property..