diff --git a/ChangeLog.md b/ChangeLog.md index 765a668..28215b6 100644 --- a/ChangeLog.md +++ b/ChangeLog.md @@ -3,6 +3,10 @@ Rest client change log ## ?.?.? / ????-??-?? +## 5.0.2 / 2022-03-30 + +* Fixed exchanging log categories - @thekid + ## 5.0.1 / 2022-03-09 * Merged PR #23: Fix missing port - @thekid diff --git a/src/main/php/webservices/rest/Endpoint.class.php b/src/main/php/webservices/rest/Endpoint.class.php index 807bdfe..e3a6366 100755 --- a/src/main/php/webservices/rest/Endpoint.class.php +++ b/src/main/php/webservices/rest/Endpoint.class.php @@ -170,6 +170,8 @@ public function resource($path, $segments= []) { public function setTrace($cat) { if (null === $cat) { $this->transfer= $this->transfer->untraced(); + } else if ($this->transfer instanceof Traced) { + $this->transfer->use($cat); } else { $this->transfer= new Traced($this->transfer, $cat); } diff --git a/src/main/php/webservices/rest/io/Traced.class.php b/src/main/php/webservices/rest/io/Traced.class.php index a4ec29d..54484ba 100755 --- a/src/main/php/webservices/rest/io/Traced.class.php +++ b/src/main/php/webservices/rest/io/Traced.class.php @@ -19,6 +19,17 @@ public function __construct($untraced, $cat) { /** @return parent */ public function untraced() { return $this->untraced; } + /** + * Use another logging category + * + * @param util.log.LogCategory $cat + * @return self + */ + public function use($cat) { + $this->cat= $cat; + return $this; + } + public function transmission($conn, $s, $target) { return new class($conn, $s, $target, $this->cat) extends Transmission { private $cat; diff --git a/src/test/php/webservices/rest/unittest/ExecuteTest.class.php b/src/test/php/webservices/rest/unittest/ExecuteTest.class.php index 73b0cee..d832e65 100755 --- a/src/test/php/webservices/rest/unittest/ExecuteTest.class.php +++ b/src/test/php/webservices/rest/unittest/ExecuteTest.class.php @@ -287,6 +287,36 @@ public function file_contents_not_logged_during_file_upload() { Assert::equals($expected, $appender->lines); } + #[Test] + public function exchange_logging() { + $appender= $this->newAppender(); + $fixture= $this->newFixture(); + + // Use first logger category + $fixture->setTrace(Logging::all()->using(new PatternLayout('%L %m'))->to($appender)); + $fixture->resource('/users/0')->get(); + + // Exchange logging + $fixture->setTrace(Logging::all()->using(new PatternLayout('[%L] %m'))->to($appender)); + $fixture->resource('/users/0')->get(); + + $request= implode("\r\n", [ + 'GET /users/0 HTTP/1.1', + 'Connection: close', + 'Host: test', + '', + ]); + $expected= [ + "INFO >>> {$request}", + "INFO <<< HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 56\r\n", + "DEBUG {$request}\r\n", + "[INFO] >>> {$request}", + "[INFO] <<< HTTP/1.1 200 OK\r\nContent-Type: text/plain\r\nContent-Length: 56\r\n", + "[DEBUG] {$request}\r\n", + ]; + Assert::equals($expected, $appender->lines); + } + #[Test, Expect(RestException::class)] public function exceptions_from_sending_requests_are_wrapped() { $fixture= (new Endpoint('http://test'))->connecting(function($uri) {