zoukankan      html  css  js  c++  java
  • 写日志(log)

    以下为我自己写的一个写日志的类,比较简洁。

    <?php
    class Log
    {
        /**
         * @Purpose      :  写日志
         * @Method Name  :  writeLog()
         * @parameter     :  string $explain          错误说明
         *                  string $error_location    错误位置
         *                  string $dir               保存日志的文件夹名
         * @return         :  (无)
         */
        function writeLog($explain,$error_location,$dir){
            if (empty($dir)) return false;
            $error_msg = "";
            if(strlen($explain) != 0){
                $error_msg .= '['.date("Y-m-d H:i:s",time()).' error_explain: ] '. $explain;
            }
            if(strlen($error_location) != 0){
                $error_msg .= ' [ error_location: ] '. $error_location;
            }
            if(! is_dir($dir)){
                mkdir($dir,0777);
                system("chown root.root {$dir} -R;chmod 777 {$dir} -R");    // 修改目录所有者,所属组和权限
            }
            $file_name = date('Y-m-d',time()).'.txt';
            $file_name = $dir.$file_name;
            if(! $file_name == false){
                system("chown root.root {$file_name} -R; chmod 777 {$file_name} -R");
                $handle1 = fopen($file_name,'w');
                fwrite($handle1,"//log file , please do not modify me! 
    ");
                fwrite($handle1,'//Created on '.date('M j, Y, G:i',time())."
    ");    // 类似于这种格式 :Created on Jun 26,2017, 11:22
                fclose($handle1);
            }
            if(is_file($file_name)){
                $handle2 = fopen($file_name,'a');
                fwrite($handle2,"$error_msg 
    ");
                fclose($handle2);
            }
            /*
            最后打印出来的日志类似于这样:
            //log file , please do not modify me!
            //Created on Jun 26, 2017, 11:34
            [2017-06-26 11:34:29 error_explain: ] test_explain [ error_location: ] the error at E:www	estindex.php line 55
            */
         }
    
        /**
         * @Purpose       :    获取错误所在位置
         * @Method  Name  :    getErrorLine()
         * @Parameters    :    string  $line    行号
         * @Return array  :    $error_line      错误所在位置
         */
        public function getErrorLine($line)
        {
            $error_line = __FILE__ . ' line ' . $line ;
            return ' '.$error_line;
        }
    }
    $Log = new Log();
    $Log -> writeLog('test_explain','the error at' . $Log -> getErrorLine(__LINE__),'E:/www/test/log/');

    下面这个是一个单独的方法:

        /**
         * @purpose :   写日志
         * @param   :   string  $data   :   写入的数据
         *          :   string  $logDir :   日志目录
         *          :   string  $file   :   文件名前缀
         * @Author  :   daicr
         * @Time    :   2018-11-20
         */
        public function writeLog($data,$logDir='/tmp/', $fileName='') {
            $write_line    = "";
            $now    = date('Y-m-d H:i:s',time());
            if(strlen($data)>0) {
                $write_line    .= "[" . date('Y-m-d H:i:s',time()) . "]" . $data;
            }
            $dir = $logDir;
            if(!is_dir($dir)) {
                mkdir($dir, 0777);
            }
            system("chown justswitch.justswitch {$dir} -R; chmod 777 {$dir} -R;");
    
            $fileName = $fileName.date('Y-m',time());
            $fileName = $dir.$fileName.".txt";
            if (false==file_exists($fileName)){
                if($fp = fopen("$fileName", 'w')) {
                    system("chown justswitch.justswitch -R $dir;chmod 777 $dir -R;");
                    fwrite($fp, "
    //JUST-CALL! log file, DO NOT modify me!
    ".
                        "//Created on ".date("M j, Y, G:i")."
    ");
                    fclose($fp);
                }
            }
            if($fp = fopen("$fileName",'a')) {
                fwrite($fp,"$write_line
    ");
                fclose($fp);
            }
    
            system("chown justswitch.justswitch {$fileName};chmod 777 {$fileName};");
        }

     

    system($commond, $return_var)

    commond   : 要执行的命令

    return_var  : 外部命令执行后要返回的状态

    成功则返回命令输出的最后一行,失败则返回 false

    本文为原创作品,如有转载请注明出处http://www.cnblogs.com/chrdai/p/7082146.html

  • 相关阅读:
    PS封装ES流
    win7无法删除文件夹,提示“找不到该项目”
    声明
    ZR#1005
    ZR#1004
    ZR#1009
    ZR#1008
    ZR#1015
    ZR#1012
    ZR#985
  • 原文地址:https://www.cnblogs.com/chrdai/p/7082146.html
Copyright © 2011-2022 走看看