brianhenryie/bh-wp-cli-logger

WP CLI 命令的 PSR 记录器。

This package is auto-updated.

Last update: 2024-08-27 18:53:14 UTC


README

WordPress tested 6.3 PHPCS WPCS PHPStan

WP CLI PSR 记录器

一个调用带有颜色级别的 WP_CLI::line() 的 PSR 记录器。

允许轻松分离 CLI 命令的逻辑和表示。即主应用程序代码中不混合 WP_CLI::line()

安装

composer require brianhenryie/bh-wp-cli-logger

操作

$logger = new \BrianHenryIE\WP_CLI_Logger\WP_CLI_Logger();
$logger->warning( 'Something happened!' );

Something happened screenshot

  • 它只是一个 PSR 记录器接口 实现(即 PSR-3 记录器)
  • 它不会过滤高于一定级别的日志
  • 它不会在任何地方记录日志

有一个过滤器用于自定义输出

/**
 * Filter logs to control output style.
 *
 * NB: try to use the `context` to only apply this filter to logs output by your own plugin.
 * Return null, an empty array, or an empty message to prevent output.
 *
 * @var ?array{level:string, message:string, context:array<mixed>, prepend:string, ansi_color:string} $log
 */
$log = apply_filters( 'bh_wp_cli_logger_log', $log );

试试看

composer install
npm install

npx wp-env start

npx wp-env run cli wp cli-logger

Test plugin screenshot

测试插件 CLI 命令有两个选项,级别和消息
NAME

  wp cli-logger

DESCRIPTION

  Test the WP_CLI PSR logger.

SYNOPSIS

  wp cli-logger [<levels>...] [--message=<message>]

  [<levels>...]
    Optional list of log levels to show.

  [--message=<message>]
    A message to use in the output. Replaces `{level}` in the template if present.
    ---
    default: "This is a {level} log message."
    ---

EXAMPLES

     # Print out a log message for notice
     $ wp test-plugin notice
     Notice:  This is a notice log message.

     # Print out a custom log message for warning
     $ wp test-plugin warning --message="Uh, oh... something looks amiss."
     Warning: Uh, oh... something looks amiss.

     # Print out a log messages for two levels
     $ wp test-plugin notice debug
     Notice:  This is a notice log message.
     Debug:   This is a debug log message.

之后删除 Docker 容器

npx wp-env destroy

原因

错误:红色,警告:黄色,以及注意:蓝色是标准 WordPress admin_notice 颜色。

调试:蓝色是标准 WP_CLI 调试颜色。

紧急、警报和关键使用 WP_CLI::error_multi_line()

信息日志不以 "Info: " 开头,如其他级别那样。我对此并不完全确定。

待办事项

级别过滤

我想象中有一个 Composer 包提供了与 WooCommerce 的 WC_Logger::should_handle() 函数相同的实用程序,并且是可扩展的。如果我发现了一个,我会在其中包含它。

当 WP CLI 命令附加了 --debug 或更具体地 --debug=my-plugin-slug 时,所有消息 都应该 输出。这应该是任何 ::should_handle() 函数的一部分。

psr/log >=2.0

这目前需要 "psr/log": "^1.0"v2.0 需要 PHP 8.0 并引入了参数类型;3.0 然后引入了返回类型。

上下文

上下文不会打印。

另请参阅