| 1 |
<?php |
| 2 |
|
| 3 |
|
| 4 |
if (!defined('ABSPATH')) { |
| 5 |
exit; |
| 6 |
} |
| 7 |
|
| 8 |
// allow-no-test-found: covered by tests/LoggingTest.php through public ABJ_404_Solution_Logging message entry points. |
| 9 |
|
| 10 |
/** |
| 11 |
* Formats and writes severity-tagged debug log messages. |
| 12 |
* |
| 13 |
* Owns the DEBUG/INFO/WARN/ERROR line shapes and the stored-debug-message |
| 14 |
* buffer used when debug mode is disabled. It deliberately receives its time, |
| 15 |
* debug-mode, and file-write dependencies as callables so the public |
| 16 |
* ABJ_404_Solution_Logging facade remains the only production entry point. |
| 17 |
*/ |
| 18 |
class ABJ_404_Solution_LoggingMessageWriter { |
| 19 |
|
| 20 |
/** @var callable */ |
| 21 |
private $timestampProvider; |
| 22 |
/** @var callable */ |
| 23 |
private $debugModeProvider; |
| 24 |
/** @var callable */ |
| 25 |
private $lineWriter; |
| 26 |
/** @var array<int, string> */ |
| 27 |
private $storedDebugMessages; |
| 28 |
|
| 29 |
/** |
| 30 |
* @param callable $timestampProvider Returns the formatted current timestamp. |
| 31 |
* @param callable $debugModeProvider Returns true when debug mode is enabled. |
| 32 |
* @param callable $lineWriter Receives one fully-formatted log line. |
| 33 |
* @param array<int, string> $storedDebugMessages Shared debug buffer from the facade. |
| 34 |
*/ |
| 35 |
public function __construct( |
| 36 |
callable $timestampProvider, |
| 37 |
callable $debugModeProvider, |
| 38 |
callable $lineWriter, |
| 39 |
array &$storedDebugMessages |
| 40 |
) { |
| 41 |
$this->timestampProvider = $timestampProvider; |
| 42 |
$this->debugModeProvider = $debugModeProvider; |
| 43 |
$this->lineWriter = $lineWriter; |
| 44 |
$this->storedDebugMessages =& $storedDebugMessages; |
| 45 |
} |
| 46 |
|
| 47 |
/** |
| 48 |
* Write immediately when debug mode is enabled; otherwise buffer for the |
| 49 |
* next error log entry. |
| 50 |
* |
| 51 |
* @param string $message |
| 52 |
* @param \Throwable|null $e |
| 53 |
* @return void |
| 54 |
*/ |
| 55 |
public function debugMessage(string $message, $e = null): void { |
| 56 |
$stacktrace = ""; |
| 57 |
if ($e != null) { |
| 58 |
$stacktrace = ", Stacktrace: " . $e->getTraceAsString(); |
| 59 |
} |
| 60 |
|
| 61 |
$line = $this->timestampPrefix('DEBUG') . $message . $stacktrace; |
| 62 |
if ((bool)call_user_func($this->debugModeProvider)) { |
| 63 |
$this->writeLine($line); |
| 64 |
return; |
| 65 |
} |
| 66 |
|
| 67 |
$this->storedDebugMessages[] = $line; |
| 68 |
} |
| 69 |
|
| 70 |
/** |
| 71 |
* @param string $message |
| 72 |
* @return void |
| 73 |
*/ |
| 74 |
public function infoMessage(string $message): void { |
| 75 |
$this->writeLine($this->timestampPrefix('INFO') . $message); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* @param string $message |
| 80 |
* @return void |
| 81 |
*/ |
| 82 |
public function warn(string $message): void { |
| 83 |
$this->writeLine($this->timestampPrefix('WARN') . $message); |
| 84 |
} |
| 85 |
|
| 86 |
/** |
| 87 |
* Flush any debug messages buffered while debug mode was disabled, then |
| 88 |
* write the ERROR line with request/plugin context. |
| 89 |
* |
| 90 |
* @param string $message |
| 91 |
* @param \Exception|null $e |
| 92 |
* @return void |
| 93 |
*/ |
| 94 |
public function errorMessage(string $message, $e = null): void { |
| 95 |
if ($e == null) { |
| 96 |
$e = new Exception; |
| 97 |
} |
| 98 |
$stacktrace = $e->getTraceAsString(); |
| 99 |
|
| 100 |
$savedDebugMessages = implode("\n", $this->storedDebugMessages); |
| 101 |
$this->storedDebugMessages = array(); |
| 102 |
|
| 103 |
$referrer = ''; |
| 104 |
if (array_key_exists('HTTP_REFERER', $_SERVER) && !empty($_SERVER['HTTP_REFERER'])) { |
| 105 |
$referrer = $_SERVER['HTTP_REFERER']; |
| 106 |
} |
| 107 |
$requestedURL = ''; |
| 108 |
if (array_key_exists('REQUEST_URI', $_SERVER) && !empty($_SERVER['REQUEST_URI'])) { |
| 109 |
$requestedURL = $_SERVER['REQUEST_URI']; |
| 110 |
} |
| 111 |
|
| 112 |
$this->writeLine($this->timestampPrefix('ERROR') . $message . ", PHP version: " . PHP_VERSION . |
| 113 |
", WP ver: " . get_bloginfo('version') . ", Plugin ver: " . ABJ404_VERSION . |
| 114 |
", Referrer: " . $referrer . ", Requested URL: " . $requestedURL . |
| 115 |
", \nStored debug messages: \n" . $savedDebugMessages . ", \nTrace: " . $stacktrace); |
| 116 |
} |
| 117 |
|
| 118 |
/** |
| 119 |
* @param string $level |
| 120 |
* @return string |
| 121 |
*/ |
| 122 |
private function timestampPrefix(string $level): string { |
| 123 |
return (string)call_user_func($this->timestampProvider) . ' (' . $level . '): '; |
| 124 |
} |
| 125 |
|
| 126 |
/** |
| 127 |
* @param string $line |
| 128 |
* @return void |
| 129 |
*/ |
| 130 |
private function writeLine(string $line): void { |
| 131 |
call_user_func($this->lineWriter, $line); |
| 132 |
} |
| 133 |
} |
| 134 |
|