PluginProbe
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! / 3.8.0
Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! v3.8.0
3.8.0 3.7.5 3.7.4 3.7.3 3.7.2 1-final 3.7.1 3.7.0 3.6.8 3.6.7 3.6.6 3.6.5 3.6.4 3.6.3 3.6.2 3.6.1 3.0.3 3.0.4 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 All 112 releases
templately / includes / Utils / Log / Logger.php

Logger.php in Templately – Elementor & Gutenberg Template Library: 6500+ Free & Pro Ready Templates And Cloud! 3.8.0, at includes/Utils/Log/Logger.php

93 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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