1212use Throwable ;
1313use function React \Promise \reject ;
1414use function React \Promise \resolve ;
15+ use function WyriHaximus \getIn ;
1516
1617class LoggerMiddleware implements MiddlewareInterface
1718{
18- private const REQUEST = 'request ' ;
19- private const RESPONSE = 'response ' ;
20- private const ERROR = 'error ' ;
19+ private const REQUEST = 'request ' ;
20+ private const RESPONSE = 'response ' ;
21+ private const ERROR = 'error ' ;
22+
23+ private const MESSAGE_URL = '[{{transaction_id}}] Requesting: {{request.uri}} ' ;
24+ private const MESSAGE_SUCCESSFUL = '[{{transaction_id}}] Request completed with {{response.status_code}} ' ;
25+ private const MESSAGE_ERROR = '[{{transaction_id}}] {{error.message}} ' ;
2126
2227 /**
2328 * @var LoggerInterface
@@ -43,6 +48,10 @@ public function pre(
4348 return resolve ($ request );
4449 }
4550
51+ $ this ->context [$ transactionId ] = [
52+ 'transaction_id ' => $ transactionId ,
53+ self ::REQUEST => [],
54+ ];
4655 $ this ->context [$ transactionId ][self ::REQUEST ]['method ' ] = $ request ->getMethod ();
4756 $ this ->context [$ transactionId ][self ::REQUEST ]['uri ' ] = (string )$ this ->stripQueryItems (
4857 $ request ->getUri (),
@@ -61,7 +70,10 @@ public function pre(
6170 return resolve ($ request );
6271 }
6372
64- $ message = 'Requesting: ' . $ this ->context [$ transactionId ][self ::REQUEST ]['uri ' ];
73+ $ message = $ this ->renderTemplate (
74+ $ options [self ::class][Options::MESSAGE_PRE ] ?? self ::MESSAGE_URL ,
75+ $ this ->context [$ transactionId ]
76+ );
6577 $ this ->logger ->log ($ options [self ::class][Options::URL_LEVEL ], $ message , $ this ->context [$ transactionId ]);
6678
6779 return resolve ($ request );
@@ -88,10 +100,11 @@ public function post(
88100 return resolve ($ response );
89101 }
90102
91- $ message = 'Request ' . $ transactionId . ' completed. ' ;
92-
93103 $ context = $ this ->addResponseToContext ($ context , $ response , $ options );
94-
104+ $ message = $ this ->renderTemplate (
105+ $ options [self ::class][Options::MESSAGE_POST ] ?? self ::MESSAGE_SUCCESSFUL ,
106+ $ context
107+ );
95108 $ this ->logger ->log ($ options [self ::class][Options::LEVEL ], $ message , $ context );
96109
97110 return resolve ($ response );
@@ -116,8 +129,6 @@ public function error(
116129 return reject ($ throwable );
117130 }
118131
119- $ message = $ throwable ->getMessage ();
120-
121132 $ response = null ;
122133 if (method_exists ($ throwable , 'getResponse ' )) {
123134 $ response = $ throwable ->getResponse ();
@@ -126,6 +137,7 @@ public function error(
126137 $ context = $ this ->addResponseToContext ($ context , $ response , $ options );
127138 }
128139
140+ $ context [self ::ERROR ]['message ' ] = $ throwable ->getMessage ();
129141 $ context [self ::ERROR ]['code ' ] = $ throwable ->getCode ();
130142 $ context [self ::ERROR ]['file ' ] = $ throwable ->getFile ();
131143 $ context [self ::ERROR ]['line ' ] = $ throwable ->getLine ();
@@ -135,6 +147,10 @@ public function error(
135147 $ context [self ::ERROR ]['context ' ] = $ throwable ->getContext ();
136148 }
137149
150+ $ message = $ this ->renderTemplate (
151+ $ options [self ::class][Options::MESSAGE_ERROR ] ?? self ::MESSAGE_ERROR ,
152+ $ context
153+ );
138154 $ this ->logger ->log ($ options [self ::class][Options::ERROR_LEVEL ], $ message , $ context );
139155
140156 return reject ($ throwable );
@@ -185,4 +201,16 @@ private function stripQueryItems(UriInterface $uri, array $options): UriInterfac
185201
186202 return $ uri ->withQuery (http_build_query ($ query ));
187203 }
204+
205+ private function renderTemplate (string $ template , array $ context ): string
206+ {
207+ $ keyValues = [];
208+ preg_match_all ("|\{\{(.*)\}\}|U " , $ template , $ out , PREG_PATTERN_ORDER );
209+ foreach (array_unique (array_values ($ out [1 ])) as $ placeHolder ) {
210+ $ keyValues ['{{ ' . $ placeHolder . '}} ' ] = getIn ($ context , $ placeHolder , '' );
211+ }
212+ $ template = str_replace (array_keys ($ keyValues ), array_values ($ keyValues ), $ template );
213+
214+ return $ template ;
215+ }
188216}
0 commit comments