      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 1550 2012-07-29 21:15:40Z 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:             $this->logfile->writeLine(array($dateTime, 'ERROR', $this->classname, $message, (isset($_SERVER['HTTP_USER_AGENT']) ?
    149:                 $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')));
    150:         }
    151:     }
    152:
    153:     /**
    154:      * Log a FATAL message
    155:      *
    156:      * @param string $message
    157:      * @since 1.0
    158:      */
    159:     public function fatal($message) {
    160:         if($this->level == 'DEBUG' || $this->level == 'INFO' || $this->level == 'WARN' || $this->level == 'ERROR' ||
    161:             $this->level == 'FATAL' || in_array($this->classname, $this->debugClasses)) {
    162:             $dateTime = date("Y-m-d H:i:s");
    163:             $this->logfile->writeLine(array($dateTime, 'FATAL', $this->classname, $message, (isset($_SERVER['HTTP_USER_AGENT']) ?
    164:                 $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')));
    165:         }
    166:     }
    167:
    168:     /**
    169:      * Log a SQL queries
    170:      *
    171:      * @param string $message
    172:      * @since 1.1
    173:      */
    174:     public function sql($message) {
    175:         if($this->level == 'SQL') {
    176:             $dateTime = date("Y-m-d H:i:s");
    177:             $this->logfile->writeLine(array($dateTime, 'SQL', $this->classname, $message, (isset($_SERVER['HTTP_USER_AGENT']) ?
    178:                 $_SERVER['HTTP_USER_AGENT'] : ''), (isset($_SERVER['REMOTE_ADDR']) ? $_SERVER['REMOTE_ADDR'] : '')));
    179:         }
    180:     }
    181:
    182:     /**
    183:      * Notify the sys admin via email when a serious error occurs
    184:      *
    185:      * @param string $message
    186:      * @since 1.0
    187:      */
    188:     public function notifyAdmin($message) {
    189:         global $config;
    190:
    191:         // just making sure an email address has been set in the .ini file
    192:         if($config->get('app.log.error.mail.address') != '') {
    193:             $body = "The following error has occured:\n\n";
    194:
    195:             $body .= "Class:-> ".$this->classname."\n\n";
    196:             $body .= "Message:-> ".$message."\n\n";
    197:
    198:             $body .= "\n\nKind regards,\n\nAdministrator\n--\n".$config->get('app.url');
    199:
    200:             mail($config->get('app.log.error.mail.address'), "Error in class ".$this->classname." on site .".$config->get('app.title'), $body,
    201:                 "From: ".$config->get('email.reply.to')."\r\nReply-To: ".$config->get('email.reply.to')."\r\nX-Mailer: PHP/" . phpversion());
    202:         }
    203:     }
    204:
    205:     /**
    206:      * Allows you to set the log file path to one other than the main application log.
    207:      *
    208:      * @param string $filepath
    209:      * @since 1.0
    210:      */
    211:     public function setLogFile($filepath) {
    212:         global $config;
    213:
    214:         $this->logfile = new LogFile($filepath);
    215:         $this->logfile->setMaxSize($config->get('app.log.file.max.size'));
    216:     }
    217: }
    218:
    219: ?>