一个简单的、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
}()
critical
、error
、warning
和 notice
的日志作为 GitHub Actions 命令发送,链接到发送日志的文件和行。critical
级别的日志记录会带有一个额外的彩色前缀,以确保日志可见。debug
的日志作为 GitHub Actions 命令发送,尽管此命令未链接到发送日志的文件(该命令不支持此功能)。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.")
...