php中自定义日志处理类(类似于java中的log4j)

<?php


/**
 * @(#)logger.php
 * PHP version 5.2.3
 *
 *
 * Copyright 2007 SoftRoad. All Rights Reserved.
 *
 *====================================================================
 * 変更履歴
 *
 * 新規作成                            2007 年5月17日   蒋彪
 */
class Logger {

 var $g_log_level = 0;
 var $g_log_file = "/tmp/php.log";

 var $g_log_levels = array (
  NONE => 0,
  ERROR => 1,
  INFO => 2,
  DEBUG => 3
 );

 function __construct($logLevel, $logfile) {
  
  $ext = explode(".", $logfile);
  $this->g_log_file = $ext[0].".".date("Y-m-d")."." .$ext[1];
  $this->g_log_level = strtoupper($logLevel);
 }

 function info($message) {

  $level = INFO;
  if ($this->g_log_levels[$this->g_log_level] >= $this->g_log_levels[$level]) {
   //$message=date("D M j G:i:s T Y").$message;
   $message = date("Y-m-d H:i:s") . "  INFO [" . $_SERVER['REQUEST_URI'] . "]   " . $message;
   $this->write_log($message);
  }
 }
 function error($message) {

  $level = ERROR;
  if ($this->g_log_levels[$this->g_log_level] >= $this->g_log_levels[$level]) {
   $message = date("Y-m-d H:i:s") . "  ERROR [" . $_SERVER['REQUEST_URI'] . "]   " . $message;
   $this->write_log($message);
  }
 }
 function debug($message) {

  $level = DEBUG;
  if ($this->g_log_levels[$this->g_log_level] >= $this->g_log_levels[$level]) {
   $message = date("Y-m-d H:i:s") . "  DEBUG [" . $_SERVER['REQUEST_URI'] . "]   " . $message;
   $this->write_log($message);
  }
 }
 function log_reset() {
  @ unlink($this->g_log_file);
 }

 function write_log($message) {

  if ((strcmp($this->g_log_file, "") == 0) || (strcmp($this->g_log_file, "logfile") == 0)) {
   echo $message;
  } else {
   $fp = @fopen($this->g_log_file, "a+");
//   if (flock($fp, LOCK_EX)) {
    @fwrite($fp, "$message/n");
//    @flock($fp, LOCK_UN);
//   } else {
//    echo "Couldn't lock the file !";
//   }
   @fclose($fp);
  }
 }
 function __destruct() {
  //print("bye bye");
 }
 
}
?>
 

你可能感兴趣的:(php中自定义日志处理类(类似于java中的log4j))