1: <?php
2:
3: namespace Alpha\Util\Logging;
4:
5: use Alpha\Util\Config\ConfigProvider;
6: use Alpha\Util\Email\EmailProviderFactory;
7: use Alpha\Util\Http\Session\SessionProviderFactory;
8: use Alpha\Util\Http\Request;
9: use Alpha\Model\ActionLog;
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: class Logger
54: {
55: 56: 57: 58: 59: 60: 61:
62: private $logProvider;
63:
64: 65: 66: 67: 68: 69: 70:
71: private $level;
72:
73: 74: 75: 76: 77: 78: 79:
80: private $classname;
81:
82: 83: 84: 85: 86: 87: 88:
89: private $debugClasses = array();
90:
91: 92: 93: 94: 95: 96: 97:
98: private $request;
99:
100: 101: 102: 103: 104: 105: 106:
107: public function __construct($classname)
108: {
109: $config = ConfigProvider::getInstance();
110:
111: $this->classname = $classname;
112: $this->level = $config->get('app.log.trace.level');
113: $this->debugClasses = explode(',', $config->get('app.log.trace.debug.classes'));
114: $this->logProvider = LogProviderFactory::getInstance('Alpha\Util\Logging\LogProviderFile');
115: $this->logProvider->setPath($config->get('app.file.store.dir').'logs/'.$config->get('app.log.file'));
116:
117: $this->request = new Request(array('method' => 'GET'));
118: }
119:
120: 121: 122: 123: 124: 125: 126:
127: public function debug($message)
128: {
129: if ($this->level == 'DEBUG' || in_array($this->classname, $this->debugClasses)) {
130: $dateTime = date('Y-m-d H:i:s');
131: $this->logProvider->writeLine(array($dateTime, 'DEBUG', $this->classname, $message,
132: $this->request->getUserAgent(), $this->request->getIP(), gethostname(), $this->request->getURI()));
133: }
134: }
135:
136: 137: 138: 139: 140: 141: 142:
143: public function info($message)
144: {
145: if ($this->level == 'DEBUG' || $this->level == 'INFO' || in_array($this->classname, $this->debugClasses)) {
146: $dateTime = date('Y-m-d H:i:s');
147: $this->logProvider->writeLine(array($dateTime, 'INFO', $this->classname, $message,
148: $this->request->getUserAgent(), $this->request->getIP(), gethostname(), $this->request->getURI()));
149: }
150: }
151:
152: 153: 154: 155: 156: 157: 158:
159: public function warn($message)
160: {
161: if ($this->level == 'DEBUG' || $this->level == 'INFO' || $this->level == 'WARN' || in_array($this->classname, $this->debugClasses)) {
162: $dateTime = date('Y-m-d H:i:s');
163: $this->logProvider->writeLine(array($dateTime, 'WARN', $this->classname, $message,
164: $this->request->getUserAgent(), $this->request->getIP(), gethostname(), $this->request->getURI()));
165: }
166: }
167:
168: 169: 170: 171: 172: 173: 174:
175: public function error($message)
176: {
177: if ($this->level == 'DEBUG' || $this->level == 'INFO' || $this->level == 'WARN' || $this->level == 'ERROR' ||
178: in_array($this->classname, $this->debugClasses)) {
179: $dateTime = date('Y-m-d H:i:s');
180: $line = array($dateTime, 'ERROR', $this->classname, $message, $this->request->getUserAgent(), $this->request->getIP(), gethostname(), $this->request->getURI());
181: $this->logProvider->writeLine($line);
182:
183: $this->notifyAdmin(print_r($line, true));
184: }
185: }
186:
187: 188: 189: 190: 191: 192: 193:
194: public function fatal($message)
195: {
196: if ($this->level == 'DEBUG' || $this->level == 'INFO' || $this->level == 'WARN' || $this->level == 'ERROR' ||
197: $this->level == 'FATAL' || in_array($this->classname, $this->debugClasses)) {
198: $dateTime = date('Y-m-d H:i:s');
199: $line = array($dateTime, 'FATAL', $this->classname, $message, $this->request->getUserAgent(), $this->request->getIP(), gethostname(), $this->request->getURI());
200: $this->logProvider->writeLine($line);
201:
202: $this->notifyAdmin(print_r($line, true));
203: }
204: }
205:
206: 207: 208: 209: 210: 211: 212:
213: public function sql($message)
214: {
215: if ($this->level == 'SQL') {
216: $dateTime = date('Y-m-d H:i:s');
217: $line = array($dateTime, 'SQL', $this->classname, $message, $this->request->getUserAgent(), $this->request->getIP(), gethostname(), $this->request->getURI());
218: $this->logProvider->writeLine($line);
219: }
220: }
221:
222: 223: 224: 225: 226: 227: 228:
229: public function action($message)
230: {
231: $config = ConfigProvider::getInstance();
232: $sessionProvider = $config->get('session.provider.name');
233: $session = SessionProviderFactory::getInstance($sessionProvider);
234:
235: if ($session->get('currentUser') != null) {
236: $action = new ActionLog();
237: $action->set('client', $this->request->getUserAgent());
238: $action->set('IP', $this->request->getIP());
239: $action->set('message', $message);
240: $action->save();
241: }
242: }
243:
244: 245: 246: 247: 248: 249: 250: 251: 252:
253: public function notifyAdmin($message)
254: {
255: $config = ConfigProvider::getInstance();
256:
257:
258: if ($config->get('app.log.error.mail.address') != '') {
259: $body = "The following error has occured:\n\n";
260:
261: $body .= 'Class:-> '.$this->classname."\n\n";
262: $body .= 'Message:-> '.$message."\n\n";
263: $body .= 'Server:-> '.gethostname()."\n\n";
264:
265: $body .= "\n\nKind regards,\n\nAdministrator\n--\n".$config->get('app.url');
266:
267: $mailer = EmailProviderFactory::getInstance('Alpha\Util\Email\EmailProviderPHP');
268: $mailer->send($config->get('app.log.error.mail.address'), $config->get('email.reply.to'), 'Error in class '.$this->classname.' on site '.$config->get('app.title'), $body);
269: }
270: }
271:
272: 273: 274: 275: 276: 277: 278:
279: public function setLogProviderFile($filepath)
280: {
281: $config = ConfigProvider::getInstance();
282:
283: $this->logProvider = new LogProviderFile($filepath);
284: $this->logProvider->setMaxSize($config->get('app.log.file.max.size'));
285: }
286: }
287: