Alpha Framework alpha--util
[ class tree: alpha--util ] [ index: alpha--util ] [ all elements ]

Source for file Logger.inc

Documentation is available at Logger.inc

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

Documentation generated on Thu, 17 Mar 2011 16:44:33 +0000 by phpDocumentor 1.4.3