1414namespace CodeIgniter \Log \Handlers ;
1515
1616use CodeIgniter \HTTP \ResponseInterface ;
17+ use JsonException ;
1718
1819/**
1920 * Allows for logging items to the Chrome console for debugging.
@@ -99,10 +100,11 @@ public function __construct(array $config = [])
99100 * will stop. Any handlers that have not run, yet, will not
100101 * be run.
101102 *
102- * @param string $level
103- * @param string $message
103+ * @param string $level
104+ * @param string $message
105+ * @param array<string, mixed> $context
104106 */
105- public function handle ($ level , $ message ): bool
107+ public function handle ($ level , $ message, array $ context = [] ): bool
106108 {
107109 $ message = $ this ->format ($ message );
108110
@@ -121,7 +123,9 @@ public function handle($level, $message): bool
121123 $ type = $ this ->levels [$ level ];
122124 }
123125
124- $ this ->json ['rows ' ][] = [[$ message ], $ backtraceMessage , $ type ];
126+ $ logArgs = $ context !== [] ? [$ message , $ context ] : [$ message ];
127+
128+ $ this ->json ['rows ' ][] = [$ logArgs , $ backtraceMessage , $ type ];
125129
126130 $ this ->sendLogs ();
127131
@@ -162,8 +166,17 @@ public function sendLogs(?ResponseInterface &$response = null)
162166 $ response = service ('response ' , null , true );
163167 }
164168
169+ try {
170+ $ encoded = json_encode ($ this ->json , JSON_THROW_ON_ERROR );
171+ } catch (JsonException ) {
172+ $ encoded = json_encode ($ this ->json , JSON_PARTIAL_OUTPUT_ON_ERROR );
173+ if ($ encoded === false ) {
174+ return ;
175+ }
176+ }
177+
165178 $ data = base64_encode (
166- mb_convert_encoding (json_encode ( $ this -> json ) , 'UTF-8 ' , mb_list_encodings ()),
179+ mb_convert_encoding ($ encoded , 'UTF-8 ' , mb_list_encodings ()),
167180 );
168181
169182 $ response ->setHeader ($ this ->header , $ data );
0 commit comments