• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

wp-graphql / wp-graphql / 15710056976

17 Jun 2025 02:27PM UTC coverage: 84.17% (-0.1%) from 84.287%
15710056976

push

github

actions-user
release: merge develop into master for v2.3.3

15925 of 18920 relevant lines covered (84.17%)

258.66 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

94.87
/src/Utils/DebugLog.php
1
<?php
2
namespace WPGraphQL\Utils;
3

4
/**
5
 * Class DebugLog
6
 *
7
 * @package WPGraphQL\Utils
8
 */
9
class DebugLog {
10

11
        /**
12
         * The log items.
13
         *
14
         * @var array<string,mixed>[]
15
         */
16
        protected $logs;
17

18
        /**
19
         * Whether logs are enabled
20
         *
21
         * @var bool
22
         */
23
        protected $logs_enabled;
24

25
        /**
26
         * DebugLog constructor.
27
         */
28
        public function __construct() {
760✔
29

30
                // Instantiate array to start capturing logs
31
                $this->logs = [];
760✔
32

33
                // Whether WPGraphQL Debug is enabled
34
                $enabled = \WPGraphQL::debug();
760✔
35

36
                /**
37
                 * Filters whether GraphQL Debug is enabled enabled. Serves as the default state for enabling debug logs.
38
                 *
39
                 * @param bool $enabled Whether logs are enabled or not
40
                 * @param \WPGraphQL\Utils\DebugLog $debug_log The DebugLog class instance
41
                 */
42
                $this->logs_enabled = apply_filters( 'graphql_debug_logs_enabled', $enabled, $this );
760✔
43
        }
44

45
        /**
46
         * Given a message and a config, a log entry is added to the log
47
         *
48
         * @template TMessage of mixed|string|mixed[]
49
         * @template TConfig of array<string,mixed>
50
         *
51
         * @param TMessage $message The debug log message
52
         * @param TConfig  $config Config for the debug log. Set type and any additional information to log
53
         *
54
         * @return array<string,array{
55
         *  type:string,
56
         *  message:TMessage,
57
         * }>
58
         */
59
        public function add_log_entry( $message, $config = [] ) {
27✔
60
                if ( empty( $message ) ) {
27✔
61
                        return [];
×
62
                }
63

64
                $type = 'GRAPHQL_DEBUG';
27✔
65

66
                if ( ! is_array( $config ) ) {
27✔
67
                        $config = [];
×
68
                }
69

70
                if ( isset( $config['type'] ) ) {
27✔
71
                        unset( $config['message'] );
7✔
72
                }
73

74
                if ( isset( $config['backtrace'] ) ) {
27✔
75
                        $config['stack'] = $config['backtrace'];
27✔
76
                        unset( $config['backtrace'] );
27✔
77
                }
78

79
                if ( isset( $config['type'] ) ) {
27✔
80
                        $type = $config['type'];
7✔
81
                        unset( $config['type'] );
7✔
82
                }
83

84
                if ( ! isset( $this->logs[ wp_json_encode( $message ) ] ) ) {
27✔
85
                        $log_entry = array_merge(
27✔
86
                                [
27✔
87
                                        'type'    => $type,
27✔
88
                                        'message' => $message,
27✔
89
                                ],
27✔
90
                                $config
27✔
91
                        );
27✔
92

93
                        $this->logs[ wp_json_encode( $message ) ] = $log_entry;
27✔
94

95
                        /**
96
                         * Filter the log entry for the debug log
97
                         *
98
                         * @param array<string,mixed> $log    The log entry
99
                         * @param array<string,mixed> $config The config passed in with the log entry
100
                         */
101
                        return apply_filters( 'graphql_debug_log_entry', $log_entry, $config );
27✔
102
                }
103

104
                return [];
1✔
105
        }
106

107
        /**
108
         * Returns the debug log
109
         *
110
         * @return array<string,mixed>[]
111
         */
112
        public function get_logs() {
753✔
113

114
                /**
115
                 * Init the debug logger
116
                 *
117
                 * @param \WPGraphQL\Utils\DebugLog $instance The DebugLog instance
118
                 */
119
                do_action( 'graphql_get_debug_log', $this );
753✔
120

121
                // If GRAPHQL_DEBUG is not enabled on the server, set a default message
122
                if ( ! $this->logs_enabled ) {
753✔
123
                        $this->logs = [
2✔
124
                                [
2✔
125
                                        'type'    => 'DEBUG_LOGS_INACTIVE',
2✔
126
                                        'message' => __( 'GraphQL Debug logging is not active. To see debug logs, GRAPHQL_DEBUG must be enabled.', 'wp-graphql' ),
2✔
127
                                ],
2✔
128
                        ];
2✔
129
                }
130

131
                /**
132
                 * Return the filtered debug log
133
                 *
134
                 * @param array<string,mixed>[]     $logs     The logs to be output with the request
135
                 * @param \WPGraphQL\Utils\DebugLog $instance The Debug Log class
136
                 */
137
                return apply_filters( 'graphql_debug_log', array_values( $this->logs ), $this );
753✔
138
        }
139
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc