|
1
|
|
|
<?php |
|
2
|
|
|
/** |
|
3
|
|
|
* Send logs by email |
|
4
|
|
|
* |
|
5
|
|
|
* @author Vitex <[email protected]> |
|
6
|
|
|
* @copyright 2009-2016 [email protected] (G) |
|
7
|
|
|
*/ |
|
8
|
|
|
|
|
9
|
|
|
namespace Ease\Logger; |
|
10
|
|
|
|
|
11
|
|
|
/** |
|
12
|
|
|
* Log to Email |
|
13
|
|
|
* |
|
14
|
|
|
* @author Vitex <[email protected]> |
|
15
|
|
|
* @copyright 2009-2012 [email protected] (G) |
|
16
|
|
|
*/ |
|
17
|
|
|
class ToEmail extends ToMemory |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* Předvolená metoda logování. |
|
21
|
|
|
* |
|
22
|
|
|
* @var string |
|
23
|
|
|
*/ |
|
24
|
|
|
public $logType = 'email'; |
|
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
|
|
|
* Obecné konfigurace frameworku. |
|
56
|
|
|
* |
|
57
|
|
|
* @var \Ease\Shared |
|
58
|
|
|
*/ |
|
59
|
|
|
public $easeShared = null; |
|
60
|
|
|
|
|
61
|
|
|
/** |
|
62
|
|
|
* List of allready flushed messages. |
|
63
|
|
|
* |
|
64
|
|
|
* @var array |
|
65
|
|
|
*/ |
|
66
|
|
|
public $flushed = []; |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Saves obejct instace (singleton...). |
|
70
|
|
|
*/ |
|
71
|
|
|
private static $_instance = null; |
|
72
|
|
|
|
|
73
|
|
|
/** |
|
74
|
|
|
* Handle to mailer. |
|
75
|
|
|
* |
|
76
|
|
|
* @var resource |
|
77
|
|
|
*/ |
|
78
|
|
|
public $mailer = null; |
|
79
|
|
|
|
|
80
|
|
|
/** |
|
81
|
|
|
* Recipient |
|
82
|
|
|
* @var string |
|
83
|
|
|
*/ |
|
84
|
|
|
public $recipient = null; |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* Log Subject |
|
88
|
|
|
* @var string |
|
89
|
|
|
*/ |
|
90
|
|
|
public $subject = null; |
|
91
|
|
|
|
|
92
|
|
|
/** |
|
93
|
|
|
* Logger to mail Class |
|
94
|
|
|
* |
|
95
|
|
|
* @param string $recipient |
|
96
|
|
|
* @param string $subject of message |
|
97
|
|
|
*/ |
|
98
|
|
|
public function __construct($recipient = null, $subject = null) |
|
99
|
|
|
{ |
|
100
|
|
|
$this->recipient = $recipient; |
|
101
|
|
|
$this->subject = empty($subject) ? $_SERVER['PHP_SELF'] : $subject; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
/** |
|
105
|
|
|
* Pri vytvareni objektu pomoci funkce singleton (ma stejne parametry, jako |
|
106
|
|
|
* konstruktor) se bude v ramci behu programu pouzivat pouze jedna jeho |
|
107
|
|
|
* instance (ta prvni). |
|
108
|
|
|
* |
|
109
|
|
|
* @link http://docs.php.net/en/language.oop5.patterns.html Dokumentace a |
|
110
|
|
|
* priklad |
|
111
|
|
|
*/ |
|
112
|
|
|
public static function singleton() |
|
113
|
|
|
{ |
|
114
|
|
|
if (!isset(self::$_instance)) { |
|
115
|
|
|
$class = __CLASS__; |
|
116
|
|
|
if (defined('EASE_APPNAME')) { |
|
117
|
|
|
self::$_instance = new $class(constant('EASE_EMAILTO'), |
|
118
|
|
|
constant('EASE_APPNAME')); |
|
119
|
|
|
} else { |
|
120
|
|
|
self::$_instance = new $class('EaseFramework'); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
return self::$_instance; |
|
125
|
|
|
} |
|
126
|
|
|
|
|
127
|
|
|
/** |
|
128
|
|
|
* Zapise zapravu do logu. |
|
129
|
|
|
* |
|
130
|
|
|
* @param string $caller název volajícího objektu |
|
131
|
|
|
* @param string $message zpráva |
|
132
|
|
|
* @param string $type typ zprávy (success|info|error|warning|*) |
|
133
|
|
|
* |
|
134
|
|
|
* @return null|boolean byl report zapsán ? |
|
135
|
|
|
*/ |
|
136
|
|
|
public function addToLog($caller, $message, $type = 'notice') |
|
137
|
|
|
{ |
|
138
|
|
|
|
|
139
|
|
|
if (!is_object($this->mailer)) { |
|
|
|
|
|
|
140
|
|
|
$this->mailer = new \Ease\Mailer($this->recipient, $this->subject); |
|
|
|
|
|
|
141
|
|
|
$this->mailer->setUserNotification(false); |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
++$this->messageID; |
|
145
|
|
|
if (($this->logLevel == 'silent') && ($type != 'error')) { |
|
146
|
|
|
return; |
|
147
|
|
|
} |
|
148
|
|
|
if (($this->logLevel != 'debug') && ($type == 'debug')) { |
|
149
|
|
|
return; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
$this->statusMessages[$type][$this->messageID] = $message; |
|
153
|
|
|
|
|
154
|
|
|
$logLine = strftime("%D %T").' `'.$caller.'`: '.$message; |
|
155
|
|
|
|
|
156
|
|
|
$this->mailer->addItem(\Ease\Shared::linkify($logLine)); |
|
|
|
|
|
|
157
|
|
|
|
|
158
|
|
|
return true; |
|
159
|
|
|
} |
|
160
|
|
|
|
|
161
|
|
|
/** |
|
162
|
|
|
* Oznamuje chybovou událost. |
|
163
|
|
|
* |
|
164
|
|
|
* @param string $caller název volající funkce, nebo objektu |
|
165
|
|
|
* @param string $message zpráva |
|
166
|
|
|
* @param mixed $objectData data k zaznamenání |
|
167
|
|
|
*/ |
|
168
|
|
|
public function error($caller, $message, $objectData = null) |
|
169
|
|
|
{ |
|
170
|
|
|
if (!is_null($objectData)) { |
|
171
|
|
|
$message .= print_r($objectData, true); |
|
172
|
|
|
} |
|
173
|
|
|
$this->addToLog($caller, $message, 'error'); |
|
174
|
|
|
} |
|
175
|
|
|
|
|
176
|
|
|
/** |
|
177
|
|
|
* Send collected messages. |
|
178
|
|
|
*/ |
|
179
|
|
|
public function __destruct() |
|
180
|
|
|
{ |
|
181
|
|
|
if (is_object($this->mailer) && ( $this->mailer->getItemsCount() > 0 )) { |
|
|
|
|
|
|
182
|
|
|
$this->mailer->send(); |
|
183
|
|
|
} |
|
184
|
|
|
} |
|
185
|
|
|
|
|
186
|
|
|
/** |
|
187
|
|
|
* Flush Messages. |
|
188
|
|
|
* |
|
189
|
|
|
* @param string $caller |
|
190
|
|
|
* |
|
191
|
|
|
* @return int how many messages was flushed |
|
192
|
|
|
*/ |
|
193
|
|
|
public function flush($caller = null) |
|
194
|
|
|
{ |
|
195
|
|
|
$flushed = 0; |
|
196
|
|
|
if (count($this->statusMessages)) { |
|
197
|
|
|
foreach ($this->statusMessages as $type => $messages) { |
|
198
|
|
|
foreach ($messages as $messageID => $message) { |
|
199
|
|
|
if (!isset($this->flushed[$type][$messageID])) { |
|
200
|
|
|
$this->addToLog($caller, $message, $type); |
|
201
|
|
|
$this->flushed[$type][$messageID] = true; |
|
202
|
|
|
++$flushed; |
|
203
|
|
|
} |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
} |
|
207
|
|
|
|
|
208
|
|
|
return $flushed; |
|
209
|
|
|
} |
|
210
|
|
|
} |
|
211
|
|
|
|