@@ -58,7 +58,8 @@ public function __construct(
5858 */
5959 public function execute (): void {
6060 $ this ->logger ->info (
61- sprintf ( 'Agentic Sync Job %s: Started ' , $ this ->batch_id )
61+ '[Sync] Started ' ,
62+ $ this ->build_log_context ()
6263 );
6364
6465 // Transform products into DTOs.
@@ -75,7 +76,8 @@ public function execute(): void {
7576
7677 if ( empty ( $ products ) ) {
7778 $ this ->logger ->info (
78- sprintf ( 'Agentic Sync Job %s: No products ' , $ this ->batch_id )
79+ '[Sync] No products ' ,
80+ $ this ->build_log_context ()
7981 );
8082
8183 $ this ->fire_completed_action ( 'empty ' , 0 , 0 , 0 );
@@ -104,7 +106,7 @@ public function execute(): void {
104106 )
105107 );
106108
107- $ this ->logger ->debug ( " Start Sync { $ this -> batch_id } ... " , $ body );
109+ $ this ->logger ->debug ( ' [ Sync] Started ...' , $ this -> build_log_context ( $ body ) );
108110
109111 if ( is_wp_error ( $ response ) ) {
110112 // Log the error message and throw an Exception.
@@ -195,11 +197,10 @@ private function handle_successful_response( string $response_body, array $paylo
195197
196198 $ this ->logger ->info (
197199 sprintf (
198- 'Agentic Sync Job %s: Successfully synced %d products ' ,
199- $ this ->batch_id ,
200+ '[Sync] Successfully synced %d products ' ,
200201 count ( $ this ->product_ids )
201202 ),
202- $ response_data
203+ $ this -> build_log_context ( $ response_data )
203204 );
204205
205206 $ contains_errors = false === ( $ response_data ['success ' ] ?? false );
@@ -279,11 +280,8 @@ private function extract_product_errors( string $error_message, array $payload_p
279280 */
280281 private function mark_products_by_validation_result ( array $ validation_errors ): void {
281282 $ this ->logger ->warning (
282- sprintf (
283- 'Agentic Sync Job %s: Validation errors ' ,
284- $ this ->batch_id
285- ),
286- $ validation_errors
283+ '[Sync] Validation errors ' ,
284+ $ this ->build_log_context ( $ validation_errors )
287285 );
288286
289287 foreach ( $ validation_errors as $ product_id => $ error_message ) {
@@ -304,10 +302,12 @@ private function mark_products_by_validation_result( array $validation_errors ):
304302 */
305303 private function handle_api_error ( array $ product_ids , string $ error_message ): void {
306304 $ this ->logger ->warning (
307- sprintf ( 'Agentic Sync Job %s: API Error - %s ' , $ this ->batch_id , $ error_message ),
308- array (
309- 'product_count ' => count ( $ product_ids ),
310- 'product_ids ' => $ product_ids ,
305+ sprintf ( '[Sync] API Error - %s ' , $ error_message ),
306+ $ this ->build_log_context (
307+ array (
308+ 'product_count ' => count ( $ product_ids ),
309+ 'product_ids ' => $ product_ids ,
310+ )
311311 )
312312 );
313313
@@ -384,4 +384,22 @@ private function mark_products_synced( array $product_ids ): void {
384384 $ this ->mark_product_synced ( $ product_id );
385385 }
386386 }
387+
388+ /**
389+ * @param mixed $context The log data - an array or any other value to log.
390+ * @return array
391+ */
392+ private function build_log_context ( $ context = array () ): array {
393+ $ log_context = array ( 'job ' => $ this ->batch_id );
394+
395+ if ( ! is_array ( $ context ) ) {
396+ if ( null === $ context ) {
397+ $ context = array ();
398+ } else {
399+ $ context = array ( 'raw ' => $ context );
400+ }
401+ }
402+
403+ return array_merge ( $ log_context , $ context );
404+ }
387405}
0 commit comments