A PHP log class for debuging

分享一个我自己用的在 WordPress 开发中用得到的一个调试日志类。

<?php
/**
 * @author: suifengtec coolwp.com
 * @date:   2013-02-03 09:55:55
 * @last Modified by:   suifengtec coolwp.com
 * @last Modified time: 2015-07-12 18:40:02
 */

if(function_exists(‘add_action‘)){
    defined(‘ABSPATH‘) or exit;
}

if(!class_exists(‘CoolWP_com_Log‘)){

    final class CoolWP_com_Log{

        private $dir = null;
        private $debug_file_name = null;
        private $f_path = null;

        public function __clone() {
            _doing_it_wrong( __FUNCTION__, ‘Cheatin’ huh?‘, ‘0.9.0‘ );
        }
        public function __wakeup() {
            _doing_it_wrong( __FUNCTION__, ‘Cheatin’ huh?‘, ‘0.9.0‘ );
        }

        public function __construct($main_file=‘‘){
            $main_file = (‘‘==$main_file)?__FILE__:$main_file;
            $this->dir = dirname($main_file).DIRECTORY_SEPARATOR.‘debug‘.DIRECTORY_SEPARATOR;
            $file_name = ‘debug_‘.md5($main_file).‘.log‘;
            $this->debug_file_name = (function_exists(‘apply_filters‘))?apply_filters(‘cwp_debug_log_file_name‘,$file_name).‘.log‘:$file_name.‘.log‘;
            $this->f_path = $this->dir.$this->debug_file_name;
            $this->check_log_file();

         }
        /**
         * adding log item
         * @param string $text : adding content
         */
        public function  add($text) {

            date_default_timezone_set(‘Asia/Shanghai‘);
            if(is_array($text)||is_obeject($text)){
                $text = json_encode($text);
            }
            $fp = fopen( $this->f_path,"a");
            flock($fp, LOCK_EX) ;
            fwrite($fp,"".date("Y-m-d H:i:s",time())."\n".$text."\n\n");
            flock($fp, LOCK_UN);
            fclose($fp);
            //return true;
            //
        }

        /**
         * checking the log file and path.
         * @return null
         */
        private function check_log_file(){

            $is_dir = is_dir($this->dir);
            $is_file = file_exists($this->f_path);
            if($is_dir && $is_file) return;
            if(!$is_dir||!$is_file){
                if(!$is_dir){
                    $md = mkdir($this->dir,0777,true);
                }
                if(!$is_file){
                    $fp = fopen( $this->f_path,"a");  
                    fclose($fp); 
                }
            }

        }
    }/*//CLASS*/
}
/*ALL DONE.*/
?>

以 WordPress 插件为例的用例:
在插件主文件的适当位置加入(假如上述代码放置在class-coolwp-debug-log.php文件中):

$this->is_debug = true;

if($this->is_debug){
    require_once( plugin_dir_path(__FILE__).‘classes/class-coolwp-debug-log.php‘);

    $this->Log = new CoolWP_com_Log();
}

如果在插件主文件中将__FILE__定义为常量 SOMEONE_THIS,那么,可以将SOMEONE_THIS 作为参数传给CoolWP_com_Log(),例如:

$this->Log = new CoolWP_com_Log(SOMEONE_THIS);

传不传参数的区别是日志文件的位置不同,如果不传参数,日志文件位于class-coolwp-debug-log.php所在目录下的debug目录下;如果传递了SOMEONE_THIS参数,那么,日志文件位于插件主目录下的debug目录下。日志文件的文件名称为debug_*******log。

日志条目默认采用北京时间。

时间: 2024-12-19 19:43:55

A PHP log class for debuging的相关文章

android &amp; Linux uevent机制

Linux uevent机制 Uevent是内核通知android有状态变化的一种方法,比如USB线插入.拔出,电池电量变化等等.其本质是内核发送(可以通过socket)一个字符串,应用层(android)接收并解释该字符串,获取相应信息. 一.Kernel侧: UEVENT的发起在Kernel端,主要是通过函数 int kobject_uevent_env(struct kobject *kobj, enum kobject_action action,char *envp_ext[]) 该函

一个简单实用的log类

自己写一些小代码的时候总是用fprintf来写log,感觉这样不太科学,还是写一个通用的简单带log level的log类,以后就拿来复用吧.这个类实现了非常简单的功能:如果指定了log文件path和name创建一个log文件,并将各种level的写入文件中,否则都打印到屏幕,格式如下: [DEBUG] : xxxxx [WARN] : xxxx [MSG] : xxxxx 代码: //--------------log.h---------------------- #ifndef __LOG

【SAS BASE】Debuging SAS Programs

SAS Program的错误主要有三类: Programing Logic Errors(How to identify and resolve); Syntax errors(How to recogize anc correct); Data errors(How to examine and resolve. 如何写一个高效的SAS Program: 尽可能写易读的代码:(每一句话一行.使用缩进排版方式.多使用注释) 测试Program的任意一个部分 用小数据集测试Program(例如用O

【转】console.log 用法

转自http://www.cnblogs.com/ctriphire/p/4116207.html 大家都有用过各种类型的浏览器,每种浏览器都有自己的特色,本人拙见,在我用过的浏览器当中,我是最喜欢Chrome的,因为它对于调试脚本及前端设计调试都有它比其它浏览器有过之而无不及的地方.可能大家对console.log会有一定的了解,心里难免会想调试的时候用alert不就行了,干嘛还要用console.log这么一长串的字符串来替代alert输出信息呢,下面我就介绍一些调试的入门技巧,让你爱上co

git日志--log

1. 查找改动某个文件所有的日志 git log --pretty=oneline somefile.java git log --oneline somefile.java git log --pretty=format:"%h %an,%cd : %s" somefile.java 2. 格式化输出,git log --pretty=format:" " 选项 说明 %H 提交对象(commit)的完整哈希字串 %h 提交对象的简短哈希字串 %T 树对象(tre

git log进阶

格式化log输出 oneline --oneline标记将每个commit压缩成一行. 默认情况下显示一个commit ID和commit描述的第一行. 输出如下: 0e25143 Merge branch 'feature' ad8621a Fix a bug in the feature 16b36c6 Add a new feature 23ad9ad Add the initial code base decorate 许多时候知道commit是和哪一个分支或tag关联的是非常有用的.

git查看单个文件log

  git log -p c.txt

游戏log的格式的优化

游戏log是非常关键的,对于追踪bug和查看一些信息,所以游戏里面是大量的log输出,sprintf的格式非常难用,而且对格式有较高的要求. 下面一种优化方案如下: // MVC.cpp : 定义控制台应用程序的入口点. // #include "stdafx.h" #include <iostream> #include <string> #include <sstream> using namespace std; class EndLogOut

使用方法拦截器MethodInterceptor和AOP统一处理log

对每个接口的请求记录log的方法有很多种,比如用filter.mvc interceptor.method interceptor等.如果需要记录请求消息的payload,前两种不适用.下面介绍第三种的实现方法. 第一步:引入包依赖 <span style="font-family: Arial, Helvetica, sans-serif; background-color: rgb(255, 255, 255);"></span><pre name=&