PHP 7+ 日志记录 class 魔法常量

PHP 7+ Logging class Magic Constants

刚刚写了一点"logging"class,想请教一下这个class的用法,如何让它更容易使用

例如:

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

这就是我目前将错误写入日志文件的方式,但它似乎很复杂?!有没有办法从 "calling" php 中获取日志记录 class 中的 "MAGIC CONSTANTS"?

这是 class 代码(也欢迎任何其他提示):

    <?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();
            }
        }
    }  

改为将这些常量作为函数参数传递:

public function Error(
    $message,
    $file = __FILE__,
    $class = __CLASS__,
    $function = __FUNCTION__,
    $line = __LINE__,
) {
    // ...
}

一如既往地打电话:

$log->Error('xxx');

如果可以的话,你的代码有味道,为什么不使用 PSR-3 兼容的记录器,比如 Monolog? Or even handle errors like a pro with Whoops

查看 debug_backtrace()

所以你可以这样做:

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

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

回溯可能包含大量数据,因此我不打算在这里举例说明完整数据,但它可以包含:

  • 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() 是调试信息的金矿 PHP。这涵盖了您在问题中要求的所有内容。