Logging cleanup, prune controller stub

This commit is contained in:
2025-09-07 17:22:36 +02:00
parent 511345b02b
commit ea035dd4a1
3 changed files with 46 additions and 5 deletions

View File

@@ -14,13 +14,16 @@ class Configuration
public bool $help = false;
public bool $verbose = false;
public static function build(): Configuration
{
$config = new Configuration();
$opts = getopt("hl:d:u:", [ "help","listen:","data:","udp:"]);
$opts = getopt("hl:d:u:v", [ "help","listen:","data:","udp:"]);
$config->help = isset($opts['h']) || isset($opts['help']);
$config->verbose = isset($opts['v']);
$config->listenAddress = (isset($opts['l']) ? $opts['l'] :
(isset($opts['listen']) ? $opts['listen'] :
(getenv("LOGDB_LISTEN") ?: $config->listenAddress)));

View File

@@ -129,4 +129,37 @@ class LogController
return $deferred->promise();
}
#[Route(path:'/api/logdb/v1/events/prune', methods:["POST"])]
public function pruneEvents(ServerRequestInterface $request)
{
$json = json_decode($request->getBody()->getContent());
// pruning
$maxAge = $json->maxAge ?? null;
// selection
$scope = $json->scope ?? null;
$origin = $json->origin ?? null;
$level = $json->level ?? null;
if (!$maxAge || !($scope || $origin || $level)) {
return Response::json([ 'error' => 'Must specify maxAge or scope|origin|level' ]);
}
$deferred = new Deferred();
$this->commandBus->execute("log.prune", [
'maxAge' => $maxAge,
'filter' => [
'scope' => $scope,
'origin' => $origin,
'level' => $level,
],
])->then(
function ($result) use ($deferred) {
$deferred->resolve(Response::json($result));
}, function (\Throwable $t) use ($deferred) {
$deferred->resolve(Response::json(['error'=>$e->getMessage()])->withStatus(500));
}
);
return $deferred;
}
}

View File

@@ -48,7 +48,8 @@ class LogDbDaemon implements CommandResolverInterface
)
{
$this->logger = new Logger("main");
$this->logger->pushHandler(new class implements HandlerInterface {
$this->logger->pushHandler(new class($this->config->verbose) implements HandlerInterface {
public function __construct(private readonly bool $verbose) {}
public function isHandling(LogRecord $record): bool {
return true;
}
@@ -168,13 +169,17 @@ class LogDbDaemon implements CommandResolverInterface
$users = new UserManager($this->db);
$httpd = new HttpServer(
new LoggingMiddleware($this->logger->withName("http")),
$middleware = [
new ErrorHandlingMiddleware(true),
// new SecurityMiddleware($users),
new RoutingMiddleware($routes),
new RequestHandler()
);
];
if ($this->config->verbose) {
array_unshift($middleware, new LoggingMiddleware($this->logger->withName("http")));
}
$httpd = new HttpServer(...$middleware);
$httpd->listen(new SocketServer($this->config->listenAddress));
$this->logger->info("Listening for HTTP connections on {$this->config->listenAddress}");