Overview

Namespaces

  • Alpha
    • Controller
      • Front
    • Exception
    • Model
      • Type
    • Task
    • Util
      • Backup
      • Cache
      • Code
        • Highlight
        • Metric
      • Config
      • Convertor
      • Email
      • Extension
      • Feed
      • File
      • Graph
      • Helper
      • Http
        • Filter
        • Session
      • Image
      • Logging
      • Search
      • Security
    • View
      • Renderer
        • Html
        • Json
      • Widget

Classes

  • KPI
  • Logger
  • LogProviderFactory
  • LogProviderFile

Interfaces

  • LogProviderInterface
  • Overview
  • Namespace
  • Class
  • Tree
  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:  * Log class used for debug and exception logging.
 13:  *
 14:  * @since 1.0
 15:  *
 16:  * @author John Collins <dev@alphaframework.org>
 17:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
 18:  * @copyright Copyright (c) 2015, John Collins (founder of Alpha Framework).
 19:  * All rights reserved.
 20:  *
 21:  * <pre>
 22:  * Redistribution and use in source and binary forms, with or
 23:  * without modification, are permitted provided that the
 24:  * following conditions are met:
 25:  *
 26:  * * Redistributions of source code must retain the above
 27:  *   copyright notice, this list of conditions and the
 28:  *   following disclaimer.
 29:  * * Redistributions in binary form must reproduce the above
 30:  *   copyright notice, this list of conditions and the
 31:  *   following disclaimer in the documentation and/or other
 32:  *   materials provided with the distribution.
 33:  * * Neither the name of the Alpha Framework nor the names
 34:  *   of its contributors may be used to endorse or promote
 35:  *   products derived from this software without specific
 36:  *   prior written permission.
 37:  *
 38:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
 39:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
 40:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 41:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
 42:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
 43:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 44:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 45:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 46:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 47:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 48:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
 49:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
 50:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 51:  * </pre>
 52:  */
 53: class Logger
 54: {
 55:     /**
 56:      * The log file the log entries will be saved to.
 57:      *
 58:      * @var Alpha\Util\Logging\LogProviderFile
 59:      *
 60:      * @since 1.0
 61:      */
 62:     private $logProvider;
 63: 
 64:     /**
 65:      * The logging level applied accross the system.  Valid options are DEBUG, INFO, WARN, ERROR, FATAL, and SQL.
 66:      *
 67:      * @var string
 68:      *
 69:      * @since 1.0
 70:      */
 71:     private $level;
 72: 
 73:     /**
 74:      * The name of the class that this Logger is logging for.
 75:      *
 76:      * @var string
 77:      *
 78:      * @since 1.0
 79:      */
 80:     private $classname;
 81: 
 82:     /**
 83:      * An array of class names that will be logged at debug level, regardless of the global Logger::level setting.
 84:      *
 85:      * @var array
 86:      *
 87:      * @since 1.0
 88:      */
 89:     private $debugClasses = array();
 90: 
 91:     /**
 92:      * A request object that will give us the IP, user-agent etc. of the client we are logging for.
 93:      *
 94:      * @var Alpha\Util\Http\Request
 95:      *
 96:      * @since 2.0
 97:      */
 98:     private $request;
 99: 
100:     /**
101:      * The constructor.
102:      *
103:      * @param string $classname
104:      *
105:      * @since 1.0
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')); // hard-coding to GET here is fine as we don't log HTTP method (yet).
118:     }
119: 
120:     /**
121:      * Log a DEBUG message.
122:      *
123:      * @param string $message
124:      *
125:      * @since 1.0
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:      * Log an INFO message.
138:      *
139:      * @param string $message
140:      *
141:      * @since 1.0
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:      * Log a WARN message.
154:      *
155:      * @param string $message
156:      *
157:      * @since 1.0
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:      * Log an ERROR message.
170:      *
171:      * @param string $message
172:      *
173:      * @since 1.0
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:      * Log a FATAL message.
189:      *
190:      * @param string $message
191:      *
192:      * @since 1.0
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:      * Log a SQL queries.
208:      *
209:      * @param string $message
210:      *
211:      * @since 1.1
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:      * Log an action carried out by a person to the ActionLog table.
224:      *
225:      * @param string $message
226:      *
227:      * @since 1.1
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:      * Notify the sys admin via email when a serious error occurs.
246:      *
247:      * @param string $message
248:      *
249:      * @since 1.0
250:      *
251:      * @throws Alpha\Exception\MailNotSentException
252:      */
253:     public function notifyAdmin($message)
254:     {
255:         $config = ConfigProvider::getInstance();
256: 
257:         // just making sure an email address has been set in the .ini file
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:      * Allows you to set the log file path to one other than the main application log.
274:      *
275:      * @param string $filepath
276:      *
277:      * @since 1.0
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: 
Alpha Framework 2.0.4 API Documentation API documentation generated by ApiGen 2.8.0