Skip to content

Commit 274bc84

Browse files
committed
Refactor logging API
1 parent ee8fa87 commit 274bc84

16 files changed

+132
-127
lines changed

src/main/php/web/Logging.class.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,15 +58,34 @@ public function tee($sink) {
5858
}
5959

6060
/**
61-
* Writes a log entry
61+
* Writes a HTTP exchange to the log
6262
*
6363
* @param web.Request $response
6464
* @param web.Response $response
6565
* @param [:var] $hints Optional hints
6666
* @return void
6767
*/
68-
public function log($request, $response, $hints= []) {
69-
$this->sink && $this->sink->log($request, $response, $hints);
68+
public function exchange($request, $response, $hints= []) {
69+
if (!$this->sink) return;
70+
71+
$uri= $request->uri()->path();
72+
if ($query= $request->uri()->query()) {
73+
$uri.= '?'.$query;
74+
}
75+
$this->sink->log($response->status(), $request->method(), $uri, $response->trace + $hints);
76+
}
77+
78+
/**
79+
* Writes a log entry
80+
*
81+
* @param string $status
82+
* @param string $method
83+
* @param string $uri
84+
* @param [:var] $hints Optional hints
85+
* @return void
86+
*/
87+
public function log($status, $method, $uri, $hints= []) {
88+
$this->sink && $this->sink->log($status, $method, $uri, $hints);
7089
}
7190

7291
/**

src/main/php/web/logging/Sink.class.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,12 +12,13 @@ abstract class Sink {
1212
/**
1313
* Writes a log entry
1414
*
15-
* @param web.Request $response
16-
* @param web.Response $response
15+
* @param string $status
16+
* @param string $method
17+
* @param string $uri
1718
* @param [:var] $hints Optional hints
1819
* @return void
1920
*/
20-
public abstract function log($request, $response, $hints);
21+
public abstract function log($status, $method, $uri, $hints);
2122

2223
/** @return string */
2324
public function target() { return nameof($this); }

src/main/php/web/logging/ToAllOf.class.php

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,15 @@
33
/**
44
* Log sink which logs to all given sinks
55
*
6-
* @test xp://web.unittest.logging.ToAllOfTest
6+
* @test web.unittest.logging.ToAllOfTest
77
*/
88
class ToAllOf extends Sink {
99
private $sinks= [];
1010

1111
/**
1212
* Creates a sink writing to all given other sinks
1313
*
14-
* @param (web.log.Sink|util.log.LogCategory|function(web.Request, web.Response, string): void)... $arg
14+
* @param (web.log.Sink|util.log.LogCategory|function(string, string, string, [:var]): void)... $arg
1515
*/
1616
public function __construct(... $args) {
1717
foreach ($args as $arg) {
@@ -40,14 +40,15 @@ public function target() {
4040
/**
4141
* Writes a log entry
4242
*
43-
* @param web.Request $response
44-
* @param web.Response $response
43+
* @param string $status
44+
* @param string $method
45+
* @param string $uri
4546
* @param [:var] $hints Optional hints
4647
* @return void
4748
*/
48-
public function log($request, $response, $hints) {
49+
public function log($status, $method, $uri, $hints) {
4950
foreach ($this->sinks as $sink) {
50-
$sink->log($request, $response, $hints);
51+
$sink->log($status, $method, $uri, $hints);
5152
}
5253
}
5354
}

src/main/php/web/logging/ToCategory.class.php

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,19 +14,17 @@ public function target() { return nameof($this).'('.$this->cat->toString().')';
1414
/**
1515
* Writes a log entry
1616
*
17-
* @param web.Request $response
18-
* @param web.Response $response
17+
* @param string $status
18+
* @param string $method
19+
* @param string $uri
1920
* @param [:var] $hints Optional hints
2021
* @return void
2122
*/
22-
public function log($request, $response, $hints) {
23-
$query= $request->uri()->query();
24-
$uri= $request->uri()->path().($query ? '?'.$query : '');
25-
23+
public function log($status, $method, $uri, $hints) {
2624
if ($hints) {
27-
$this->cat->warn($response->status(), $request->method(), $uri, $hints);
25+
$this->cat->warn($status, $method, $uri, $hints);
2826
} else {
29-
$this->cat->info($response->status(), $request->method(), $uri);
27+
$this->cat->info($status, $method, $uri);
3028
}
3129
}
3230
}

src/main/php/web/logging/ToConsole.class.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -8,26 +8,26 @@ class ToConsole extends Sink {
88
/**
99
* Writes a log entry
1010
*
11-
* @param web.Request $response
12-
* @param web.Response $response
11+
* @param string $status
12+
* @param string $method
13+
* @param string $uri
1314
* @param [:var] $hints Optional hints
1415
* @return void
1516
*/
16-
public function log($request, $response, $hints) {
17-
$query= $request->uri()->query();
17+
public function log($status, $method, $uri, $hints) {
1818
$hint= '';
1919
foreach ($hints as $kind => $value) {
2020
$hint.= ', '.$kind.': '.(is_string($value) ? $value : Objects::stringOf($value));
2121
}
2222

2323
Console::writeLinef(
24-
" \e[33m[%s %d %.3fkB]\e[0m %d %s %s%s",
24+
" \e[33m[%s %d %.3fkB]\e[0m %s %s %s%s",
2525
date('Y-m-d H:i:s'),
2626
getmypid(),
2727
memory_get_usage() / 1024,
28-
$response->status(),
29-
$request->method(),
30-
$request->uri()->path().($query ? '?'.$query : ''),
28+
$status,
29+
$method,
30+
$uri,
3131
$hint ? " \e[2m[".substr($hint, 2)."]\e[0m" : ''
3232
);
3333
}

src/main/php/web/logging/ToFile.class.php

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,26 @@ public function target() { return nameof($this).'('.$this->file.')'; }
2828
/**
2929
* Writes a log entry
3030
*
31-
* @param web.Request $response
32-
* @param web.Response $response
31+
* @param string $status
32+
* @param string $method
33+
* @param string $uri
3334
* @param [:var] $hints Optional hints
3435
* @return void
3536
*/
36-
public function log($request, $response, $hints) {
37-
$query= $request->uri()->query();
37+
public function log($status, $method, $uri, $hints) {
3838
$hint= '';
3939
foreach ($hints as $kind => $value) {
4040
$hint.= ', '.$kind.': '.(is_string($value) ? $value : Objects::stringOf($value));
4141
}
4242

4343
$line= sprintf(
44-
"[%s %d %.3fkB] %d %s %s%s\n",
44+
"[%s %d %.3fkB] %s %s %s%s\n",
4545
date('Y-m-d H:i:s'),
4646
getmypid(),
4747
memory_get_usage() / 1024,
48-
$response->status(),
49-
$request->method(),
50-
$request->uri()->path().($query ? '?'.$query : ''),
48+
$status,
49+
$method,
50+
$uri,
5151
$hint ? ' ['.substr($hint, 2).']' : ''
5252
);
5353
file_put_contents($this->file, $line, FILE_APPEND | LOCK_EX);

src/main/php/web/logging/ToFunction.class.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,18 +5,19 @@ class ToFunction extends Sink {
55

66
/** @param callable $function */
77
public function __construct($function) {
8-
$this->function= cast($function, 'function(web.Request, web.Response, [:var]): void');
8+
$this->function= cast($function, 'function(string, string, string, [:var]): void');
99
}
1010

1111
/**
1212
* Writes a log entry
1313
*
14-
* @param web.Request $response
15-
* @param web.Response $response
14+
* @param string $status
15+
* @param string $method
16+
* @param string $uri
1617
* @param [:var] $hints Optional hints
1718
* @return void
1819
*/
19-
public function log($request, $response, $hints) {
20-
$this->function->__invoke($request, $response, $hints);
20+
public function log($status, $method, $uri, $hints) {
21+
$this->function->__invoke($status, $method, $uri, $hints);
2122
}
2223
}

src/main/php/xp/web/srv/HttpProtocol.class.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ private function sendError($request, $response, $error) {
5353
break;
5454
}
5555
}
56-
$this->logging->log($request, $response, $response->trace + ['error' => $error]);
56+
$this->logging->exchange($request, $response, ['error' => $error]);
5757
}
5858

5959
/**
@@ -113,9 +113,9 @@ public function handleData($socket) {
113113
$close= true;
114114
}
115115

116-
$this->logging->log($request, $response, $response->trace);
116+
$this->logging->exchange($request, $response);
117117
} catch (CannotWrite $e) {
118-
$this->logging->log($request, $response, $response->trace + ['warn' => $e]);
118+
$this->logging->exchange($request, $response, ['warn' => $e]);
119119
} catch (Error $e) {
120120
$this->sendError($request, $response, $e);
121121
} catch (Throwable $e) {

src/main/php/xp/web/srv/WsProtocol.class.php

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -50,26 +50,16 @@ public function handleData($socket) {
5050
try {
5151
if (Opcodes::CLOSE === $type) {
5252
$conn->close();
53-
$hint= 'status='.unpack('n', $message)[1];
53+
$hints= unpack('nstatus/a*reason', $message);
5454
} else {
5555
yield from $conn->on($message) ?? [];
56-
$hint= '';
56+
$hints= [];
5757
}
5858
} catch (Any $e) {
59-
$hint= Throwable::wrap($e)->compoundMessage();
59+
$hint= ['error' => Throwable::wrap($e)];
6060
}
6161

62-
// TODO: Use logging facility
63-
// $this->logging->log($request, $response);
64-
\util\cmd\Console::writeLinef(
65-
" \e[33m[%s %d %.3fkB]\e[0m WS %s %s%s",
66-
date('Y-m-d H:i:s'),
67-
getmypid(),
68-
memory_get_usage() / 1024,
69-
Opcodes::nameOf($type),
70-
$conn->path(),
71-
$hint ? " \e[2m[{$hint}]\e[0m" : ''
72-
);
62+
$this->logging->log('WS', Opcodes::nameOf($type), $conn->path(), $hints);
7363
}
7464
}
7565

src/test/php/web/unittest/HttpProtocolTest.class.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,15 @@
22

33
use io\streams\Streams;
44
use peer\SocketException;
5-
use test\{Assert, AssertionFailed, Test, Values};
5+
use test\{Assert, Before, AssertionFailed, Test, Values};
66
use web\{Application, Environment, Logging};
77
use xp\web\srv\{CannotWrite, HttpProtocol};
88

99
class HttpProtocolTest {
1010
private $log;
1111

12-
public function __construct() {
12+
#[Before]
13+
public function log() {
1314
$this->log= new Logging(null);
1415
}
1516

@@ -155,7 +156,7 @@ public function catches_write_errors_and_logs_them_as_warning() {
155156
throw new CannotWrite('Test error', new SocketException('...'));
156157
});
157158
}),
158-
Logging::of(function($req, $res, $hints) use(&$caught) { $caught= $hints['warn']; })
159+
Logging::of(function($status, $method, $uri, $hints) use(&$caught) { $caught= $hints['warn']; })
159160
);
160161

161162
$this->assertHttp(
@@ -177,7 +178,7 @@ public function response_trace_appears_in_log() {
177178
$this->application(function($req, $res) {
178179
$res->trace('request-time-ms', 1);
179180
}),
180-
Logging::of(function($req, $res, $hints) use(&$logged) { $logged= $hints; })
181+
Logging::of(function($status, $method, $uri, $hints) use(&$logged) { $logged= $hints; })
181182
);
182183

183184
$this->handle($p, ["GET / HTTP/1.1\r\n\r\n"]);

0 commit comments

Comments
 (0)