- Hands-On Server-Side Web Development with Swift
- Angus Yeung
- 412字
- 2021-06-10 19:00:23
Using the Logger API in a Kitura Project
Similarly, Kitura provides a unified interface for different kinds of logger implementations. Kitura uses the LoggerAPI as the Logging API throughout its implementation. In a typical Kitura project, HeliumLogger is often used. HeliumLogger is a lightweight implementation of LoggerAPI and it is available as an open source project.
If you use kitura init to create a boilerplate for your project, you do not need to import LoggerAPI and HeliumLogger specifically, as the packages have already been included as dependencies in your Kitura project.
You'll find the import LoggerAPI and import HeliumLogger statements at the beginning of main.swift:
...
// main.swift
import LoggerAPI
import HeliumLogger
do {
HeliumLogger.use(LoggerMessageType.info) // [1]
...
} catch let error {
Log.error(error.localizedDescription) // [2]
}
This code snippet shows how LoggerAPI and HeliumLogger are used in your project:
- HeliumLogger is configured to use LoggerMessageType.info
- Calls Log.error() to log an error message
The LoggerMessageType is an enum consisting of the following message types:
The message types are listed in ascending order of severity levels. If you choose to use a message type with a higher severity level, messages with lower severity will not be shown. Consider the verbose severity level:
HeliumLogger.use(LoggerMessageType.verbose)
When the verbose severity level is chosen, the debug messages will not be shown because the debug message type has a lower severity level.
If you change the HeliumLogger message type setting from LoggerMessageType.info to LoggerMessageType.verbose in [1] and rebuild your project using command + R, you're going to see additional information logged and printed on screen with the [VERBOSE] message type:
. . .
[Mon Jun 25 11:31:32 2018] com.ibm.diagnostics.healthcenter.loader INFO: Swift Application Metrics
[2018-06-25T11:31:32.154+08:00] [VERBOSE] [Router.swift:108 init(mergeParameters:)] Router initialized
[2018-06-25T11:31:32.200+08:00] [VERBOSE] [Router.swift:108 init(mergeParameters:)] Router initialized
[2018-06-25T11:31:32.200+08:00] [INFO] [Metrics.swift:20 initializeMetrics(router:)] Initialized metrics.
[2018-06-25T11:31:32.201+08:00] [VERBOSE] [Kitura.swift:104 run()] Starting Kitura framework...
[2018-06-25T11:31:32.201+08:00] [VERBOSE] [Kitura.swift:118 start()] Starting an HTTP Server on port 8080...
[2018-06-25T11:31:32.202+08:00] [INFO] [HTTPServer.swift:124 listen(on:)] Listening on port 8080
[2018-06-25T11:31:32.202+08:00] [VERBOSE] [HTTPServer.swift:125 listen(on:)] Options for port 8080: maxPendingConnections: 100, allowPortReuse: false
Throughout your project, you can log different types of logging messages. Some examples are:
Log.warning("The input parameter is out of range!")
Log.debug("Variable x increments by 100.")
Log.error("Bummer! Something is not working well.")
Logging is a useful server-side development technique. Since your server application will be deployed and running on a hosted server remotely in a data center, logging is perhaps one of the best ways to detect problems and find out their root causes.