1515 * @author Olivier Paroz <github@oparoz.com>
1616 * @author Robin Appelman <robin@icewind.nl>
1717 * @author Roeland Jago Douma <roeland@famdouma.nl>
18+ * @author Thomas Citharel <nextcloud@tcit.fr>
1819 * @author Thomas Müller <thomas.mueller@tmit.eu>
1920 * @author Victor Dubiniuk <dubiniuk@owncloud.com>
2021 *
@@ -207,11 +208,11 @@ public function log(int $level, string $message, array $context = []) {
207208 array_walk ($ context , [$ this ->normalizer , 'format ' ]);
208209
209210 $ app = $ context ['app ' ] ?? 'no app in context ' ;
210- $ message = $ this ->interpolateMessage ($ context , $ message );
211+ $ entry = $ this ->interpolateMessage ($ context , $ message );
211212
212213 try {
213214 if ($ level >= $ minLevel ) {
214- $ this ->writeLog ($ app , $ message , $ level );
215+ $ this ->writeLog ($ app , $ entry , $ level );
215216
216217 if ($ this ->crashReporters !== null ) {
217218 $ messageContext = array_merge (
@@ -220,11 +221,11 @@ public function log(int $level, string $message, array $context = []) {
220221 'level ' => $ level
221222 ]
222223 );
223- $ this ->crashReporters ->delegateMessage ($ message , $ messageContext );
224+ $ this ->crashReporters ->delegateMessage ($ entry [ ' message ' ] , $ messageContext );
224225 }
225226 } else {
226227 if ($ this ->crashReporters !== null ) {
227- $ this ->crashReporters ->delegateBreadcrumb ($ message , 'log ' , $ context );
228+ $ this ->crashReporters ->delegateBreadcrumb ($ entry [ ' message ' ] , 'log ' , $ context );
228229 }
229230 }
230231 } catch (\Throwable $ e ) {
@@ -309,8 +310,11 @@ public function logException(\Throwable $exception, array $context = []) {
309310 $ level = $ context ['level ' ] ?? ILogger::ERROR ;
310311
311312 $ serializer = new ExceptionSerializer ($ this ->config );
312- $ data = $ serializer ->serializeException ($ exception );
313- $ data ['CustomMessage ' ] = $ this ->interpolateMessage ($ context , $ context ['message ' ] ?? '-- ' );
313+ $ data = $ context ;
314+ unset($ data ['app ' ]);
315+ unset($ data ['level ' ]);
316+ $ data = array_merge ($ serializer ->serializeException ($ exception ), $ data );
317+ $ data = $ this ->interpolateMessage ($ data , $ context ['message ' ] ?? '-- ' , 'CustomMessage ' );
314318
315319 $ minLevel = $ this ->getLogLevel ($ context );
316320
@@ -375,16 +379,20 @@ public function getLogPath():string {
375379 /**
376380 * Interpolate $message as defined in PSR-3
377381 *
378- * @param array $context
379- * @param string $message
380- *
381- * @return string
382+ * Returns an array containing the context without the interpolated
383+ * parameters placeholders and the message as the 'message' - or
384+ * user-defined - key.
382385 */
383- private function interpolateMessage (array $ context , string $ message ): string {
386+ private function interpolateMessage (array $ context , string $ message, string $ messageKey = ' message ' ): array {
384387 $ replace = [];
388+ $ usedContextKeys = [];
385389 foreach ($ context as $ key => $ val ) {
386- $ replace ['{ ' . $ key . '} ' ] = $ val ;
390+ $ fullKey = '{ ' . $ key . '} ' ;
391+ $ replace [$ fullKey ] = $ val ;
392+ if (strpos ($ message , $ fullKey ) !== false ) {
393+ $ usedContextKeys [$ key ] = true ;
394+ }
387395 }
388- return strtr ($ message , $ replace );
396+ return array_merge ( array_diff_key ( $ context , $ usedContextKeys ), [ $ messageKey => strtr ($ message , $ replace)] );
389397 }
390398}
0 commit comments