PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.2
Jetpack – WP Security, Backup, Speed, & Growth v16.2
16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 13.9.2 14.0.1 14.1.1 14.2.2 All 502 releases
jetpack / modules / sitemaps / sitemap-logger.php

sitemap-logger.php in Jetpack – WP Security, Backup, Speed, & Growth 16.2, at modules/sitemaps/sitemap-logger.php

95 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 if ( ! defined( 'ABSPATH' ) ) {
10 exit( 0 );
11 }
12
13 /**
14 * Handles logging errors and debug messages for sitemap generator.
15 *
16 * A Jetpack_Sitemap_Logger object keeps track of its birth time as well
17 * as a "unique" ID string. Calling the report() method writes a message
18 * to the PHP error log as well as the ID string for easier grepping.
19 *
20 * @since 4.8.0
21 */
22 class Jetpack_Sitemap_Logger {
23 /**
24 * A unique-ish string for each logger, enabling us to grep
25 * for the messages written by an individual generation phase.
26 *
27 * @access private
28 * @since 4.8.0
29 * @var string $key The key string.
30 */
31 private $key;
32
33 /**
34 * The birth time of this object in microseconds.
35 *
36 * @access private
37 * @since 4.8.0
38 * @var int $starttime The birth time.
39 */
40 private $starttime;
41
42 /**
43 * Initializes a new logger object.
44 *
45 * @access public
46 * @since 4.8.0
47 *
48 * @param string $message An optional message string to be written to the debug log on initialization.
49 */
50 public function __construct( $message = null ) {
51 $this->key = wp_generate_password( 5, false );
52 $this->starttime = microtime( true );
53 if ( $message !== null ) {
54 $this->report( $message );
55 }
56 }
57
58 /**
59 * Writes a string to the debug log, including the logger's ID string.
60 *
61 * @access public
62 * @since 4.8.0
63 *
64 * @param string $message The string to be written to the log.
65 * @param boolean $is_error If true, $message will be logged even if JETPACK_DEV_DEBUG is not enabled.
66 */
67 public function report( $message, $is_error = false ) {
68 $message = 'jp-sitemap-' . $this->key . ': ' . $message;
69 if ( ! ( defined( 'WP_DEBUG' ) && WP_DEBUG ) ) {
70 return;
71 }
72 if ( ! $is_error && ! ( defined( 'JETPACK_DEV_DEBUG' ) && JETPACK_DEV_DEBUG ) ) {
73 return;
74 }
75 // Append memory usage in MB (human readable)
76 $usage = memory_get_usage( true );
77 $usage_mb = round( $usage / MB_IN_BYTES, 2 );
78 $message .= ' [Memory usage: ' . $usage_mb . ' MB]';
79 error_log( $message ); // phpcs:ignore WordPress.PHP.DevelopmentFunctions.error_log_error_log
80 }
81
82 /**
83 * Writes the elapsed lifetime of the logger to the debug log, with an optional message.
84 *
85 * @access public
86 * @since 4.8.0
87 *
88 * @param string $message The optional message string. Default is the empty string.
89 */
90 public function time( $message = '' ) {
91 $time = round( microtime( true ) - $this->starttime, 3 );
92 $this->report( $message . ' ' . $time . ' seconds elapsed.' );
93 }
94 }
95