sitemap-buffer-fallback.php
4 years ago
sitemap-buffer-image-fallback.php
4 years ago
sitemap-buffer-image.php
3 years ago
sitemap-buffer-master-fallback.php
4 years ago
sitemap-buffer-master.php
3 years ago
sitemap-buffer-news-fallback.php
4 years ago
sitemap-buffer-news.php
3 years ago
sitemap-buffer-page-fallback.php
4 years ago
sitemap-buffer-page.php
3 years ago
sitemap-buffer-video-fallback.php
4 years ago
sitemap-buffer-video.php
3 years ago
sitemap-buffer.php
4 years ago
sitemap-builder.php
3 years ago
sitemap-constants.php
4 years ago
sitemap-finder.php
4 years ago
sitemap-librarian.php
4 years ago
sitemap-logger.php
4 years ago
sitemap-state.php
4 years ago
sitemap-stylist.php
4 years ago
sitemaps.php
5 years ago
sitemap-logger.php
87 lines
| 1 | <?php // phpcs:ignore WordPress.Files.FileName.InvalidClassFileName |
| 2 | /** |
| 3 | * A message logger for the Jetpack Sitemap module. |
| 4 | * |
| 5 | * @package automattic/jetpack |
| 6 | * @since 4.8.0 |
| 7 | */ |
| 8 | |
| 9 | /** |
| 10 | * Handles logging errors and debug messages for sitemap generator. |
| 11 | * |
| 12 | * A Jetpack_Sitemap_Logger object keeps track of its birth time as well |
| 13 | * as a "unique" ID string. Calling the report() method writes a message |
| 14 | * to the PHP error log as well as the ID string for easier grepping. |
| 15 | * |
| 16 | * @since 4.8.0 |
| 17 | */ |
| 18 | class Jetpack_Sitemap_Logger { |
| 19 | /** |
| 20 | * A unique-ish string for each logger, enabling us to grep |
| 21 | * for the messages written by an individual generation phase. |
| 22 | * |
| 23 | * @access private |
| 24 | * @since 4.8.0 |
| 25 | * @var string $key The key string. |
| 26 | */ |
| 27 | private $key; |
| 28 | |
| 29 | /** |
| 30 | * The birth time of this object in microseconds. |
| 31 | * |
| 32 | * @access private |
| 33 | * @since 4.8.0 |
| 34 | * @var int $starttime The birth time. |
| 35 | */ |
| 36 | private $starttime; |
| 37 | |
| 38 | /** |
| 39 | * Initializes a new logger object. |
| 40 | * |
| 41 | * @access public |
| 42 | * @since 4.8.0 |
| 43 | * |
| 44 | * @param string $message An optional message string to be written to the debug log on initialization. |
| 45 | */ |
| 46 | public function __construct( $message = null ) { |
| 47 | $this->key = wp_generate_password( 5, false ); |
| 48 | $this->starttime = microtime( true ); |
| 49 | if ( $message !== null ) { |
| 50 | $this->report( $message ); |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | /** |
| 55 | * Writes a string to the debug log, including the logger's ID string. |
| 56 | * |
| 57 | * @access public |
| 58 | * @since 4.8.0 |
| 59 | * |
| 60 | * @param string $message The string to be written to the log. |
| 61 | * @param boolean $is_error If true, $message will be logged even if JETPACK_DEV_DEBUG is not enabled. |
| 62 | */ |
| 63 | public function report( $message, $is_error = false ) { |
| 64 | $message = 'jp-sitemap-' . $this->key . ': ' . $message; |
| 65 | if ( ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) { |
| 66 | return; |
| 67 | } |
| 68 | if ( ! $is_error && ! ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) ) { |
| 69 | return; |
| 70 | } |
| 71 | error_log( $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log |
| 72 | } |
| 73 | |
| 74 | /** |
| 75 | * Writes the elapsed lifetime of the logger to the debug log, with an optional message. |
| 76 | * |
| 77 | * @access public |
| 78 | * @since 4.8.0 |
| 79 | * |
| 80 | * @param string $message The optional message string. Default is the empty string. |
| 81 | */ |
| 82 | public function time( $message = '' ) { |
| 83 | $time = round( microtime( true ) - $this->starttime, 3 ); |
| 84 | $this->report( $message . ' ' . $time . ' seconds elapsed.' ); |
| 85 | } |
| 86 | } |
| 87 |