From a217db19455a0861c169780bd1bfdd5b0be8dee0 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Wed, 22 Nov 2023 11:48:50 +0100 Subject: [PATCH] Allow log path specification --- Sources/App/Config.swift | 16 ++++++++++++++++ Sources/App/configure.swift | 9 +++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/Sources/App/Config.swift b/Sources/App/Config.swift index 13eb2c6..5b595f9 100644 --- a/Sources/App/Config.swift +++ b/Sources/App/Config.swift @@ -13,6 +13,22 @@ struct Config { /// The authentication tokens to use for monitoring of the service let authenticationTokens: Set + + /// The path to the folder where the metric logs are stored + /// + /// If no path is provided, then a folder `logs` in the resources directory is created + /// If the path is relative, then it is assumed relative to the resources directory + let logPath: String? + + func logURL(possiblyRelativeTo resourcesDirectory: URL) -> URL { + guard let logPath else { + return resourcesDirectory.appendingPathComponent("logs") + } + guard !logPath.hasPrefix("/") else { + return .init(fileURLWithPath: logPath) + } + return resourcesDirectory.appendingPathComponent(logPath) + } } extension Config: Codable { diff --git a/Sources/App/configure.swift b/Sources/App/configure.swift index a175e3b..1c3c5ec 100755 --- a/Sources/App/configure.swift +++ b/Sources/App/configure.swift @@ -24,7 +24,11 @@ enum ServerError: Error { // configures your application public func configure(_ app: Application) throws { let storageFolder = URL(fileURLWithPath: app.directory.resourcesDirectory) - let logFolder = storageFolder.appendingPathComponent("logs") + + let configUrl = storageFolder.appendingPathComponent("config.json") + let config = try Config(loadFrom: configUrl) + + let logFolder = config.logURL(possiblyRelativeTo: storageFolder) let monitor = MetricObserver(logFileFolder: logFolder, logMetricId: "sesame.log") MetricObserver.standard = monitor @@ -34,9 +38,6 @@ public func configure(_ app: Application) throws { _ = try await status.update(.initializing) } - let configUrl = storageFolder.appendingPathComponent("config.json") - let config = try Config(loadFrom: configUrl) - app.http.server.configuration.port = config.port let keyFile = storageFolder.appendingPathComponent(config.keyFileName)