🤖 GHALogger (GitHub Actions 日志记录器)

Compatible from Swift 5.1 to 6. Compatible with macOS, iOS, visionOS, tvOS and watchOS. Compatible with Linux, Windows, WASI and Android.

一个简单的、swift-log 兼容的日志记录器,专为 GitHub Actions 设计。

用法 🤓

在创建任何日志记录器之前,使用 GHALogger 引导 LoggingSystem

LoggingSystem.bootstrap(GHALogger.init, metadataProvider: nil/* or whatever you want */)

一个常见的模式是在 GitHub Actions 中运行时使用 GHALogger,否则使用另一个日志记录器(例如,CLTLogger)。

/* Detect whether process is running in GitHub Actions. */
let isGitHubActionsRun = ProcessInfo.processInfo.environment["GITHUB_ACTIONS"] == "true"

/* Bootstrap the logger and make it available globally. */
LoggingSystem.bootstrap(isGitHubActionsRun ? GHALogger.init : CLTLogger.init, metadataProvider: nil)

/* Create a logger.
 * We set the log level to debug if running in GitHub Actions with `RUNNER_DEBUG` set to `1`.
let logger: Logger = {
    var ret = Logger(label: "main")
    ret.logLevel = (!isGitHubActionsRun || ProcessInfo.processInfo.environment["RUNNER_DEBUG"] != "1") ? .info : .debug
    return ret
}()

屏幕截图 📺

GHALogger Log Example.

设计 📖

发送原始命令

GitHub Actions 支持接收在 stdout 上解析的命令,GHALogger 支持直接发送这些命令。使用 GHALogger.sendCommand 方法发送命令。

例如,要创建一组行,请执行

if isGitHubActionsRun {GHALogger.sendCommand(.startGroup, withText: "The start of my group.")}
defer {if isGitHubActionsRun {GHALogger.sendCommand(.endGroup)}}
logger.warning("We might have a problem.")
...