2010-09-24 84 views
2

所以我想找到允许我的记录器类访问脚本的任何部分或另一个类/函数/等等的最佳方法...我会怎么做这个?我如何使其成为全球性的。记录器对象 - 变量范围 - 访问类/函数/等

我可以做这样的事情:

Logger::info('Add message like this?'); 

调用脚本:calling.php

require_once('Logger.class.php'); // Just adding the class initializes the Logger Object 
require_once('Another.class.php'); 
require_once('Functions.php'); 

$logEntry->info("Log info"); 
$logEntry->error("Log error"); 
$logEntry->warning("Log warning"); 
$logEntry->notice("Log notice"); 

$logEntry->enableDebug(); // prints debug to log 
$logEntry->debug("Log debug enabled"); 
$logEntry->disableDebug(); 
$logEntry->debug("Log debug disabled"); // will not print to log 

$another_obj = new Another(); // want the Logger to have access inside this class 
More(); // want the Logger to have access inside this function 

Another.class.php

class Another { 
    private $var; 

    // I want to add the Logger here 
    $logEntry->info("Another Log"); 

    // More code here it's just an example ... 
} 

的functions.php

function More() { 
    // I want to add the Logger here 
    $logEntry->info("More Log"); 
} 

这里是Logger.class.php脚本

<?php 
//Define Constants 
define("LOG_FILE_DIRECTORY", "/var/www/logs"); 
ini_set("memory_limit","128M"); // Logger class is taking up memory 

class Logger { 
    private $log_file_directory = LOG_FILE_DIRECTORY; 
    private $first_run;   // Flag to add line break at the beginning of script execution 
    private $calling_script; // Base name of the calling script 
    private $log_file;   // log file path and name 
    private $log_entry;   // information to be logged 
    private $log_level;   // Log severity levels: error, warning, notice, debug, info 
    private $fh;    // File handle 
    private $file_name;   // File path and name 
    private $file_parts;  // Array of $file_name 
    private $script_name;  // Script Name 
    private $script_parts;  // Array of $script_name 
    private $line_number_arr; // Line number of where the logging event occurred 
    private $debug_flag;  // Set to true if you want to log your debug logger 

    function __construct() { 
     $this->first_run  = true; 
     $this->debug_flag  = false; 
     $this->calling_script = '';  
     $this->log_file   = ''; 
     $this->log_entry  = ''; 
     $this->log_level  = ''; 
     $this->fh    = ''; 
     $this->file_name  = ''; 
     $this->file_parts  = ''; 
     $this->script_name  = ''; 
     $this->script_parts  = ''; 
     $this->line_number_arr = ''; 
    } 

    /** 
    * @enableDebug 
    */ 
    public function enableDebug() { 
     $this->debug_flag = true; 
    } 

    /** 
    * @disbaleDebug 
    */ 
    public function disableDebug() { 
     $this->debug_flag = false; 
    } 

    /** 
    * @info 
    */ 
    public function info($message) { 
     $this->log_level = 'info'; 
     $this->line_number_arr = debug_backtrace(); 
     $this->addEntry($message); 
    } 

    /** 
    * @error 
    */ 
    public function error($message) { 
     $this->log_level = 'error'; 
     $this->line_number_arr = debug_backtrace(); 
     $this->addEntry($message); 
    } 

    /** 
    * @warning 
    */ 
    public function warning($message) { 
     $this->log_level = 'warning'; 
     $this->line_number_arr = debug_backtrace(); 
     $this->addEntry($message); 
    } 

    /** 
    * @notice 
    */ 
    public function notice($message) { 
     $this->log_level = 'notice'; 
     $this->line_number_arr = debug_backtrace(); 
     $this->addEntry($message); 
    } 

    /** 
    * @debug 
    * must add the below to the script you wish to debug 
    * define("DEBUG", true); // true enables, false disables 
    */ 
    public function debug($message) { 
     if($this->debug_flag) { 
      $this->log_level = 'debug'; 
      $this->line_number_arr = debug_backtrace(); 
      $this->addEntry($message); 
     }  
    } 

    private function addEntry($message) { 
     $this->calling_script = $this->getScriptBaseName(); 
     $this->log_file = $this->log_file_directory."/".$this->calling_script.".log"; 
     $this->fh = fopen($this->log_file, 'a') or die("Can't open log file: ".$this->log_file); 

     if($this->first_run) { 
      $this->log_entry = "\n[" . date("Y-m-d H:i:s", mktime()) . "][line:".$this->line_number_arr[0]['line']."|".$this->log_level."]:\t".$message."\n"; 
     } else { 
      $this->log_entry = "[" . date("Y-m-d H:i:s", mktime()) . "][line:".$this->line_number_arr[0]['line']."|".$this->log_level."]:\t".$message."\n"; 
     } 
     fwrite($this->fh, $this->log_entry); 
     fclose($this->fh); 

     $this->first_run = false; 
    } 

    /** 
    * return the base name of the calling script 
    */ 
    private function getScriptBaseName() { 
     $this->file_name = $_SERVER["SCRIPT_NAME"]; 
     $this->file_parts = explode('/', $this->file_name); 
     $this->script_name = $this->file_parts[count($this->file_parts) - 1]; 
     $this->script_parts = explode('.', $this->script_name); 

     // If file doesn't exists don't add line break 
     if(!file_exists($this->script_parts[0].".log")) { 
      $this->first_run = false; 
     } 
     return $this->script_parts[0]; 
    } 
} 

