zoukankan      html  css  js  c++  java
  • php error_log记录日志的使用方法--拿来即用,超简单

    如果本文对你有用,请爱心点个赞,提高排名,帮助更多的人。谢谢大家!❤

    如果解决不了,可以在文末进群交流。

    如果对你有帮助的话麻烦点个【推荐】~最好还可以follow一下我的GitHub~感谢观看!

    对于PHP开发者来 说,一旦某个产品投入使用,应该立即将 display_errors选项关闭,以免因为这些错误所透露的路径、数据库连接、数据表等信息而遭到黑客攻击。但是,任何一个产品在投入使用后,都难 免会有错误出现,那么如何记录一些对开发者有用的错误报告呢?我们可以在单独的文本文件中将错误报告作为日志记录。错误日志的记录,可以帮助开发人员或者 管理人员查看系统是否存在问题。 如果需要将程序中的错误报告写入错误日志中,只要在PHP的配置文件中,将配置指令log_errors开启即可。错误 报告默认就会记录到Web服务器的日志文件里,例如记录到Apache服务器的错误日志文件error.log中。当然也可以记录错误日志到指定的文件中 或发送给系统syslog。

    下面是直接封装OK的,直接调用即可。写入的信息样式如下:

    [2018-07-24 10:27:43][debug] begin notify3
    [2018-07-24 10:27:43][debug] call back:[]
    [2018-07-24 10:27:43][debug] error:can shu cuo wu
    [2018-07-24 14:11:47][debug] begin notify3
    [2018-07-24 14:11:47][debug] call back:[]
    [2018-07-24 14:11:47][debug] error:can shu cuo wu
    [2018-07-24 14:11:48][debug] begin notify3
    [2018-07-24 14:11:48][debug] call back:[]
    [2018-07-24 14:11:48][debug] error:can shu cuo wu
    [2018-07-24 14:11:48][debug] begin notify3
    [2018-07-24 14:11:48][debug] call back:[]
    [2018-07-24 14:11:48][debug] error:can shu cuo wu
    [2018-07-24 14:11:48][debug] begin notify3
    [2018-07-24 14:11:48][debug] call back:[]
    [2018-07-24 14:11:48][debug] error:can shu cuo wu
    [2018-07-24 14:11:48][debug] begin notify3
    [2018-07-24 14:11:48][debug] call back:[]
    [2018-07-24 14:11:48][debug] error:can shu cuo wu
    [2018-07-24 19:16:22][debug] error:post data is empty
    [2018-07-24 20:05:59][debug] error:post data is empty
    [2018-07-24 20:06:00][debug] error:post data is empty
    [2018-07-24 20:06:01][debug] error:post data is empty
    [2018-07-24 20:06:06][debug] error:post data is empty
    [2018-07-24 20:06:07][debug] error:post data is empty
    [2018-07-24 20:06:07][debug] error:post data is empty
    [2018-07-24 20:06:08][debug] error:post data is empty
    [2018-07-24 21:27:16][debug] error:post data is empty
    
    <?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."
    ";
    			$this->handler->write($msg);
    		}
    	}
    }
    

    调用方法:

    <?php
        $logDir = DISCUZ_ROOT."./source/plugin/vyuan/logs/";
        if(!is_dir($logDir)){
            mkdir($logDir, 0777,true);
        }else{
            chmod($logDir, 0777); 
        }
    
        //初始化日志
        $logHandler= new CLogFileHandler("/source/plugin/logs/".date("Y-m-d").".log");
        $log = Log::Init($logHandler, 15);
    
        if(empty($orderInfo)){
            Log::DEBUG('dingdan bu cun zai');
        }else{
            Log::DEBUG('dingdan cun zai');
        }

    不懂地方可留言,或者进微信群内交流。群二维码如果过期,请加我微信:mengyilingjian.

  • 相关阅读:
    LIBSVM
    tf-idf
    DIV+CSS例子
    网页背景设置
    获取JDBC中的ResultSet的记录的条数
    SQL 基本语句
    经典SQL语句大全
    JS(截取字符串,显示当前系统时间yyyy-MM-dd,从文本框得到的数值计算)
    JavaScript实现全排列
    Java发送邮件
  • 原文地址:https://www.cnblogs.com/mengyilingjian/p/11699856.html
Copyright © 2011-2022 走看看