From e3022469e783c65168bb3cd1784c173e37466cc7 Mon Sep 17 00:00:00 2001 From: Christopher Vagnetoft Date: Sat, 17 Jan 2026 18:05:02 +0100 Subject: [PATCH] Implement proper log output --- bin/ntfid | 11 +++++++++++ src/NtfiDaemon.php | 45 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 46 insertions(+), 10 deletions(-) diff --git a/bin/ntfid b/bin/ntfid index ee6985e..7194212 100755 --- a/bin/ntfid +++ b/bin/ntfid @@ -1,6 +1,8 @@ #!/usr/bin/env php setLogger($logger) ->setConfigFile($configFile) ->start(); diff --git a/src/NtfiDaemon.php b/src/NtfiDaemon.php index 1f91e7e..abf4e37 100644 --- a/src/NtfiDaemon.php +++ b/src/NtfiDaemon.php @@ -4,6 +4,8 @@ namespace NoccyLabs\Ntfi; use Psr\Http\Message\ResponseInterface; use Psr\Http\Message\ServerRequestInterface; +use Psr\Log\LoggerInterface; +use Psr\Log\NullLogger; use React\Http\Browser; use React\Http\HttpServer; use React\Http\Message\Response; @@ -24,6 +26,12 @@ class NtfiDaemon private array $channels = []; + private LoggerInterface $logger; + + public function __construct() + { + $this->logger = new NullLogger(); + } /** * Set the configuration file to use * @@ -39,6 +47,12 @@ class NtfiDaemon return $this; } + public function setLogger(LoggerInterface $logger): self + { + $this->logger = $logger; + return $this; + } + /** * Read the configuration, and/or return with defaults. * @@ -79,13 +93,17 @@ class NtfiDaemon $config = $this->readConfig(); $this->servers = $config['servers']; - printf("Configured servers: %s\n", join(", ",array_keys($this->servers))); + $this->logger->debug("Configured servers:"); + foreach ($this->servers as $server => $info) { + $this->logger->debug(sprintf(" %s -> %s", $server, $info['server']??'?')); + } $this->channels = $config['channels']; - printf("Configured channels:\n"); + $this->logger->debug("Configured channels:"); foreach ($this->channels as $channel=>$info) { - printf(" %s -> %s\n", $channel, $info['destination']??'?'); + $this->logger->debug(sprintf(" %s -> %s", $channel, $info['destination']??'?')); } $this->setupHttp($config['listen']); + $this->logger->info("Listening on http://{$config['listen']}"); return $this; } @@ -113,17 +131,24 @@ class NtfiDaemon */ private function onRequest(ServerRequestInterface $request) { + $d = new Deferred(); + $path = trim($request->getUri()->getPath(), '/'); $type = $request->getHeaderLine("content-type"); $body = $request->getBody(); try { - $this->doPublish($path, $type, $body); + $this->doPublish($path, $type, $body)->then( + fn($r) => $d->resolve($r), + fn($t) => $d->reject($t) + ); } catch (\Exception $e) { - return Response::plaintext($e->getMessage())->withStatus(500); + $this->logger->error($e->getMessage(), [ 'file' => $e->getFile(), 'line' => $e->getLine() ]); + $d->reject(new \Throwable("Publish failed")); + // return Response::plaintext($e->getMessage())->withStatus(500); } - return Response::plaintext("OK"); + return $d->promise(); } /** @@ -136,7 +161,7 @@ class NtfiDaemon */ private function doPublish(string $path, string $type, string $body): PromiseInterface { - printf("in: %s (%s) %d bytes\n", $path, $type, strlen($body)); + $this->logger->info(sprintf("request: %s (%s) %d bytes", $path, $type, strlen($body))); foreach ($this->channels as $pattern => $channel) { $re = '/^'.preg_replace('/\{(.+?)\}/', '(?P<$1>[a-zA-Z0-9-_]+)', $pattern).'$/'; // printf("pattern={%s} re={%s}\n", $pattern, $re); @@ -152,6 +177,7 @@ class NtfiDaemon } } return new Promise(function (callable $resolve, callable $reject) use ($path) { + $this->logger->notice("No channel found matching '{$path}'"); $reject(new \Exception("No channel matching '{$path}'")); }); } @@ -168,11 +194,10 @@ class NtfiDaemon */ private function publish(string $connection, string $topic, string $type, string $body): PromiseInterface { - printf("out: %s %s (%s) %d bytes\n", $connection, $topic, $type, strlen($body)); + $this->logger->debug(sprintf("Attempting to publish to topic '%s' on '%s'", $topic, $connection)); $d = new Deferred(); $serverInfo = $this->servers[$connection] ?? throw new \Exception("The server '{$connection}' is not configured"); - $browser = new Browser(); if (isset($serverInfo['token']) && $serverInfo['token']) { @@ -187,7 +212,7 @@ class NtfiDaemon $url = sprintf("https://%s/%s", $serverInfo['server'], $topic); - printf("post: %s\n", $url); + $this->logger->info(sprintf("Posting to URL '%s'", $url)); $browser->post($url, [ 'content-type' => $type, ], $body)->then(