Advertisement
Guest User

KLogger.php

a guest
Sep 30th, 2012
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.21 KB | None | 0 0
  1. <?php
  2.  
  3. /* Finally, A light, permissions-checking logging class.
  4.  *
  5.  * Author    : Kenneth Katzgrau < katzgrau@gmail.com >
  6.  * Date    : July 26, 2008
  7.  * Comments    : Originally written for use with wpSearch
  8.  * Website    : http://codefury.net
  9.  * http://codefury.net/2008/07/klogger-a-simple-logging-class-for-php/
  10.  * Version    : 1.0
  11.  *
  12.  * Usage:
  13.  *        $log = new KLogger ( "log.txt" , KLogger::INFO );
  14.  *        $log->LogInfo("Returned a million search results");    //Prints to the log file
  15.  *        $log->LogFATAL("Oh dear.");                //Prints to the log file
  16.  *        $log->LogDebug("x = 5");                    //Prints nothing due to priority setting
  17.  */
  18.  
  19. class KLogger
  20. {
  21.     const DEBUG = 1; // Most Verbose
  22.     const INFO = 2; // ...
  23.     const WARN = 3; // ...
  24.     const ERROR = 4; // ...
  25.     const FATAL = 5; // Least Verbose
  26.     const OFF = 6; // Nothing at all.
  27.    
  28.     const LOG_OPEN = 1;
  29.     const OPEN_FAILED = 2;
  30.     const LOG_CLOSED = 3;
  31.    
  32.     /* Public members: Not so much of an example of encapsulation, but that's okay. */
  33.     public $Log_Status = KLogger::LOG_CLOSED;
  34.     public $DateFormat = "Y-m-d G:i:s";
  35.     public $MessageQueue;
  36.    
  37.     private $log_file;
  38.     private $priority = KLogger::INFO;
  39.    
  40.     private $file_handle;
  41.    
  42.     public function __construct($filepath, $priority)
  43.     {
  44.         if ($priority == KLogger::OFF)
  45.             return;
  46.        
  47.         $this->log_file     = $filepath;
  48.         $this->MessageQueue = array();
  49.         $this->priority     = $priority;
  50.        
  51.         if (file_exists($this->log_file)) {
  52.             if (!is_writable($this->log_file)) {
  53.                 $this->Log_Status     = KLogger::OPEN_FAILED;
  54.                 $this->MessageQueue[] = "The file exists, but could not be opened for writing. Check that appropriate permissions have been set.";
  55.                 return;
  56.             }
  57.         }
  58.        
  59.         if ($this->file_handle = fopen($this->log_file, "a")) {
  60.             $this->Log_Status     = KLogger::LOG_OPEN;
  61.             $this->MessageQueue[] = "The log file was opened successfully.";
  62.         } else {
  63.             $this->Log_Status     = KLogger::OPEN_FAILED;
  64.             $this->MessageQueue[] = "The file could not be opened. Check permissions.";
  65.         }
  66.        
  67.         return;
  68.     }
  69.    
  70.     public function __destruct()
  71.     {
  72.         if ($this->file_handle)
  73.             fclose($this->file_handle);
  74.     }
  75.    
  76.     public function LogInfo($line)
  77.     {
  78.         $this->Log($line, KLogger::INFO);
  79.     }
  80.    
  81.     public function LogDebug($line)
  82.     {
  83.         $this->Log($line, KLogger::DEBUG);
  84.     }
  85.    
  86.     public function LogWarn($line)
  87.     {
  88.         $this->Log($line, KLogger::WARN);
  89.     }
  90.    
  91.     public function LogError($line)
  92.     {
  93.         $this->Log($line, KLogger::ERROR);
  94.     }
  95.    
  96.     public function LogFatal($line)
  97.     {
  98.         $this->Log($line, KLogger::FATAL);
  99.     }
  100.    
  101.     public function Log($line, $priority)
  102.     {
  103.         if ($this->priority <= $priority) {
  104.             $status = $this->getTimeLine($priority);
  105.             $this->WriteFreeFormLine("$status $line \n");
  106.         }
  107.     }
  108.    
  109.     public function WriteFreeFormLine($line)
  110.     {
  111.         if ($this->Log_Status == KLogger::LOG_OPEN && $this->priority != KLogger::OFF) {
  112.             if (fwrite($this->file_handle, $line) === false) {
  113.                 $this->MessageQueue[] = "The file could not be written to. Check that appropriate permissions have been set.";
  114.             }
  115.         }
  116.     }
  117.    
  118.     private function getTimeLine($level)
  119.     {
  120.         $time = date($this->DateFormat);
  121.        
  122.         switch ($level) {
  123.             case KLogger::INFO:
  124.                 return "$time - INFO  -->";
  125.             case KLogger::WARN:
  126.                 return "$time - WARN  -->";
  127.             case KLogger::DEBUG:
  128.                 return "$time - DEBUG -->";
  129.             case KLogger::ERROR:
  130.                 return "$time - ERROR -->";
  131.             case KLogger::FATAL:
  132.                 return "$time - FATAL -->";
  133.             default:
  134.                 return "$time - LOG   -->";
  135.         }
  136.     }
  137.    
  138. }
  139.  
  140. $logger = New KLogger(APP_PATH . 'log/cspum.log', KLogger::DEBUG);
  141. $log =& $logger;
  142. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement