php+自定义日志,php中自定义日志处理类(类似于java中的log4j) | 学步园

/**

* @(#)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");

}

}

?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值