-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use a custom middleware to log any incoming request (#34)
* `RequestLoggerMiddleware` because we are not carpenters * Apply suggestions from code review Co-Authored-By: Yasuhiro Inami <[email protected]>
- Loading branch information
1 parent
05f731b
commit 121e6a4
Showing
4 changed files
with
30 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,8 @@ | ||
import Vapor | ||
import Bot | ||
|
||
public final class PrintLogger: LoggerProtocol { | ||
public final class PrintLogger: LoggerProtocol, Service { | ||
public func log(_ message: String) { | ||
print("[WALL-E] \(message)") | ||
print("\(Date()) | [WALL-E] \(message)") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Vapor | ||
import Bot | ||
|
||
final class RequestLoggerMiddleware: Middleware, ServiceType { | ||
private let logger: LoggerProtocol | ||
|
||
init(logger: LoggerProtocol) { | ||
self.logger = logger | ||
} | ||
|
||
func respond(to request: Request, chainingTo next: Responder) throws -> Future<Response> { | ||
logger.log(""" | ||
📝 Request logger 📝 | ||
\(request) | ||
=========================== | ||
""") | ||
return try next.respond(to: request) | ||
} | ||
|
||
static func makeService(for container: Container) throws -> RequestLoggerMiddleware { | ||
return RequestLoggerMiddleware(logger: try container.make(PrintLogger.self)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters