Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Log the request path of a failed request in the ErrorMiddleware. (#2170)
* Log the request path in the ErrorMiddleware. * Added logging tests. * ...and allTests. * Review changes.
- Loading branch information
1 parent
82ca880
commit 642f3d4
Showing
5 changed files
with
114 additions
and
3 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
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
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,64 @@ | ||
import Vapor | ||
import XCTest | ||
|
||
class LoggingTests : XCTestCase { | ||
|
||
func testNotFoundLogging() throws { | ||
|
||
var services = Services.default() | ||
var config = Config.default() | ||
let loggerProvider = TestLoggerProvider() | ||
|
||
try services.register(loggerProvider) | ||
config.prefer(TestLogger.self, for: Logger.self) | ||
|
||
let app = try Application(config: config, services: services) | ||
|
||
let req = Request( | ||
http: HTTPRequest(method: .GET, url: "/hello/vapor"), | ||
using: app | ||
) | ||
|
||
do { | ||
_ = try app.make(Responder.self).respond(to: req).wait() | ||
} catch {} | ||
|
||
XCTAssert(loggerProvider.logger.didLog(string: "Abort.404: GET /hello/vapor Not Found")) | ||
} | ||
|
||
func testInternalServerErrorLogging() throws { | ||
|
||
var services = Services.default() | ||
var config = Config.default() | ||
let loggerProvider = TestLoggerProvider() | ||
|
||
try services.register(loggerProvider) | ||
config.prefer(TestLogger.self, for: Logger.self) | ||
|
||
let router = EngineRouter.default() | ||
|
||
router.post("fail/me") { (_) -> String in | ||
throw Abort(.internalServerError) | ||
} | ||
|
||
services.register(router, as: Router.self) | ||
|
||
let app = try Application(config: config, services: services) | ||
|
||
let req = Request( | ||
http: HTTPRequest(method: .POST, url: "/fail/me"), | ||
using: app | ||
) | ||
|
||
do { | ||
_ = try app.make(Responder.self).respond(to: req).wait() | ||
} catch {} | ||
|
||
XCTAssert(loggerProvider.logger.didLog(string: "Abort.500: POST /fail/me Internal Server Error")) | ||
} | ||
|
||
static let allTests = [ | ||
("testNotFoundLogging", testNotFoundLogging), | ||
("testInternalServerErrorLogging", testInternalServerErrorLogging) | ||
] | ||
} |
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,33 @@ | ||
import Foundation | ||
import Vapor | ||
|
||
final class TestLogger: Logger { | ||
|
||
var logs = [String]() | ||
|
||
public func log(_ string: String, at level: LogLevel, file: String, function: String, line: UInt, column: UInt) { | ||
|
||
logs += [string] | ||
} | ||
|
||
func didLog(string: String) -> Bool { | ||
return logs.contains(string) | ||
} | ||
} | ||
|
||
extension TestLogger: Service {} | ||
|
||
final class TestLoggerProvider: Provider { | ||
|
||
let logger = TestLogger() | ||
|
||
func register(_ services: inout Services) throws { | ||
services.register(Logger.self) { container -> TestLogger in | ||
return self.logger | ||
} | ||
} | ||
|
||
func didBoot(_ container: Container) throws -> EventLoopFuture<Void> { | ||
return .done(on: container) | ||
} | ||
} |