log.php 另,有一个php函数 error_log("$sql \n", 3, "/data/web/hr_sas/errors.log");
<?php
//以下为日志
interface ILogHandler
{
public function write($msg);
}
class CLogFileHandler implements ILogHandler
{
private $handle = null;
public function __construct($file = '')
{
$this->handle = fopen($file,'a');
}
public function write($msg)
{
fwrite($this->handle, $msg, 4096);
}
public function __destruct()
{
fclose($this->handle);
}
}
class Log
{
private $handler = null;
private $level = 15;
private static $instance = null;
private function __construct(){}
private function __clone(){}
public static function Init($handler = null,$level = 15)
{
if(!self::$instance instanceof self)
{
self::$instance = new self();
self::$instance->__setHandle($handler);
self::$instance->__setLevel($level);
}
return self::$instance;
}
private function __setHandle($handler){
$this->handler = $handler;
}
private function __setLevel($level)
{
$this->level = $level;
}
public static function DEBUG($msg)
{
self::$instance->write(1, $msg);
}
public static function WARN($msg)
{
self::$instance->write(4, $msg);
}
public static function ERROR($msg)
{
$debugInfo = debug_backtrace();
$stack = "[";
foreach($debugInfo as $key => $val){
if(array_key_exists("file", $val)){
$stack .= ",file:" . $val["file"];
}
if(array_key_exists("line", $val)){
$stack .= ",line:" . $val["line"];
}
if(array_key_exists("function", $val)){
$stack .= ",function:" . $val["function"];
}
}
$stack .= "]";
self::$instance->write(8, $stack . $msg);
}
public static function INFO($msg)
{
self::$instance->write(2, $msg);
}
private function getLevelStr($level)
{
switch ($level)
{
case 1:
return 'debug';
break;
case 2:
return 'info';
break;
case 4:
return 'warn';
break;
case 8:
return 'error';
break;
default:
}
}
protected function write($level,$msg)
{
if(($level & $this->level) == $level )
{
$msg = '['.date('Y-m-d H:i:s').']['.$this->getLevelStr($level).'] '.$msg."\n";
$this->handler->write($msg);
}
}
}
使用
include_once "/data/web/fenghui/Application/Home/Controller/log.php";
$logHandler= new \CLogFileHandler("/data/web/fenghui/Application/Home/Controller/wxlogs/".date('Y-m-d').'.log');
$log = new \Log();
$log->Init($logHandler, 15);
$log->DEBUG("query:" . "begin \n"); //写入日志文件
if (strtolower($postObj->Event == 'subscribe')) {
//未关注,如果是扫描带参数的二维码进来的
$eventkey = $postObj->EventKey;
$key = explode('_',$eventkey);
$log->DEBUG("query:" . "key-> $key \n");//写入日志文件
}
例二
/**
* [log_into_txt 写入日志到txt文件]
* @param [type] $file [文件路径]
* @param [type] $data [写入的数据]
* @param [type] $type [head写入头端,foot写入尾端,all全部]
* @return [type] [description]
*/
function log_into_txt($file,$str,$type=''){
//文件地址
$fileName=substr($file,0,-(strlen($file)-strrpos($file,'/')));
if(!is_dir($fileName)){
mkdir($fileName, 0777,true);
chmod($fileName, 0777);
}
$date=date('Y-m-d H:i:s');
$handle=fopen($file,"a+");
flock($handle, LOCK_EX);
if($type == 'head' || $type == 'all')
fwrite($handle,"---------------------------------------------------------------------------------\r\n");
fwrite($handle,"$date\t{$str}\r\n");
if($type == 'foot' || $type == 'all')
fwrite($handle,"---------------------------------------------------------------------------------\r\n");
flock($handle, LOCK_UN);
fclose($handle);
}
使用
log_into_txt($this->alipy_log,"【接收到微信的notifyurl通知】:\n",'head');