@@ -146,19 +146,27 @@ public function requestSent(): bool {
146
146
protected function getRequestLogContent (): string {
147
147
return $ this ->client ->__toString ();
148
148
}
149
+ protected function getResponseLogContent (Swlib \Saber \Response $ response ): string {
150
+ return $ response ->__toString ();
151
+ }
149
152
protected function recv (): \Swlib \Saber \Response {
150
153
if (! $ this ->request_sent ) {
151
154
throw new \Exception ('Request not sent ' );
152
155
}
153
156
$ client = $ this ->client ;
154
- $ log_string = date ('[Y-m-d H:i:s] ' , $ this ->send_time ) . "----------Request---------- \n" ;
155
- $ log_string .= $ this ->getRequestLogContent () . "\n" ;
157
+ $ log_buffer = \Swlib \Http \stream_for ('' );
158
+ $ log_buffer ->write (date ('[Y-m-d H:i:s] ' , $ this ->send_time ));
159
+ $ log_buffer ->write ("----------Request---------- \n" );
160
+ $ log_buffer ->write ($ this ->getRequestLogContent ());
161
+ $ log_buffer ->write ("\n" );
156
162
try {
157
163
$ response = $ client ->recv ();
158
164
$ code = $ response ->statusCode ;
159
- $ log_string .= date ('[Y-m-d H:i:s] ' , \Time \now ()) . "----------Response--------- \n" ;
160
- $ log_string .= $ response ->__toString () . "\n\n" ;
161
- $ this ->writeLog ($ log_string );
165
+ $ log_buffer ->write (date ('[Y-m-d H:i:s] ' , \Time \now ()));
166
+ $ log_buffer ->write ("----------Response--------- \n" );
167
+ $ log_buffer ->write ($ this ->getResponseLogContent ($ response ));
168
+ $ log_buffer ->write ("\n\n" );
169
+ $ this ->writeLogWithStream ($ log_buffer );
162
170
if ($ code !== 200 && ! $ this ->handleHttpErrorCode ($ code )) {
163
171
$ name = $ this ->exception_name ['error ' ];
164
172
if (! isset ($ name ) || $ name === '' ) {
@@ -170,8 +178,9 @@ protected function recv(): \Swlib\Saber\Response {
170
178
} catch (\Swlib \Http \Exception \ConnectException $ e ) {
171
179
$ code = $ e ->getCode ();
172
180
$ error = $ e ->getMessage ();
173
- $ log_string .= "----Response: $ error---- \n\n" ;
174
- $ this ->writeLog ($ log_string );
181
+ $ log_buffer ->write (date ('[Y-m-d H:i:s] ' , \Time \now ()));
182
+ $ log_buffer ->write ("----Response: $ error---- \n\n" );
183
+ $ this ->writeLogWithStream ($ log_buffer );
175
184
if ($ code === -1 || $ code === -2 ) {
176
185
$ name = $ this ->exception_name ['timeout ' ];
177
186
if (isset ($ name )) {
@@ -198,6 +207,24 @@ protected function getLogDir(): string {
198
207
}
199
208
return $ dir ;
200
209
}
210
+ protected function writeLogWithStream (Psr \Http \Message \StreamInterface $ log_stream ): void {
211
+ $ log_stream ->seek (0 );
212
+ $ log_string = $ log_stream ->getContents ();
213
+ if (self ::$ _echo_log_on && \Swango \Environment::getWorkingMode ()->isInCliScript ()) {
214
+ if (strlen ($ log_string ) > 4096 ) {
215
+ echo substr ($ log_string , 0 , 4096 ) . "\n\n" ;
216
+ } else {
217
+ echo $ log_string ;
218
+ }
219
+ }
220
+ $ dir = $ this ->getLogDir ();
221
+ if (! is_dir ($ dir )) {
222
+ mkdir ($ dir , 0777 , true );
223
+ }
224
+ $ fp = fopen ($ dir . '/ ' . date ('Y-m-d ' ) . '.log ' , 'a ' );
225
+ fwrite ($ fp , $ log_string );
226
+ fclose ($ fp );
227
+ }
201
228
protected function writeLog (string &$ log_string ): void {
202
229
if (self ::$ _echo_log_on && \Swango \Environment::getWorkingMode ()->isInCliScript ()) {
203
230
if (strlen ($ log_string ) > 4096 ) {
0 commit comments