使用 PerfectLogger
模块,除了控制台之外,事件还可以被记录到指定的文件中。
此模块还包括对将日志事件远程记录到 Perfect Log Server 的支持。
将依赖项添加到您项目的 Package.swift 文件中
.Package(url: "https://github.com/PerfectlySoft/Perfect-Logger.git", majorVersion: 3),
现在将 import
指令添加到您希望使用日志记录的文件中
import PerfectLogger
要将事件记录到本地控制台以及文件
LogFile.debug("debug message", logFile: "test.txt")
LogFile.info("info message", logFile: "test.txt")
LogFile.warning("warning message", logFile: "test.txt")
LogFile.error("error message", logFile: "test.txt")
LogFile.critical("critical message", logFile: "test.txt")
LogFile.terminal("terminal message", logFile: "test.txt")
要记录到默认文件,请省略文件名参数。
每个日志事件都会返回一个事件 ID 字符串。如果为指令提供了 eventid 字符串,则它将在日志文件中使用提供的 eventid - 这使得将相关事件链接在一起变得容易。
let eid = LogFile.warning("test 1")
LogFile.critical("test 2", eventid: eid)
返回
[WARNING] [62f940aa-f204-43ed-9934-166896eda21c] [2016-11-16 15:18:02 GMT-05:00] test 1
[CRITICAL] [62f940aa-f204-43ed-9934-166896eda21c] [2016-11-16 15:18:02 GMT-05:00] test 2
返回的 eventid 被标记为 @discardableResult
,因此如果不需要重复使用,可以安全地忽略它。
默认日志文件位置是 ./log.log
。要设置自定义日志文件位置,请设置 LogFile.location
变量
LogFile.location = "/var/log/myLog.log"
现在可以使用以下方式将消息直接记录到设置的文件中
LogFile.debug("debug message")
LogFile.info("info message")
LogFile.warning("warning message")
LogFile.error("error message")
LogFile.critical("critical message")
LogFile.terminal("terminal message")
出于调试目的,您希望看到尽可能多的信息。但是,在生产服务器上,您可能希望使用较小的日志文件并过滤掉所有冗余信息。
为此,您可以将 LogFile 的 threshold
属性设置为您希望实际记录到文件中的最低优先级。
例如
LogFile.threshold = .warning
LogFile.debug("This won't be logged into the file")
LogFile.info("This won't be logged into the file")
LogFile.warning("This will be logged into the file")
LogFile.error("This will be logged into the file")
LogFile.critical("This will be logged into the file")
此属性的默认值为 .debug
,以保持向后兼容性,并且此属性不会影响控制台/远程记录器。
根据您的需要,您可能对事件 ID、时间戳或优先级不感兴趣。
使用 LogFile 的 options
属性,您可以自定义哪些字段将实际作为前缀添加到日志消息中。
例如
// Default behaviour (equal to `[.priority, .eventId, .timestamp]`)
LogFile.options = .default
LogFile.debug("This is my log message")
// Will log: "[DEBUG] [CEC5B5DB-931F-4C5A-A794-17D060BABC80] [2019-05-04 15:16:11 GMT+02:00] This is my log message"
LogFile.options = .none
LogFile.debug("This is my log message")
// Will log: "This is my log message"
LogFile.options = [.priority, .timestamp]
LogFile.debug("This is my log message")
// Will log: "[DEBUG] [2019-05-04 15:16:11 GMT+02:00] This is my log message"
LogFile.options = [.priority]
LogFile.debug("This is my log message")
// Will log: "[DEBUG] This is my log message"
[DEBUG] [ec6a9ca5-00b1-4656-9e4c-ddecae8dde02] [2016-11-16 15:18:02 GMT-05:00] a debug message
[INFO] [ec6a9ca5-00b1-4656-9e4c-ddecae8dde02] [2016-11-16 15:18:02 GMT-05:00] an informational message
[WARNING] [ec6a9ca5-00b1-4656-9e4c-ddecae8dde02] [2016-11-16 15:18:02 GMT-05:00] a warning message
[ERROR] [62f940aa-f204-43ed-9934-166896eda21c] [2016-11-16 15:18:02 GMT-05:00] an error message
[CRITICAL] [62f940aa-f204-43ed-9934-166896eda21c] [2016-11-16 15:18:02 GMT-05:00] a critical message
[EMERG] [ec6a9ca5-00b1-4656-9e4c-ddecae8dde02] [2016-11-16 15:18:02 GMT-05:00] an emergency message
“Perfect-Logging” 依赖项包括对远程日志记录到此日志服务器的支持。
要将依赖项包含在您的项目中,请将以下内容添加到您项目的 Package.swift 文件中
.Package(url: "https://github.com/PerfectlySoft/Perfect-Logger.git", majorVersion: 3),
现在将 import 指令添加到您希望使用日志记录的文件中
import PerfectLogger
需要三个配置参数
// Your token
RemoteLogger.token = "<your token>"
// App ID (Optional)
RemoteLogger.appid = "<your appid>"
// URL to access the log server.
// Note, this is not the full API path, just the host and port.
RemoteLogger.logServer = "https://:8181"
var obj = [String: Any]()
obj["one"] = "donkey"
RemoteLogger.critical(obj)
每个日志事件都会返回一个事件 ID 字符串。如果为指令提供了 eventid 字符串,则它将在日志指令中使用提供的 eventid - 这使得将相关事件链接在一起变得容易。
let eid = RemoteLogger.critical(obj)
RemoteLogger.info(obj, eventid: eid)
返回的 eventid 被标记为 @discardableResult,因此如果不需要重复使用,可以安全地忽略它。
有关 Perfect 项目的更多信息,请访问 perfect.org。