我爱编程

如何在服务器插入日志

2017-08-07  本文已影响0人  Omit03

永远坚信一句话 代码是最好文档   不辜负关注我的小伙伴  直接上源码

namespace common\log;

class newLog{

private $handle;

private $time;

//avr log类型

public function __construct($filename=''){

//global $log_config;  缓存文件路径

$this->time = $this->microtime_float();

$todaystr=date('Ymd');

//$logpath="/www/bsc/bwtest/buylog/";

if(empty($filename)){

$logpath=__DIR__."cache/";

$filename="buylog_".$todaystr.".txt";

}else{

$logpath=__DIR__."cache/".$filename."/";

$filename.=$todaystr.".txt";

}

if (!file_exists($logpath)){

mkdir ($logpath,0777,true);

}

//$handle = fopen($logpath."buylog_".$todaystr.".txt", "a+");

$handle = fopen($logpath.$filename, "a+");

$this->handle=$handle;

$this->write_log("-----记录开始----");

}

public function __destruct(){

$use_time = ($this-> microtime_float())-($this->time);

$this->write_log("-----记录完成,所需时间".$use_time."-----");

fclose($this->handle);

}

public function reclog($msg){

$this->write_log($msg);

}

public function write_log($msg=''){

$text = $_SERVER["REMOTE_ADDR"]."::".date("Y-m-d H:i:s")." ".$msg."\r\n";

fwrite($this->handle,$text);

}

public function microtime_float(){

list($usec, $sec) = explode(" ", microtime());

return ((float)$usec + (float)$sec);

}

}



照顾一下  下面是 使用方法

use common\log\newLog;//引用

$newlog=new newLog("logbook"); //实例化

$newlog->reclog("用户userid : ".$userId);

$newlog->reclog("优购券交互时间 : ".date('Y-m-d H:i:s',time()));

上一篇 下一篇

猜你喜欢

热点阅读