mhcg / monolog-wp-cli
Extension for Monolog to support outputting to WP-CLI (The WordPress command line interface) when running wp command lines.
Installs: 70 449
Dependents: 1
Suggesters: 0
Security: 0
Stars: 8
Watchers: 3
Forks: 3
Open Issues: 2
Requires
- php: ^7.3 | ^7.4 | ^8.0
- monolog/monolog: ^2.5
Requires (Dev)
- phpunit/phpunit: ^9.5
- wp-cli/wp-cli: ^2.0
README
Handler for Monolog to support outputting to WP-CLI when running wp command lines.
Installation
Install the latest version with Composer.
$ composer require mhcg/monolog-wp-cli
Basic Usage
Example 1 - Basic Concept
Monolog WP-CLI Handler works the same as any other handler, so create a new WPCLIHandler object and push it to your Logger object. Of course, this will only work as part of a WP-CLI command within a WordPress theme or plugin. See Example 2 for a real-world example.
<?php use Monolog\Logger; use MHCG\Monolog\Handler\WPCLIHandler; // create a log channel $log = new Logger('name'); $log->pushHandler(new WPCLIHandler(Logger::WARNING)); // output to WP-CLI $log->warning('This is a warning'); $log->error('An error has occurred'); $log->critical('This will report error and exit out'); $log->debug('Only shown when running wp with --debug'); $log->info('General logging - will not be shown when running wp with --quiet');
Example 2 - Basic WordPress Plugin
The following code will create a a new WP-CLI command of mycommand
that does some logging.
Note this code shows basic usage only and is not a recommended or suggested way to create a new command or plugin. Check out the WordPress-Plugin-Boilerplate GitHub project for a much better way to write plugins.
composer require mhcg/monolog-wp-cli
<?php /** * Plugin Name: My Plugin */ //my-plugin.php use Monolog\Logger; use MHCG\Monolog\Handler\WPCLIHandler; // If this file is called directly, abort. if ( ! defined( 'ABSPATH' ) ) { die; } // Autoload $autoload = dirname( __FILE__ ) . '/vendor/autoload.php'; if ( file_exists( $autoload ) ) { require_once $autoload; } // 'mycommand' WP-CLI command if ( defined( 'WP_CLI' ) && WP_CLI ) { function mycommand_command( $args ) { // create logger $log = new Logger( 'name' ); $log->pushHandler( new WPCLIHandler( Logger::INFO ) ); // debug -- will only show when wp is run with --debug $log->debug( 'Some geeky stuff'); // the following won't show when wp is run with --quiet $log->info( ' Started running' ); $log->warning( 'Something happened of note' ); // always shows even with --quiet $log->error( 'An error has occurred' ); // all done - no real equivalent in Logger of WP_CLI::success WP_CLI::success( 'Finished running mycommand' ); } WP_CLI::add_command( 'mycommand', 'mycommand_command' ); }
$ wp mycommand Started running Warning: (WARNING) Something happened of note Error: (ERROR) An error has occurred Success: Finished running mycommand
$ wp mycommand --quiet Error: (ERROR) An error has occured
Author
Mark Heydon <contact@mhcg.co.uk>
License
Monolog is licensed under the MIT License and therefore so is this handler - see the LICENSE file for details.