mhcg/monolog-wp-cli

Monolog 扩展,支持在运行 wp 命令行时输出到 WP-CLI(WordPress 命令行界面)。

v2.0.0 2022-08-12 01:21 UTC

This package is auto-updated.

Last update: 2024-09-12 05:41:42 UTC


README

Latest Stable Version Code Climate Code Climate coverage

当运行 wp 命令行时,Monolog 处理器支持输出到 WP-CLI。

安装

使用 Composer 安装最新版本。

$ composer require mhcg/monolog-wp-cli

基本用法

示例 1 - 基本概念

Monolog WP-CLI 处理器与其他处理器的工作方式相同,因此创建一个新的 WPCLIHandler 对象并将其推送到您的 Logger 对象。当然,这只能在 WordPress 主题或插件中的 WP-CLI 命令部分工作。请参阅示例 2 以获取真实世界的示例。

<?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');

示例 2 - 基本WordPress插件

以下代码将创建一个名为 mycommand 的新 WP-CLI 命令,该命令执行一些日志记录。

注意 此代码仅显示基本用法,不是创建新命令或插件的建议或推荐方法。请查看 WordPress-Plugin-Boilerplate GitHub 项目,以获取编写插件的更好方法。

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

作者

Mark Heydon <contact@mhcg.co.uk>

许可

Monolog 在 MIT 许可下授权,因此此处理器也是如此 - 有关详细信息,请参阅 LICENSE 文件。