brianhenryie / bh-wp-cli-logger
A PSR logger for WP CLI commands.
Requires
- psr/log: ^1.0
Requires (Dev)
- 10up/wp_mock: *
- antecedent/patchwork: ^2.1
- brianhenryie/color-logger: ^1.0
- brianhenryie/composer-phpstorm: dev-master
- codeception/module-asserts: ^1.0
- codeception/module-cli: ^1.0
- codeception/module-db: ^1.0.0
- codeception/module-filesystem: ^1.0
- codeception/module-phpbrowser: ^1.0.0
- codeception/module-webdriver: ^1.0
- codeception/util-universalframework: ^1.0
- cweagans/composer-patches: *
- dealerdirect/phpcodesniffer-composer-installer: *
- jaschilz/php-coverage-badger: ^2.0
- johnpbloch/wordpress: *
- kporras07/composer-symlinks: dev-master
- lucatume/wp-browser: *
- phpcompatibility/phpcompatibility-wp: *
- phpstan/extension-installer: *
- phpunit/phpcov: *
- squizlabs/php_codesniffer: *
- szepeviktor/phpstan-wordpress: *
- wordpress/wordpress: *
- wp-cli/dist-archive-command: dev-main
- wp-cli/i18n-command: ^2.4
- wp-cli/wp-cli-bundle: *
- wp-coding-standards/wpcs: *
- wpackagist-theme/twentytwenty: *
This package is auto-updated.
Last update: 2024-11-27 19:23:24 UTC
README
WP CLI PSR Logger
A PSR logger which calls WP_CLI::line()
with the level name coloured.
Allows easy separation of logic and presentation for CLI commands. I.e. no intermingling of WP_CLI::line()
in main application code.
Install
composer require brianhenryie/bh-wp-cli-logger
Operation
$logger = new \BrianHenryIE\WP_CLI_Logger\WP_CLI_Logger(); $logger->warning( 'Something happened!' );
- It is just a PSR Logger Interface implementation (aka a PSR-3 logger)
- It does not filter logs to above a certain level
- It does not record the logs anywhere
There is one filter for customising the output:
/** * 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 );
Try it out
composer install npm install npx wp-env start npx wp-env run cli wp cli-logger
The test plugin CLI command has two options, levels and message:
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.
Delete the Docker containers afterwards:
npx wp-env destroy
Rationale
Error: red, warning: yellow, and notice: blue are the standard WordPress admin_notice
colours.
Debug: blue is the standard WP_CLI debug colour.
Emergency, alert and critical use WP_CLI::error_multi_line()
.
Info logs are not prepended with "Info: ", as other levels are. I'm not 100% set on this.
TODO
Level Filtering
I imagine there is a Composer package that provides the same utility as WooCommerce's WC_Logger::should_handle() function, and is extensible. If I come across one, I'll include it here.
When WP CLI commands are appended with --debug
or more specifically --debug=my-plugin-slug
, all messages should be output. This should be part of any ::should_handle()
function.
psr/log >=2.0
This currently requires "psr/log": "^1.0"
. v2.0 requires PHP 8.0 and introduces parameter types; 3.0 then introduces return types.
Context
Context is not printed.
See Also
- BH WP Logger – this class was extracted from that
- BH Color Logger – like this, for unit tests