From e4f2fc547bc884d20be2ffa2f3bffece995bae65 Mon Sep 17 00:00:00 2001 From: Christoph Hagen Date: Mon, 6 Feb 2023 21:31:12 +0100 Subject: [PATCH] Improve logging --- Package.swift | 2 +- Resources/example_config.json | 2 +- Sources/App/CapServer+Routes.swift | 36 +++++++++++++++--------------- Sources/App/Config.swift | 2 +- Sources/Run/main.swift | 2 +- 5 files changed, 22 insertions(+), 22 deletions(-) diff --git a/Package.swift b/Package.swift index 42cb4b5..f07c131 100755 --- a/Package.swift +++ b/Package.swift @@ -8,7 +8,7 @@ let package = Package( ], dependencies: [ .package(url: "https://github.com/vapor/vapor", from: "4.0.0"), - .package(url: "https://github.com/christophhagen/Clairvoyant", branch: "main"), + .package(url: "https://github.com/christophhagen/Clairvoyant", from: "0.4.0"), ], targets: [ .target(name: "App", diff --git a/Resources/example_config.json b/Resources/example_config.json index 8f362e3..2a4e0f4 100644 --- a/Resources/example_config.json +++ b/Resources/example_config.json @@ -1,7 +1,7 @@ { "port" : 6001, "maxBodySize" : "2mb", - "logPath": "\/var\/log\/caps.log", + "logPath": "\/var\/log\/caps/metrics", "serveFiles": true, "writers" : [ "auth_key_1" diff --git a/Sources/App/CapServer+Routes.swift b/Sources/App/CapServer+Routes.swift index 05aca13..96d4249 100755 --- a/Sources/App/CapServer+Routes.swift +++ b/Sources/App/CapServer+Routes.swift @@ -29,26 +29,26 @@ extension CapServer { app.postCatching("cap") { request in try self.ensureOperability() try authenticator.authorize(request) - let data = try request.getBodyData() + let data = try request.getBodyData(request: "/cap") let cap = try decoder.decode(Cap.self, from: data) try self.addOrUpdate(cap) } // Upload an image - app.postCatching("images", ":n") { request in + app.postCatching("images", ":cap") { request in try self.ensureOperability() try authenticator.authorize(request) guard let cap = request.parameters.get("n", as: Int.self) else { - log("Invalid parameter for cap") + log("/images/:cap: Invalid 'cap' parameter") throw Abort(.badRequest) } - let data = try request.getBodyData() + let data = try request.getBodyData(request: "/images/:cap") try self.save(image: data, for: cap) } // Update the classifier app.on(.POST, "classifier", ":version", body: .collect(maxSize: "50mb")) { request -> HTTPStatus in guard let version = request.parameters.get("version", as: Int.self) else { - log("Invalid parameter for version") + log("/classifier/:version: Invalid parameter for version") throw Abort(.badRequest) } guard version > self.classifierVersion else { @@ -57,7 +57,7 @@ extension CapServer { try self.ensureOperability() try authenticator.authorize(request) - let data = try request.getBodyData() + let data = try request.getBodyData(request: "/classifier/:version") try self.save(classifier: data, version: version) return .ok } @@ -65,17 +65,17 @@ extension CapServer { // Update the trained classes app.postCatching("classes", ":date") { request in guard let dateString = request.parameters.get("date") else { - log("Invalid parameter for date") + log("/classes/:date: Invalid 'date' parameter") throw Abort(.badRequest) } guard let date = dateFormatter.date(from: dateString) else { - log("Invalid date specification") + log("/classes/:date: Invalid 'date' specification") throw Abort(.badRequest) } try self.ensureOperability() try authenticator.authorize(request) - let body = try request.getStringBody() + let body = try request.getStringBody(request: "/classes/:date") self.updateTrainedClasses(content: body) self.removeAllEntriesInImageChangeList(before: date) @@ -91,23 +91,23 @@ extension CapServer { // Upload the thumbnail of a cap app.postCatching("thumbnails", ":cap") { request in guard let cap = request.parameters.get("cap", as: Int.self) else { - log("Invalid cap parameter for thumbnail upload") + log("/thumbnails/:cap: Invalid cap parameter for thumbnail upload") throw Abort(.badRequest) } try self.ensureOperability() try authenticator.authorize(request) - let data = try request.getBodyData() + let data = try request.getBodyData(request: "/thumbnails/:cap") self.saveThumbnail(data, for: cap) } // Delete the image of a cap app.postCatching("delete", ":cap", ":version") { request in guard let cap = request.parameters.get("cap", as: Int.self) else { - log("Invalid cap parameter for image deletion") + log("/delete/:cap/:version: Invalid 'cap' parameter for image deletion") throw Abort(.badRequest) } guard let version = request.parameters.get("version", as: Int.self) else { - log("Invalid version parameter for image deletion") + log("/delete/:cap/:version: Invalid 'version' parameter for image deletion") throw Abort(.badRequest) } @@ -122,18 +122,18 @@ extension CapServer { private extension Request { - func getBodyData() throws -> Data { + func getBodyData(request: String) throws -> Data { guard let buffer = body.data else { - log("Missing body data") + log("\(request): Missing body data") throw CapError.invalidBody } return Data(buffer: buffer) } - func getStringBody() throws -> String { - let data = try getBodyData() + func getStringBody(request: String) throws -> String { + let data = try getBodyData(request: request) guard let content = String(data: data, encoding: .utf8) else { - log("Invalid string body") + log("\(request): Invalid string body") throw CapError.invalidBody } return content diff --git a/Sources/App/Config.swift b/Sources/App/Config.swift index 7ee358f..ef2c364 100644 --- a/Sources/App/Config.swift +++ b/Sources/App/Config.swift @@ -8,7 +8,7 @@ struct Config: Codable { /// The maximum size of the request body let maxBodySize: String - /// The path to the log file + /// The path to the folder where the metric logs are stored let logPath: String /// Serve files in the Public directory using Vapor diff --git a/Sources/Run/main.swift b/Sources/Run/main.swift index 373be5f..e715f81 100755 --- a/Sources/Run/main.swift +++ b/Sources/Run/main.swift @@ -1,7 +1,7 @@ import App import Vapor -var env = try Environment.detect() +var env = Environment.production try LoggingSystem.bootstrap(from: &env) let app = Application(env) defer { app.shutdown() }