// Start log instance 
$logEntry = new Logger(); 

?> 

回答

3

您可以实现它作为一类完全静态的功能,例如:

class Logger { 
    protected $logfile = null; 

    public static load() { 
    self::$logfile = fopen('error.log', 'a'); 
    } 

    public static info($msg) { 
    if(self::$logfile == null) 
     self::load(); 

    fwrite(self::$logfile, $msg); 
    } 
} 

,然后用Logger::info("My message..");使用它。另一种常见的做法是使用单例类,这样你就只能创建一个单独的“记录器”对象并使用它来检索它。 Logger::getInstance()->logInfo("My message");

在你的情况下(因为你已经将Logger作为一个普通的类来实现),我将使__construct为private,并将该类实现为一个单例。无法使$ logEntry全局可用。您的代码将变为:

<?php 
//Define Constants 
define("LOG_FILE_DIRECTORY", "/var/www/logs"); 
ini_set("memory_limit","128M"); // Logger class is taking up memory 

class Logger { 
    private $log_file_directory = LOG_FILE_DIRECTORY; 
    private $first_run;   // Flag to add line break at the beginning of script execution 
    private $calling_script; // Base name of the calling script 
    private $log_file;   // log file path and name 
    private $log_entry;   // information to be logged 
    private $log_level;   // Log severity levels: error, warning, notice, debug, info 
    private $fh;    // File handle 
    private $file_name;   // File path and name 
    private $file_parts;  // Array of $file_name 
    private $script_name;  // Script Name 
    private $script_parts;  // Array of $script_name 
    private $line_number_arr; // Line number of where the logging event occurred 
    private $debug_flag;  // Set to true if you want to log your debug logger 
    private static $instance = null; 

    private function __construct() { 
     $this->first_run  = true; 
     $this->debug_flag  = false; 
     $this->calling_script = '';  
     $this->log_file   = ''; 
     $this->log_entry  = ''; 
     $this->log_level  = ''; 
     $this->fh    = ''; 
     $this->file_name  = ''; 
     $this->file_parts  = ''; 
     $this->script_name  = ''; 
     $this->script_parts  = ''; 
     $this->line_number_arr = ''; 
    } 

    public static function getInstance() { 
     if (!isset(self::$instance)) { 
      $c = __CLASS__; 
      self::$instance = new $c; 
     } 

     return self::$instance; 
    } 

    /** 
    * @enableDebug 
    */ 
    public function enableDebug() { 
     $this->debug_flag = true; 
    } 

    /** 
    * @disbaleDebug 
    */ 
    public function disableDebug() { 
     $this->debug_flag = false; 
    } 

    /** 
    * @info 
    */ 
    public function info($message) { 
     $this->log_level = 'info'; 
     $this->line_number_arr = debug_backtrace(); 
     $this->addEntry($message); 
    } 

    /** 
    * @error 
    */ 
    public function error($message) { 
     $this->log_level = 'error'; 
     $this->line_number_arr = debug_backtrace(); 
     $this->addEntry($message); 
    } 

    /** 
    * @warning 
    */ 
    public function warning($message) { 
     $this->log_level = 'warning'; 
     $this->line_number_arr = debug_backtrace(); 
     $this->addEntry($message); 
    } 

    /** 
    * @notice 
    */ 
    public function notice($message) { 
     $this->log_level = 'notice'; 
     $this->line_number_arr = debug_backtrace(); 
     $this->addEntry($message); 
    } 

    /** 
    * @debug 
    * must add the below to the script you wish to debug 
    * define("DEBUG", true); // true enables, false disables 
    */ 
    public function debug($message) { 
     if($this->debug_flag) { 
      $this->log_level = 'debug'; 
      $this->line_number_arr = debug_backtrace(); 
      $this->addEntry($message); 
     }  
    } 

    private function addEntry($message) { 
     $this->calling_script = $this->getScriptBaseName(); 
     $this->log_file = $this->log_file_directory."/".$this->calling_script.".log"; 
     $this->fh = fopen($this->log_file, 'a') or die("Can't open log file: ".$this->log_file); 

     if($this->first_run) { 
      $this->log_entry = "\n[" . date("Y-m-d H:i:s", mktime()) . "][line:".$this->line_number_arr[0]['line']."|".$this->log_level."]:\t".$message."\n"; 
     } else { 
      $this->log_entry = "[" . date("Y-m-d H:i:s", mktime()) . "][line:".$this->line_number_arr[0]['line']."|".$this->log_level."]:\t".$message."\n"; 
     } 
     fwrite($this->fh, $this->log_entry); 
     fclose($this->fh); 

     $this->first_run = false; 
    } 

    /** 
    * return the base name of the calling script 
    */ 
    private function getScriptBaseName() { 
     $this->file_name = $_SERVER["SCRIPT_NAME"]; 
     $this->file_parts = explode('/', $this->file_name); 
     $this->script_name = $this->file_parts[count($this->file_parts) - 1]; 
     $this->script_parts = explode('.', $this->script_name); 

     // If file doesn't exists don't add line break 
     if(!file_exists($this->script_parts[0].".log")) { 
      $this->first_run = false; 
     } 
     return $this->script_parts[0]; 
    } 
} 

?> 

你会然后用你的类全球使用Logger::getInstance()->info($msg);

+0

哇那伟大工程,谢谢! – 2010-09-24 20:43:00