Changeset 2525538
- Timestamp:
- 05/03/2021 08:00:00 PM (5 years ago)
- Location:
- debug-logger/trunk
- Files:
-
- 5 edited
-
README.md (modified) (6 diffs)
-
readme.txt (modified) (5 diffs)
-
src/class-log.php (modified) (2 diffs)
-
src/class-logger.php (modified) (4 diffs)
-
wp-debug-logger.php (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
-
debug-logger/trunk/README.md
r2523842 r2525538 14 14 15 15 16 16 17 ## Version 17 18 18 The current version is 0. 2.0. This project uses [semantic versioning](http://semver.org).19 The current version is 0.3.0. This project uses [semantic versioning](http://semver.org). 19 20 20 21 … … 96 97 $logger = new \WP_Debug_Logger\Logger(); 97 98 $logger->error('The SQL query returned zero rows'); 98 ``` `99 ``` 99 100 100 101 it's recommended that you replace fully namespaced class names with an … … 108 109 $logger = new Logger(); 109 110 $logger->error('The SQL query returned zero rows'); 110 ``` `111 ``` 111 112 112 113 ### Static Log Methods … … 123 124 124 125 Log::error('The SQL query returned zero rows'); 125 ``` `126 ``` 126 127 127 128 Here's the list of all static methods for the Log class … … 136 137 Log::info( 'This is a info message' ); 137 138 Log::debug( 'This is a debug message' ); 138 ``` `139 ``` 139 140 140 141 ### Passing Data to the Log … … 188 189 $your_data 189 190 ); 191 ``` 192 193 One 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 196 Log::wp_error( 197 $error_level, 198 $log_message, 199 $your_data 200 ); 201 ``` 202 203 The `$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 206 if ( is_wp_error( $result ) ) { 207 Log::wp_error( 208 'critical', 209 'This very important query has a severe problem', 210 $result 211 ); 212 } 190 213 ``` 191 214 -
debug-logger/trunk/readme.txt
r2523842 r2525538 1 1 === Debug Logger === 2 2 Contributors: awoods 3 Tags: psr-3, logs, logging, debug, monolog 3 Tags: psr-3, logs, logging, debug, monolog, dev, development 4 4 Requires at least: 5.7 5 5 Tested up to: 5.7 6 6 Requires PHP: 7.4 7 Stable tag: 0. 2.07 Stable tag: 0.3.0 8 8 License: GPLv2 or later 9 9 License URI: https://www.gnu.org/licenses/gpl-2.0.html … … 14 14 == Description == 15 15 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. 16 As 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. 23 17 24 18 … … 32 26 1. Enable debugging in your `wp-config.php` 33 27 34 ` ``php28 ` 35 29 // in your wp-config.php 36 30 define( 'WP_DEBUG', true ); … … 41 35 // For good measure, this will hide errors from being displayed on-screen 42 36 @ini_set('display_errors', 0); 43 ``` 37 ` 38 1. As you write your code, sprinkle in these Log methods. 44 39 45 40 = Minimum Level = 46 41 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. 42 WP_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. 55 43 56 44 = Displaying Errors = 57 45 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. 46 In 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. 63 47 64 ` ``48 ` 65 49 /* That’s all, stop editing! Happy blogging. */ 66 ` ``50 ` 67 51 68 52 == Logging Levels == 69 53 70 There are 8 logging levels available, [defined by RFC 71 5424](https://tools.ietf.org/html/rfc5424). The levels specified in 54 There are 8 logging levels available, [defined by RFC 5424](https://tools.ietf.org/html/rfc5424). The levels specified in 72 55 order from the most severe to the least severe: 73 56 … … 87 70 = Why not just use the error_log function? = 88 71 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. 72 You 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. 94 73 95 74 = Where can I find more documentation? = 96 75 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. 76 This project is [developed on Github](https://github.com/andrewwoods/wp-debug-logger). There is a more complete readme there, with links to supplemental information. 100 77 101 78 = Why use PSR-3? = 102 79 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. 80 A 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 86 No screenshots yet. 87 88 89 90 == Upgrade Notice == 91 92 = 0.3.0 = 93 94 Gain the ability to log WP_Error objects 106 95 107 96 108 97 109 98 == 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 110 105 111 106 = 0.2.0 = -
debug-logger/trunk/src/class-log.php
r2523842 r2525538 2 2 3 3 namespace WP_Debug_Logger; 4 5 use Psr\Log\InvalidArgumentException; 6 use WP_Error; 4 7 5 8 /** … … 112 115 $logger->debug( $message . "=\n" . $data ); 113 116 } 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 } 114 138 } -
debug-logger/trunk/src/class-logger.php
r2523842 r2525538 4 4 5 5 use Psr\Log\LogLevel; 6 use WP_Error; 6 7 7 8 class Logger implements \Psr\Log\LoggerInterface { … … 19 20 20 21 /** 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 /** 21 33 * Interpolates context values into the message placeholders. 22 34 * … … 118 130 } 119 131 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 120 152 /** 121 153 * Determine if the current level meets the minimum *severity* level … … 156 188 return $this->levels[ LogLevel::ERROR ]; 157 189 } 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 } 158 211 } -
debug-logger/trunk/wp-debug-logger.php
r2523842 r2525538 8 8 * Text Domain: wp-debug-logger 9 9 * Domain Path: /languages 10 * Version: 0. 2.010 * Version: 0.3.0 11 11 * 12 12 * @package Wp_Debug_Logger
Note: See TracChangeset
for help on using the changeset viewer.