      1: <?php
      2:
      3: /**
      4:  * Log class used for debug and exception logging
      5:  *
      6:  * @package alpha::util
      7:  * @since 1.0
      8:  * @author John Collins <dev@alphaframework.org>
      9:  * @version $Id: Logger.inc 1693 2013-12-09 23:33:24Z alphadevx $
     10:  * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
     11:  * @copyright Copyright (c) 2012, John Collins (founder of Alpha Framework).
     12:  * All rights reserved.
     13:  *
     14:  * <pre>
     15:  * Redistribution and use in source and binary forms, with or
     16:  * without modification, are permitted provided that the
     17:  * following conditions are met:
     18:  *
     19:  * * Redistributions of source code must retain the above
     20:  *   copyright notice, this list of conditions and the
     21:  *   following disclaimer.
     22:  * * Redistributions in binary form must reproduce the above
     23:  *   copyright notice, this list of conditions and the
     24:  *   following disclaimer in the documentation and/or other
     25:  *   materials provided with the distribution.
     26:  * * Neither the name of the Alpha Framework nor the names
     27:  *   of its contributors may be used to endorse or promote
     28:  *   products derived from this software without specific
     29:  *   prior written permission.
     30:  *
     31:  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
     32:  * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
     33:  * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
     34:  * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
     35:  * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
     36:  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
     37:  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     38:  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
     39:  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     40:  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     41:  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
     42:  * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
     43:  * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     44:  * </pre>
     45:  *
     46:  */
     47: class Logger {
     48:     /**
     49:      * The log file the log entries will be saved to
     50:      *
     51:      * @var LogFile
     52:      * @since 1.0
     53:      */
     54:     private $logfile;
     55:
     56:     /**
     57:      * The logging level applied accross the system.  Valid options are DEBUG, INFO, WARN, ERROR, FATAL, and SQL
     58:      *
     59:      * @var string
     60:      * @since 1.0
     61:      */
     62:     private $level;
     63:
     64:     /**
     65:      * The name of the class that this Logger is logging for
     66:      *
     67:      * @var string
     68:      * @since 1.0
     69:      */
     70:     private $classname;
     71:
     72:     /**
     73:      * An array of class names that will be logged at debug level, regardless of the global Logger::level setting
     74:      *
     75:      * @var array
     76:      * @since 1.0
     77:      */
     78:     private $debugClasses = array();
     79:
     80:     /**
     81:      * The constructor
     82:      *
     83:      * @param string $classname
     84:      * @since 1.0
     85:      */
     86:     public function __construct($classname) {
     87:         global $config;
     88:
     89:         $this->classname = $classname;
     90:         $this->level = $config->get('app.log.trace.level');
     91:         $this->debugClasses = explode(',', $config->get('app.log.trace.debug.classes'));
     92:         $this->logfile = new LogFile($config->get('app.file.store.dir').'logs/'.$config->get('app.log.file'));
     93:         $this->logfile->setMaxSize($config->get('app.log.file.max.size'));
     94:     }
     95:
     96:     /**
     97:      * Log a DEBUG message
     98:      *
     99:      * @param string $message
    100:      * @since 1.0
    101:      */
    102:     public function debug($message) {
    103:         if($this->level == 'DEBUG' || in_array($this->classname, $this->debugClasses)) {
    104:             $dateTime = date("Y-m-d H:i:s");
    105:             $this->logfile->writeLine(array($dateTime, 'DEBUG', $this->classname, $message,
    106:                 (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')));
    107:         }
    108:     }
    109:
    110:     /**
    111:      * Log an INFO message
    112:      *
    113:      * @param string $message
    114:      * @since 1.0
    115:      */
    116:     public function info($message) {
    117:         if($this->level == 'DEBUG' || $this->level == 'INFO' || in_array($this->classname, $this->debugClasses)) {
    118:             $dateTime = date("Y-m-d H:i:s");
    119:             $this->logfile->writeLine(array($dateTime, 'INFO', $this->classname, $message,
    120:                 (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')));
    121:         }
    122:     }
    123:
    124:     /**
    125:      * Log a WARN message
    126:      *
    127:      * @param string $message
    128:      * @since 1.0
    129:      */
    130:     public function warn($message) {
    131:         if($this->level == 'DEBUG' || $this->level == 'INFO' || $this->level == 'WARN' || in_array($this->classname, $this->debugClasses)) {
    132:             $dateTime = date("Y-m-d H:i:s");
    133:             $this->logfile->writeLine(array($dateTime, 'WARN', $this->classname, $message,
    134:                 (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')));
    135:         }
    136:     }
    137:
    138:     /**
    139:      * Log an ERROR message
    140:      *
    141:      * @param string $message
    142:      * @since 1.0
    143:      */
    144:     public function error($message) {
    145:         if($this->level == 'DEBUG' || $this->level == 'INFO' || $this->level == 'WARN' || $this->level == 'ERROR' ||
    146:             in_array($this->classname, $this->debugClasses)) {
    147:             $dateTime = date("Y-m-d H:i:s");
    148:             $line = array($dateTime, 'ERROR', $this->classname, $message, (isset($_SERVER['HTTP_USER_AGENT']) ?
    149:                 $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''));
    150:             $this->logfile->writeLine($line);
    151:
    152:             $this->notifyAdmin(print_r($line, true));
    153:         }
    154:     }
    155:
    156:     /**
    157:      * Log a FATAL message
    158:      *
    159:      * @param string $message
    160:      * @since 1.0
    161:      */
    162:     public function fatal($message) {
    163:         if($this->level == 'DEBUG' || $this->level == 'INFO' || $this->level == 'WARN' || $this->level == 'ERROR' ||
    164:             $this->level == 'FATAL' || in_array($this->classname, $this->debugClasses)) {
    165:             $dateTime = date("Y-m-d H:i:s");
    166:             $line = array($dateTime, 'FATAL', $this->classname, $message, (isset($_SERVER['HTTP_USER_AGENT']) ?
    167:                 $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''));
    168:             $this->logfile->writeLine($line);
    169:
    170:             $this->notifyAdmin(print_r($line, true));
    171:         }
    172:     }
    173:
    174:     /**
    175:      * Log a SQL queries
    176:      *
    177:      * @param string $message
    178:      * @since 1.1
    179:      */
    180:     public function sql($message) {
    181:         if($this->level == 'SQL') {
    182:             $dateTime = date("Y-m-d H:i:s");
    183:             $this->logfile->writeLine(array($dateTime, 'SQL', $this->classname, $message, (isset($_SERVER['HTTP_USER_AGENT']) ?
    184:                 $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')));
    185:         }
    186:     }
    187:
    188:     /**
    189:      * Log an action carried out by a person to the ActionLog table
    190:      *
    191:      * @param string $message
    192:      * @since 1.1
    193:      */
    194:     public function action($message) {
    195:
    196:         if(isset($_SESSION['currentUser'])) {
    197:             $action = new ActionLogObject();
    198:             $action->set('client', (isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : ''));
    199:             $action->set('IP', (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : ''));
    200:             $action->set('message', $message);
    201:             $action->save();
    202:         }
    203:     }
    204:
    205:     /**
    206:      * Notify the sys admin via email when a serious error occurs
    207:      *
    208:      * @param string $message
    209:      * @since 1.0
    210:      */
    211:     public function notifyAdmin($message) {
    212:         global $config;
    213:
    214:         // just making sure an email address has been set in the .ini file
    215:         if($config->get('app.log.error.mail.address') != '') {
    216:             $body = "The following error has occured:\n\n";
    217:
    218:             $body .= "Class:-> ".$this->classname."\n\n";
    219:             $body .= "Message:-> ".$message."\n\n";
    220:
    221:             $body .= "\n\nKind regards,\n\nAdministrator\n--\n".$config->get('app.url');
    222:
    223:             mb_send_mail($config->get('app.log.error.mail.address'), "Error in class ".$this->classname." on site ".$config->get('app.title'), $body,
    224:                 "From: ".$config->get('email.reply.to')."\r\nReply-To: ".$config->get('email.reply.to')."\r\nX-Mailer: PHP/" . phpversion());
    225:         }
    226:     }
    227:
    228:     /**
    229:      * Allows you to set the log file path to one other than the main application log.
    230:      *
    231:      * @param string $filepath
    232:      * @since 1.0
    233:      */
    234:     public function setLogFile($filepath) {
    235:         global $config;
    236:
    237:         $this->logfile = new LogFile($filepath);
    238:         $this->logfile->setMaxSize($config->get('app.log.file.max.size'));
    239:     }
    240: }
    241:
    242: ?>