gebruederheitz/wp-debug-log

一个轻量级的类和特性,使调试输出更简单。

1.0.1 2022-07-13 13:03 UTC

This package is auto-updated.

Last update: 2024-09-13 18:17:10 UTC


README

一个轻量级的类和特性,使调试输出更简单。

安装

通过 composer

> composer require gebruederheitz/wp-debug-log

确保您已安装 Composer 自动加载或备选类加载器。

用法

该库使用内部 error_log() 函数,根据条件将消息写入 Wordpress 的调试日志文件。日志输出的唯一要求是环境变量 WORDPRESS_ENV 已设置且不等于字符串 "production"

您可以直接调用 Debug 类的静态方法

use Gebruederheitz\Wordpress\Debug;

if (Debug::isDebug()) {
    // do dev-instance only things
}

// Writes the message to the logfile whenever WORDPRESS_ENV is set and not
// "production"
Debug::log('My first log message');
Debug::log('I\'m logging a context as well now', [$myVariable]);

// Uses output buffering to capture a var_dump() and write the result to the
// logfile
Debug::dump($myVariable);
Debug::dump($myVariable. 'This is my variable, dumped:');

要从不同的模块获得更详细的输出,您可以使用提供的特性,这将把命名空间添加到您的日志消息前

use Gebruederheitz\Wordpress\withDebug;

class MyClass {
    use withDebug;
    
    public function doSomething($arg = 42): void 
    {
        self::debugLog('Doing something with these args:', [$arg]);
        /*
         * Results in:
         * [MyClass] Doing something with these args: 42 
         */
         
         self::debugDump($arg);
    }
}

您还可以通过静态类属性设置自定义命名空间

use Gebruederheitz\Wordpress\withDebug;

class MyClass {
    use withDebug;
    
    protected static $debugNamespace = 'Module:NextL3vel';
    
    public function doSomething(): void 
    {
        $myVar = 42;
         self::debugDump($myVar, 'The answer to question about life, the universe, and everything is');
        /*
         * Results in:
         * [Module:NextL3vel] The answer to question about life, the universe, and everything is 
         * (int)42 
         */
    }
}

开发

依赖

  • PHP >= 7.3
  • Composer 2.x
  • NVM 和 nodeJS LTS (v16.x)
  • 推荐:GNU Make(或替代方案)