一个 SwiftLog 的日志后端,可以将日志消息转换为 GitHub Actions 的工作流命令。
import Logging
import LoggingGitHubActions
import struct Foundation.ProcessInfo
LoggingSystem.bootstrap { label in
if ProcessInfo.processInfo.environment["GITHUB_ACTIONS"] == "true" {
return GitHubActionsLogHandler.standardOutput(label: label)
} else {
return StreamLogHandler.standardOutput(label: label)
}
}
创建一个 Logger
的实例,并相应地记录消息。当你的程序作为 GitHub Actions 工作流中的一个步骤运行时,警告和错误消息将被格式化,以便在 GitHub Actions UI 中显示。
import Logging
let logger = Logger(label: "com.example.MyApp")
logger.error("Something went wrong")
// Prints "::error file=Sources/main.swift,line=5::Something went wrong
将 swift-log-github-actions
添加为你的 Package.swift
文件的依赖项。
.package(url: "https://github.com/NSHipster/swift-log-github-actions.git", from: "1.0.0")
将 "LoggingGitHubActions
添加到你的目标的依赖项中。
.target(name: "Example",
dependencies: ["LoggingGitHubActions"])
MIT
Mattt (@mattt)