dpr77335 2019-03-01 19:31
浏览 45
已采纳

PHP 7+记录类Magic Constants

I just wrote a little "logging" class and want to ask a question about the usage of this class, how i could make it easier to use.

For example:

$log = new Log();
$log->Error("You have an error!", __FILE__, __CLASS__, __FUNCTION__, __LINE__);

This is how i write errors to a log file at the moment, but it seems to complex?! Is there a way to get the "MAGIC CONSTANTS" inside the logging class from the "calling" php ?

Here is the class code (any other tips are welcome too):

    <?php
    class Log
    {   
        private $path;

        public function __construct()
        {
            $config = new Config();         // init. from autoloader
            $path = $config->app_log_dir;

            if (!is_dir($path) && !is_writable($path))
            {
                error_log('[ERROR] [Log::__Construct()] -> ' . $path . ' does not exist or is not writeable!',0);
                header("HTTP/1.0 500 Internal Server Error");
                exit();
            }

            $this->path = $path;
        }

        public function Error($message, $file, $class = '', $function = '', $line)
        {
            $array_data = array($message, $file, $class, $function, $line);
            $this->write('ERROR', $array_data);
        }

        public function TestError($message, $file = __FILE__, $class = __CLASS__, $function = __FUNCTION__, $line = __LINE__)
        {
            $array_data = array($message, $file, $class, $function, $line);
            $this->write('TESTERROR', $array_data);
        }

        private function write($error_type, $array_data)
        {
            $date = date("Y-m-d H:i:s");
            $dateFile = date("Y-m-d");      

            $message = "[{$date}] [{$error_type}] [{$array_data[1]}->{$array_data[2]}::{$array_data[3]}:{$array_data[4]}] $array_data[0]".PHP_EOL;

            try 
            {
                file_put_contents($this->path.'/'.$dateFile.'.log', $message, FILE_APPEND);
            }
            catch (Exception $e)
            {
                error_log('[ERROR] [Log::write()] -> ' . $e, 0);
                header("HTTP/1.0 500 Internal Server Error");
                exit();
            }
        }
    }  
  • 写回答

2条回答 默认 最新

  • dongyanfeng0546 2019-03-01 19:53
    关注

    Check out debug_backtrace().

    So you can do :

    public function Error($message, $debug)
    {
        $array_data = array($message, $debug);
        $this->write('ERROR', $array_data);
    }
    

    $log->Error("Oh noo!!", print_r(debug_backtrace(),true) );
    

    A backtrace contains a potentially huge amount of data so I'm not going to example a full one here, but it can contain:

    • function ; The current function name. See also __FUNCTION__.
    • line ; The current line number. See also __LINE__.
    • file ; The current file name. See also __FILE__.
    • class ; The current class name. See also __CLASS__.
    • object ; The current object.
    • type ; The current call type. If a method call, "->" is returned. If a static method call, "::" is returned. If a function call, nothing is returned.
    • args ; If inside a function, this lists the functions arguments. If inside an included file, this lists the included file name(s).

    debug_backtrace() is a goldmine of information to debug PHP. This covers everything you ask for in your question.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥50 求解vmware的网络模式问题
  • ¥24 EFS加密后,在同一台电脑解密出错,证书界面找不到对应指纹的证书,未备份证书,求在原电脑解密的方法,可行即采纳
  • ¥15 springboot 3.0 实现Security 6.x版本集成
  • ¥15 PHP-8.1 镜像无法用dockerfile里的CMD命令启动 只能进入容器启动,如何解决?(操作系统-ubuntu)
  • ¥30 请帮我解决一下下面六个代码
  • ¥15 关于资源监视工具的e-care有知道的嘛
  • ¥35 MIMO天线稀疏阵列排布问题
  • ¥60 用visual studio编写程序,利用间接平差求解水准网
  • ¥15 Llama如何调用shell或者Python
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?