1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* Log to stdout/stderr |
4
|
|
|
* |
5
|
|
|
* @author Vitex <[email protected]> |
6
|
|
|
* @copyright 2009-2016 [email protected] (G) |
7
|
|
|
*/ |
8
|
|
|
|
9
|
|
|
namespace Ease\Logger; |
10
|
|
|
|
11
|
|
|
/** |
12
|
|
|
* Log to syslog. |
13
|
|
|
* |
14
|
|
|
* @author Vitex <[email protected]> |
15
|
|
|
* @copyright 2009-2019 [email protected] (G) |
16
|
|
|
*/ |
17
|
|
|
class ToStd extends ToMemory implements Loggingable |
18
|
|
|
{ |
19
|
|
|
/** |
20
|
|
|
* Předvolená metoda logování. |
21
|
|
|
* |
22
|
|
|
* @var string |
23
|
|
|
*/ |
24
|
|
|
public $logType = 'stdout'; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* úroveň logování. |
28
|
|
|
* |
29
|
|
|
* @var string - silent,debug |
30
|
|
|
*/ |
31
|
|
|
public $logLevel = 'debug'; |
32
|
|
|
|
33
|
|
|
/** |
34
|
|
|
* Odkaz na vlastnící objekt. |
35
|
|
|
* |
36
|
|
|
* @var \Ease\Sand |
37
|
|
|
*/ |
38
|
|
|
public $parentObject = null; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* Pole uložených zpráv. |
42
|
|
|
* |
43
|
|
|
* @var array |
44
|
|
|
*/ |
45
|
|
|
public $statusMessages = []; |
46
|
|
|
|
47
|
|
|
/** |
48
|
|
|
* ID naposledy ulozene zpravy. |
49
|
|
|
* |
50
|
|
|
* @var int unsigned |
51
|
|
|
*/ |
52
|
|
|
private $messageID = 0; |
53
|
|
|
|
54
|
|
|
/** |
55
|
|
|
* List of allready flushed messages. |
56
|
|
|
* |
57
|
|
|
* @var array |
58
|
|
|
*/ |
59
|
|
|
public $flushed = []; |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Saves obejct instace (singleton...). |
63
|
|
|
*/ |
64
|
|
|
private static $instance = null; |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* Log Name |
68
|
|
|
* |
69
|
|
|
* @var string |
70
|
|
|
*/ |
71
|
|
|
public $logName = null; |
72
|
|
|
|
73
|
|
|
/** |
74
|
|
|
* Logovací třída. |
75
|
|
|
* |
76
|
|
|
* @param string $logName symbolic name for log |
77
|
|
|
*/ |
78
|
|
|
public function __construct($logName = null) |
79
|
|
|
{ |
80
|
|
|
$this->logName = $logName; |
81
|
|
|
} |
82
|
|
|
|
83
|
|
|
/** |
84
|
|
|
* Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako |
85
|
|
|
* konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho |
86
|
|
|
* instance (ta prvni). |
87
|
|
|
* |
88
|
|
|
* @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a |
89
|
|
|
* priklad |
90
|
|
|
*/ |
91
|
|
|
public static function singleton() |
92
|
|
|
{ |
93
|
|
|
if (!isset(self::$instance)) { |
94
|
|
|
self::$instance = new self(defined('EASE_APPNAME') ? constant('EASE_APPNAME') : 'EaseFramework'); |
95
|
|
|
} |
96
|
|
|
|
97
|
|
|
return self::$instance; |
98
|
|
|
} |
99
|
|
|
|
100
|
|
|
/** |
101
|
|
|
* Zapise zapravu do logu. |
102
|
|
|
* |
103
|
|
|
* @param string $caller název volajícího objektu |
104
|
|
|
* @param string $message zpráva |
105
|
|
|
* @param string $type typ zprávy (success|info|error|warning|*) |
106
|
|
|
* |
107
|
|
|
* @return null|boolean byl report zapsán ? |
108
|
|
|
*/ |
109
|
|
|
public function addToLog($caller, $message, $type = 'message') |
110
|
|
|
{ |
111
|
|
|
++$this->messageID; |
112
|
|
|
if (($this->logLevel == 'silent') && ($type != 'error')) { |
113
|
|
|
return; |
114
|
|
|
} |
115
|
|
|
if (($this->logLevel != 'debug') && ($type == 'debug')) { |
116
|
|
|
return; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
$this->statusMessages[$type][$this->messageID] = $message; |
120
|
|
|
|
121
|
|
|
$message = htmlspecialchars_decode(strip_tags(stripslashes($message))); |
122
|
|
|
|
123
|
|
|
$user = \Ease\User::singleton(); |
124
|
|
|
if (get_class($user) !== 'Ease\\Anonym') { |
125
|
|
|
if (method_exists($user, 'getUserName')) { |
126
|
|
|
$person = $user->getUserName(); |
127
|
|
|
} else { |
128
|
|
|
$person = $user->getObjectName(); |
129
|
|
|
} |
130
|
|
|
$caller = $person.' '.$caller; |
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
$logLine = ' `'.$caller.'` '.str_replace(['notice', 'message', 'debug', 'report', |
134
|
|
|
'error', 'warning', 'success', 'info', 'mail',], |
135
|
|
|
['**', '##', '@@', '::'], $type).' '.$message."\n"; |
136
|
|
|
if (!isset($this->logStyles[$type])) { |
137
|
|
|
$type = 'notice'; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
$this->output($type, $logLine); |
141
|
|
|
|
142
|
|
|
return true; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Output logline to stderr/stdout by its type |
147
|
|
|
* |
148
|
|
|
* @param string $type message type 'error' or anything else |
149
|
|
|
* @param string $logLine message to output |
150
|
|
|
*/ |
151
|
|
|
public function output($type, $logLine) |
152
|
|
|
{ |
153
|
|
|
switch ($type) { |
154
|
|
|
case 'error': |
155
|
|
|
$stderr = fopen('php://stderr', 'w'); |
156
|
|
|
fwrite($stderr, $this->logName.': '.$logLine); |
|
|
|
|
157
|
|
|
fclose($stderr); |
|
|
|
|
158
|
|
|
break; |
159
|
|
|
default: |
160
|
|
|
$stdout = fopen('php://stdout', 'w'); |
161
|
|
|
fwrite($stdout, $this->logName.': '.$logLine); |
162
|
|
|
fclose($stdout); |
163
|
|
|
break; |
164
|
|
|
} |
165
|
|
|
} |
166
|
|
|
|
167
|
|
|
/** |
168
|
|
|
* Last message check/modify point before output |
169
|
|
|
* |
170
|
|
|
* @param string $messageRaw |
171
|
|
|
* |
172
|
|
|
* @return string ready to use message |
173
|
|
|
*/ |
174
|
|
|
public function finalizeMessage($messageRaw) |
175
|
|
|
{ |
176
|
|
|
return trim($messageRaw).PHP_EOL; |
177
|
|
|
} |
178
|
|
|
|
179
|
|
|
/** |
180
|
|
|
* Flush Messages. |
181
|
|
|
* |
182
|
|
|
* @param string $caller |
183
|
|
|
* |
184
|
|
|
* @return int how many messages was flushed |
185
|
|
|
*/ |
186
|
|
|
public function flush($caller = null) |
187
|
|
|
{ |
188
|
|
|
$flushed = 0; |
189
|
|
|
if (count($this->statusMessages)) { |
190
|
|
|
foreach ($this->statusMessages as $type => $messages) { |
191
|
|
|
foreach ($messages as $messageID => $message) { |
192
|
|
|
if (!isset($this->flushed[$type][$messageID])) { |
193
|
|
|
$this->addToLog($caller, $message, $type); |
194
|
|
|
$this->flushed[$type][$messageID] = true; |
195
|
|
|
++$flushed; |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
} |
199
|
|
|
} |
200
|
|
|
|
201
|
|
|
return $flushed; |
202
|
|
|
} |
203
|
|
|
} |
204
|
|
|
|