Skip to content

Commit

Permalink
Fix exchanging log categories
Browse files Browse the repository at this point in the history
  • Loading branch information
thekid committed Mar 30, 2022
1 parent ba7e899 commit 2259ca9
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 0 deletions.
4 changes: 4 additions & 0 deletions ChangeLog.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions src/main/php/webservices/rest/Endpoint.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
11 changes: 11 additions & 0 deletions src/main/php/webservices/rest/io/Traced.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
30 changes: 30 additions & 0 deletions src/test/php/webservices/rest/unittest/ExecuteTest.class.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down

0 comments on commit 2259ca9

Please sign in to comment.