| 1 |
<?php |
| 2 |
|
| 3 |
namespace Templately\Utils\Log; |
| 4 |
|
| 5 |
use Templately\Utils\Helper; |
| 6 |
use Throwable; |
| 7 |
|
| 8 |
/** |
| 9 |
* The one place server-side detail is recorded (spec 043 / FR-008). |
| 10 |
* |
| 11 |
* The contract's hard rule is that a stack trace, a server file path or a raw |
| 12 |
* exception message NEVER travels to the client. This facade is the other half |
| 13 |
* of that rule: the detail is not discarded, it is logged here — so the |
| 14 |
* normalizer and the envelope can stay ruthless about what they emit. |
| 15 |
* |
| 16 |
* Thin on purpose: it delegates to `Helper::log()` (which is `WP_DEBUG_LOG` |
| 17 |
* gated) and exists so call sites express intent — `Logger::exception()` — and |
| 18 |
* so the "never echo this" boundary has a name. |
| 19 |
*/ |
| 20 |
class Logger { |
| 21 |
|
| 22 |
/** |
| 23 |
* @param string $message |
| 24 |
* @param string $context |
| 25 |
* @return void |
| 26 |
*/ |
| 27 |
public static function info( $message, $context = '' ) { |
| 28 |
Helper::log( $message, $context, 'info' ); |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* @param string $message |
| 33 |
* @param string $context |
| 34 |
* @return void |
| 35 |
*/ |
| 36 |
public static function warning( $message, $context = '' ) { |
| 37 |
Helper::log( $message, $context, 'warning' ); |
| 38 |
} |
| 39 |
|
| 40 |
/** |
| 41 |
* @param string $message |
| 42 |
* @param string $context |
| 43 |
* @return void |
| 44 |
*/ |
| 45 |
public static function error( $message, $context = '' ) { |
| 46 |
Helper::log( $message, $context, 'error' ); |
| 47 |
} |
| 48 |
|
| 49 |
/** |
| 50 |
* Record a throwable in full — the ONLY place its trace is allowed to exist. |
| 51 |
* |
| 52 |
* @param Throwable $throwable |
| 53 |
* @param string $context |
| 54 |
* @return void |
| 55 |
*/ |
| 56 |
public static function exception( $throwable, $context = '' ) { |
| 57 |
if ( ! $throwable instanceof Throwable ) { |
| 58 |
return; |
| 59 |
} |
| 60 |
|
| 61 |
Helper::log( |
| 62 |
sprintf( |
| 63 |
'%s: %s in %s:%d', |
| 64 |
get_class( $throwable ), |
| 65 |
$throwable->getMessage(), |
| 66 |
$throwable->getFile(), |
| 67 |
$throwable->getLine() |
| 68 |
), |
| 69 |
$context, |
| 70 |
'error' |
| 71 |
); |
| 72 |
|
| 73 |
Helper::log( $throwable->getTraceAsString(), $context, 'error' ); |
| 74 |
} |
| 75 |
|
| 76 |
/** |
| 77 |
* Log an upstream body that we refused to pass through, with the |
| 78 |
* client-visible detail already removed. |
| 79 |
* |
| 80 |
* @param array $body |
| 81 |
* @param string $context |
| 82 |
* @return void |
| 83 |
*/ |
| 84 |
public static function upstream( $body, $context = '' ) { |
| 85 |
if ( ! is_array( $body ) ) { |
| 86 |
Helper::log( (string) $body, $context, 'error' ); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
Helper::log( wp_json_encode( $body ), $context, 'error' ); |
| 91 |
} |
| 92 |
} |
| 93 |
|