如何在服务器插入日志

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

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()));

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 7,285评论 0 17
  • Spring Cloud为开发人员提供了快速构建分布式系统中一些常见模式的工具(例如配置管理,服务发现,断路器,智...
    卡卡罗2017阅读 136,457评论 19 139
  • <?php /** * 常用函数库 * */ class Core_Fun { /** * 对变量进行反...
    寻梦xunm阅读 610评论 0 0
  • NAME dnsmasq - A lightweight DHCP and caching DNS server....
    ximitc阅读 2,983评论 0 0
  • 1、感恩父母给予我生命。 2、感恩祖辈将我养育。 3、感恩母亲、祖母为家的巨大付出。 4、感恩亲友们对我家的照顾。...
    朱晓军阅读 248评论 0 1

友情链接更多精彩内容