Plugin Directory

Changeset 2525538


Ignore:
Timestamp:
05/03/2021 08:00:00 PM (5 years ago)
Author:
awoods
Message:

Add Debug Logger version 0.3.0

Location:
debug-logger/trunk
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • debug-logger/trunk/README.md

    r2523842 r2525538  
    1414
    1515
     16
    1617## Version
    1718
    18 The current version is 0.2.0. This project uses [semantic versioning](http://semver.org).
     19The current version is 0.3.0. This project uses [semantic versioning](http://semver.org).
    1920
    2021
     
    9697$logger = new \WP_Debug_Logger\Logger();
    9798$logger->error('The SQL query returned zero rows');
    98 ````
     99```
    99100
    100101it's recommended that you replace fully namespaced class names with an
     
    108109$logger = new Logger();
    109110$logger->error('The SQL query returned zero rows');
    110 ````
     111```
    111112
    112113### Static Log Methods
     
    123124
    124125Log::error('The SQL query returned zero rows');
    125 ````
     126```
    126127
    127128Here's the list of all static methods for the Log class
     
    136137Log::info( 'This is a info message' );
    137138Log::debug( 'This is a debug message' );
    138 ````
     139```
    139140
    140141### Passing Data to the Log
     
    188189    $your_data
    189190);
     191```
     192
     193One of the key features of the WordPress error model, is the WP_Error class. There are some functions that will return an WP_Error object, instead of false, like many PHP functions. Now you have an easy way to log these objects. Here's how you'd call it.
     194
     195```php
     196Log::wp_error(
     197    $error_level,
     198    $log_message,
     199    $your_data
     200);
     201```
     202
     203The `$error_level` **must** be one of these values: `emergency, alert, critical, error, warning, notice, info, debug`. In the event you don't, an exception will be thrown.
     204
     205```php
     206if ( is_wp_error( $result ) ) {
     207    Log::wp_error(
     208        'critical',
     209        'This very important query has a severe problem',
     210        $result
     211    );
     212}
    190213```
    191214
  • debug-logger/trunk/readme.txt

    r2523842 r2525538  
    11=== Debug Logger ===
    22Contributors: awoods
    3 Tags: psr-3, logs, logging, debug, monolog
     3Tags: psr-3, logs, logging, debug, monolog, dev, development
    44Requires at least: 5.7
    55Tested up to: 5.7
    66Requires PHP: 7.4
    7 Stable tag: 0.2.0
     7Stable tag: 0.3.0
    88License: GPLv2 or later
    99License URI: https://www.gnu.org/licenses/gpl-2.0.html
     
    1414== Description ==
    1515
    16 As PHP moves forward, so must WordPress. This plugin helps WordPress use
    17 the tools of modern PHP. Monolog — PHP's most popular logging package —
    18 is a composer package. Since WordPress doesn't currently have a
    19 universal way to support composer, this WordPress plugin is meant to
    20 start bridging the gap. This logger is
    21 [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md)
    22 compliant, a PHP standard which Monolog also uses.
     16As PHP moves forward, so must WordPress. This plugin helps WordPress use the tools of modern PHP. Monolog — PHP's most popular logging package — is a composer package. Since WordPress doesn't currently have a universal way to support composer, this WordPress plugin is meant to start bridging the gap. This logger is [PSR-3](https://github.com/php-fig/fig-standards/blob/master/accepted/PSR-3-logger-interface.md) compliant, a PHP standard which Monolog also uses.
    2317
    2418
     
    32261. Enable debugging in your `wp-config.php`
    3327
    34 ```php
     28`
    3529// in your wp-config.php
    3630define( 'WP_DEBUG', true );
     
    4135// For good measure, this will hide errors from being displayed on-screen
    4236@ini_set('display_errors', 0);
    43 ```
     37`
     381. As you write your code, sprinkle in these Log methods.
    4439
    4540= Minimum Level =
    4641
    47 `WP_DEBUG_MINIMUM_LEVEL` is a new constant that determines the minimum
    48 severity level you wish to write to your `wp-content/debug.log` file. In
    49 your *development* environment, I recommend using `debug` so you can see
    50 all the errors being written. For your *production* environment, I'd
    51 recommend the `error` level, so you can capture all the significant
    52 problems. Here are the values to use: **emergency, alert, critical,
    53 error, warning, notice, info, debug**. Note: they're all lowercase, as
    54 the value is case-sensitive.
     42WP_DEBUG_MINIMUM_LEVEL is a new constant that determines the minimum severity level you wish to write to your *wp-content/debug.log* file. In your *development* environment, I recommend using `debug` so you can see all the errors being written. For your *production* environment, I'd recommend the `error` level, so you can capture all the significant problems. Here are the values to use: **emergency, alert, critical, error, warning, notice, info, debug**. Note: they're all lowercase, as the value is case-sensitive.
    5543
    5644= Displaying Errors =
    5745
    58 In your **development** environment, you may choose to set
    59 `WP_DEBUG_DISPLAY` to `true`, so the error messages show in your
    60 browser. However, I **strongly recommend** that you do not change it,
    61 for your *production* environment. These settings can be placed anywhere
    62 above the line.
     46In your **development** environment, you may choose to set `WP_DEBUG_DISPLAY` to `true`, so the error messages show in your browser. However, I **strongly recommend** that you do not change it, for your *production* environment. These settings can be placed anywhere above the line.
    6347
    64 ```
     48`
    6549/* That’s all, stop editing! Happy blogging. */
    66 ```
     50`
    6751
    6852== Logging Levels ==
    6953
    70 There are 8 logging levels available, [defined by RFC
    71 5424](https://tools.ietf.org/html/rfc5424). The levels specified in
     54There are 8 logging levels available, [defined by RFC 5424](https://tools.ietf.org/html/rfc5424). The levels specified in
    7255order from the most severe to the least severe:
    7356
     
    8770= Why not just use the error_log function? =
    8871
    89 You still can. However, the plugin will add value to your logging
    90 efforts. Using this logger will add structure io the debug.log file,
    91 *and* give you a modern PHP interface to control the amount of logging
    92 in your website. The logging methods in this plugin also provide
    93 information about the severity of the error.
     72You still can. However, the plugin will add value to your logging efforts. Using this logger will add structure io the debug.log file, *and* give you a modern PHP interface to control the amount of logging in your website. The logging methods in this plugin also provide information about the severity of the error.
    9473
    9574= Where can I find more documentation? =
    9675
    97 This project is [developed on
    98 Github](https://github.com/andrewwoods/wp-debug-logger).  There is a
    99 more complete readme there, with links to supplemental information.
     76This project is [developed on Github](https://github.com/andrewwoods/wp-debug-logger). There is a more complete readme there, with links to supplemental information.
    10077
    10178= Why use PSR-3? =
    10279
    103 A PSR is a PHP Standard Recommendation. PSRs are use to create and
    104 maintain interoperability between PHP-based frameworks and content
    105 management systems.
     80A PSR is a PHP Standard Recommendation. PSRs are use to create and maintain interoperability between PHP-based frameworks and content management systems.
     81
     82
     83
     84== Screenshots ==
     85
     86No screenshots yet.
     87
     88
     89
     90== Upgrade Notice ==
     91
     92= 0.3.0 =
     93
     94Gain the ability to log WP_Error objects
    10695
    10796
    10897
    10998== Changelog ==
     99
     100= 0.3.0 =
     101
     102* Add the ability to log WP_Error objects
     103    - Add `Log::wp_error()` and its corresponding `Logger->log_wp_error` method
     104    - Add method Logger->has_level() to ensure a level exists
    110105
    111106= 0.2.0 =
  • debug-logger/trunk/src/class-log.php

    r2523842 r2525538  
    22
    33namespace WP_Debug_Logger;
     4
     5use Psr\Log\InvalidArgumentException;
     6use WP_Error;
    47
    58/**
     
    112115        $logger->debug( $message . "=\n" . $data );
    113116    }
     117
     118    /**
     119     * Write the codes and messages in a WP_Error object to the log using the specified level
     120     *
     121     * @param string $level
     122     * @param string $message
     123     * @param WP_Error $wp_error
     124     *
     125     * @return void
     126     *
     127     * @throws InvalidArgumentException
     128     */
     129    public static function wp_error( string $level, string $message, WP_Error $wp_error ) {
     130        $logger = new Logger();
     131
     132        if ( ! $logger->has_level( $level ) ) {
     133            throw new InvalidArgumentException( "The level your requested '{$level}' is not valid" );
     134        }
     135
     136        $logger->log_wp_error( $level, $message, $wp_error );
     137    }
    114138}
  • debug-logger/trunk/src/class-logger.php

    r2523842 r2525538  
    44
    55use Psr\Log\LogLevel;
     6use WP_Error;
    67
    78class Logger implements \Psr\Log\LoggerInterface {
     
    1920
    2021    /**
     22     * Determine if a level exists
     23     *
     24     * @param string $level
     25     *
     26     * @return bool
     27     */
     28    public function has_level( string $level ) : bool {
     29        return isset( $this->levels[ $level ] );
     30    }
     31
     32    /**
    2133     * Interpolates context values into the message placeholders.
    2234     *
     
    118130    }
    119131
     132    public function log_wp_error( $level, $message, $error ) {
     133
     134        if ( ! WP_DEBUG ) {
     135            return; // Don't allow writing when WP_DEBUG is false
     136        }
     137
     138        if ( ! WP_DEBUG_LOG ) {
     139            return; // Don't allow writing when WP_DEBUG_LOG is false
     140        }
     141
     142        if ( $this->meets_minimum_level( $level, WP_DEBUG_MINIMUM_LEVEL ) ) {
     143            $content = date('Y-m-d H:i:s' ) . ' ';
     144            $content .= strtoupper( $level ) . ': ';
     145            $content .= "{$message}:\n";
     146            $content .= $this->get_errors( $error );
     147
     148            error_log( $content );
     149        }
     150    }
     151
    120152    /**
    121153     * Determine if the current level meets the minimum *severity* level
     
    156188        return $this->levels[ LogLevel::ERROR ];
    157189    }
     190
     191    /**
     192     * Extract all the messages from a WP_Error object
     193     *
     194     * @param WP_Error $wp_error
     195     *
     196     * @return string
     197     */
     198    public function get_errors( WP_Error $wp_error ) {
     199        $errors = '';
     200        if ($wp_error->has_errors()) {
     201            error_log('Yup! Has errors' );
     202            foreach ($wp_error->get_error_codes() as $error_code ){
     203                $messages = $wp_error->get_error_messages( $error_code );
     204                $message = implode('; ', $messages );
     205                $errors .= "Code {$error_code}: {$message}\n";
     206            }
     207        }
     208
     209        return $errors;
     210    }
    158211}
  • debug-logger/trunk/wp-debug-logger.php

    r2523842 r2525538  
    88 * Text Domain:     wp-debug-logger
    99 * Domain Path:     /languages
    10  * Version:         0.2.0
     10 * Version:         0.3.0
    1111 *
    1212 * @package         Wp_Debug_Logger
Note: See TracChangeset for help on using the changeset viewer.