Improve logging

This commit is contained in:
Christoph Hagen 2023-02-06 21:31:12 +01:00
parent 29ecde067f
commit e4f2fc547b
5 changed files with 22 additions and 22 deletions

View File

@ -8,7 +8,7 @@ let package = Package(
], ],
dependencies: [ dependencies: [
.package(url: "https://github.com/vapor/vapor", from: "4.0.0"), .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: [ targets: [
.target(name: "App", .target(name: "App",

View File

@ -1,7 +1,7 @@
{ {
"port" : 6001, "port" : 6001,
"maxBodySize" : "2mb", "maxBodySize" : "2mb",
"logPath": "\/var\/log\/caps.log", "logPath": "\/var\/log\/caps/metrics",
"serveFiles": true, "serveFiles": true,
"writers" : [ "writers" : [
"auth_key_1" "auth_key_1"

View File

@ -29,26 +29,26 @@ extension CapServer {
app.postCatching("cap") { request in app.postCatching("cap") { request in
try self.ensureOperability() try self.ensureOperability()
try authenticator.authorize(request) 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) let cap = try decoder.decode(Cap.self, from: data)
try self.addOrUpdate(cap) try self.addOrUpdate(cap)
} }
// Upload an image // Upload an image
app.postCatching("images", ":n") { request in app.postCatching("images", ":cap") { request in
try self.ensureOperability() try self.ensureOperability()
try authenticator.authorize(request) try authenticator.authorize(request)
guard let cap = request.parameters.get("n", as: Int.self) else { 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) throw Abort(.badRequest)
} }
let data = try request.getBodyData() let data = try request.getBodyData(request: "/images/:cap")
try self.save(image: data, for: cap) try self.save(image: data, for: cap)
} }
// Update the classifier // Update the classifier
app.on(.POST, "classifier", ":version", body: .collect(maxSize: "50mb")) { request -> HTTPStatus in app.on(.POST, "classifier", ":version", body: .collect(maxSize: "50mb")) { request -> HTTPStatus in
guard let version = request.parameters.get("version", as: Int.self) else { 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) throw Abort(.badRequest)
} }
guard version > self.classifierVersion else { guard version > self.classifierVersion else {
@ -57,7 +57,7 @@ extension CapServer {
try self.ensureOperability() try self.ensureOperability()
try authenticator.authorize(request) try authenticator.authorize(request)
let data = try request.getBodyData() let data = try request.getBodyData(request: "/classifier/:version")
try self.save(classifier: data, version: version) try self.save(classifier: data, version: version)
return .ok return .ok
} }
@ -65,17 +65,17 @@ extension CapServer {
// Update the trained classes // Update the trained classes
app.postCatching("classes", ":date") { request in app.postCatching("classes", ":date") { request in
guard let dateString = request.parameters.get("date") else { guard let dateString = request.parameters.get("date") else {
log("Invalid parameter for date") log("/classes/:date: Invalid 'date' parameter")
throw Abort(.badRequest) throw Abort(.badRequest)
} }
guard let date = dateFormatter.date(from: dateString) else { guard let date = dateFormatter.date(from: dateString) else {
log("Invalid date specification") log("/classes/:date: Invalid 'date' specification")
throw Abort(.badRequest) throw Abort(.badRequest)
} }
try self.ensureOperability() try self.ensureOperability()
try authenticator.authorize(request) try authenticator.authorize(request)
let body = try request.getStringBody() let body = try request.getStringBody(request: "/classes/:date")
self.updateTrainedClasses(content: body) self.updateTrainedClasses(content: body)
self.removeAllEntriesInImageChangeList(before: date) self.removeAllEntriesInImageChangeList(before: date)
@ -91,23 +91,23 @@ extension CapServer {
// Upload the thumbnail of a cap // Upload the thumbnail of a cap
app.postCatching("thumbnails", ":cap") { request in app.postCatching("thumbnails", ":cap") { request in
guard let cap = request.parameters.get("cap", as: Int.self) else { 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) throw Abort(.badRequest)
} }
try self.ensureOperability() try self.ensureOperability()
try authenticator.authorize(request) try authenticator.authorize(request)
let data = try request.getBodyData() let data = try request.getBodyData(request: "/thumbnails/:cap")
self.saveThumbnail(data, for: cap) self.saveThumbnail(data, for: cap)
} }
// Delete the image of a cap // Delete the image of a cap
app.postCatching("delete", ":cap", ":version") { request in app.postCatching("delete", ":cap", ":version") { request in
guard let cap = request.parameters.get("cap", as: Int.self) else { 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) throw Abort(.badRequest)
} }
guard let version = request.parameters.get("version", as: Int.self) else { 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) throw Abort(.badRequest)
} }
@ -122,18 +122,18 @@ extension CapServer {
private extension Request { private extension Request {
func getBodyData() throws -> Data { func getBodyData(request: String) throws -> Data {
guard let buffer = body.data else { guard let buffer = body.data else {
log("Missing body data") log("\(request): Missing body data")
throw CapError.invalidBody throw CapError.invalidBody
} }
return Data(buffer: buffer) return Data(buffer: buffer)
} }
func getStringBody() throws -> String { func getStringBody(request: String) throws -> String {
let data = try getBodyData() let data = try getBodyData(request: request)
guard let content = String(data: data, encoding: .utf8) else { guard let content = String(data: data, encoding: .utf8) else {
log("Invalid string body") log("\(request): Invalid string body")
throw CapError.invalidBody throw CapError.invalidBody
} }
return content return content

View File

@ -8,7 +8,7 @@ struct Config: Codable {
/// The maximum size of the request body /// The maximum size of the request body
let maxBodySize: String let maxBodySize: String
/// The path to the log file /// The path to the folder where the metric logs are stored
let logPath: String let logPath: String
/// Serve files in the Public directory using Vapor /// Serve files in the Public directory using Vapor

View File

@ -1,7 +1,7 @@
import App import App
import Vapor import Vapor
var env = try Environment.detect() var env = Environment.production
try LoggingSystem.bootstrap(from: &env) try LoggingSystem.bootstrap(from: &env)
let app = Application(env) let app = Application(env)
defer { app.shutdown() } defer { app.shutdown() }