php中自訂Tlog類(類似於java中的log4j)

來源:互聯網
上載者:User

<?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");
 }
 
}
?>
 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.