MELogger

安装

您可以在您的 Project / Package Dependencies 设置下安装此包。或者,如果您将其指定为另一个包的依赖项,您可以将其添加到 Package.swift 中。

//...
dependencies: [
    .package(
        url: "git@github.com:Mono-Empire/MELogger.git",
        from: Version("3.0.0")
    )
],
targets: [
    .target(
        name: "YourProject",
        dependencies: [
            .product(
                name: "MELogger",
                package: "MELogger"
            ),
        ]
    ),
    //...
]
//...

基本用法

提供了一个非常类似于 SwiftLog 的通用日志接口。 允许将日志记录到控制台、文件或任何其他自定义目标(例如云服务、Slack 等)。

使用非常简单

// Instantiate a Logger (perhaps as a global constant, but that's up to you)
let logger = Logger(label: "com.example.mybundle")
// ...by default messages will be logged to the console.

// Log a message, specifying the log-level with the appropriate method:
logger.warning("This is a warning")
// ...you can add some meta-data too:
logger.notice("This is just a notice", metadata: ['class': 'MyClassName'])

// For all levels you can also pass an optional `Error` (though obviously more common for things like `error` or `critical`)
logger.error("Something bad happened!", error: err)

您可以使用以下级别

指定日志目标

每个 logger 实例都可以设置一个或多个日志目标 - 因此,例如,日志消息可以同时发送到控制台和日志文件。对于每个目标,您可以设置要记录的级别,以及许多其他特定于目标的设置,例如日志文件大小和轮换首选项。

这是一个设置日志记录到文件的示例

// Create a file logger (see the in-code documentation for more settings)
let settings = FileLoggerDestination.Settings(logFileSizeLimit: 300, logFileRotationsKept: 2)
let fileLoggerDestination = FileLoggerDestination(settings: settings)

// Add the destination to an existing logger instance...
logger.destinationManager.add(fileLoggerDestination)

// ...or you can add the destination to all logger instances:
MELoggerDestinationManager.shared.add(fileLoggerDestination)

目标对象还提供了管理日志文件的有用方法

// Get all the log files
let logFiles = fileLoggerDestination.getLogFiles()

// Clear the log files
fileLoggerDestination.clearLogFiles()

内置日志目标

默认情况下,MELogger 提供了一组开箱即用的有用日志目标

有关每个目标支持的内容的更多信息,请参见每个目标的 Settings

自定义日志目标

您还可以编写自己的自定义日志目标,例如记录到云服务。 只需实现一个符合 MELoggerDestination 的新 struct

例如,请参阅此处的 CrashlyticsLoggerDestination。 并非所有软件包都具有 Crashlytics 依赖项,但只需将 CrashlyticsLoggerDestination 添加一次到您的共享目标即可

// For example, in your AppDelegate's didFinishLaunchingWithOptions...

// Initialize Firebase
FirebaseApp.configure()

// Add Crashlytics destination
MELoggerDestinationManager.shared.add(CrashlyticsLoggerDestination())

现在您已将 CrashlyticsLoggerDestination 作为共享目标,即使来自没有 Firebase 依赖项的子包的相关日志消息也将被发送到 Crashlytics。

使用 MockLoggerDestination 进行测试

在某些情况下,使用 MockLoggerDestination 测试代码中的错误情况可能很有用。 此日志目标允许您访问任何先前的日志消息以进行断言。 例如,在您的测试中

// ExampleTests.swift

// Create and add mock destination
let mockLogDestination = MockLogDestination()
MELoggerDestinationManager.shared.removeAll()
MELoggerDestinationManager.shared.add(mockLogDestination)

// Do something illegal
let myExample = Example()
myExample.doSomethingThatTriggersWarning()

// Check if it was illegal
XCTAssertEqual(anotherMockLogDestination.lastLoggedLevel, .warning)
XCTAssertFalse(anotherMockLogDestination.loggedMessages.isEmpty)
XCTAssertEqual(anotherMockLogDestination.lastLoggedMessage, "The warning message")