mirror of
https://github.com/apple/swift-log
synced 2026-11-24 03:47:49 +00:00
Add compile-time traits for eliminating log levels from binaries ### Motivation: Applications often know in advance which log levels will never be needed in production. For example, a production service might only need warning and above, while trace and debug levels are only useful during development. This PR introduces compile-time traits that completely eliminate unwanted log levels from binaries, achieving zero runtime overhead for disabled log statements. ### Modifications: - Added seven `MaxLogLevel*` traits - Extended benchmarks with the `MaxLogLevelWarning` suite - Added "Disable log levels during compilation" section to the best practices guide ### Result: Users can eliminate unwanted log levels at compile time by specifying traits in their `Package.swift` dependency declaration.
83 lines
3.0 KiB
Swift
83 lines
3.0 KiB
Swift
// swift-tools-version:6.1
|
|
//===----------------------------------------------------------------------===//
|
|
//
|
|
// This source file is part of the Swift Logging API open source project
|
|
//
|
|
// Copyright (c) 2018-2019 Apple Inc. and the Swift Logging API project authors
|
|
// Licensed under Apache License v2.0
|
|
//
|
|
// See LICENSE.txt for license information
|
|
// See CONTRIBUTORS.txt for the list of Swift Logging API project authors
|
|
//
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
//
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
import PackageDescription
|
|
|
|
let package = Package(
|
|
name: "swift-log",
|
|
products: [
|
|
.library(name: "Logging", targets: ["Logging"]),
|
|
.library(name: "InMemoryLogging", targets: ["InMemoryLogging"]),
|
|
],
|
|
traits: [
|
|
.trait(name: "MaxLogLevelDebug", description: "Debug and above available (compiles out trace)"),
|
|
.trait(name: "MaxLogLevelInfo", description: "Info and above available (compiles out trace, debug)"),
|
|
.trait(name: "MaxLogLevelNotice", description: "Notice and above available (compiles out trace, debug, info)"),
|
|
.trait(
|
|
name: "MaxLogLevelWarning",
|
|
description: "Warning and above available (compiles out trace, debug, info, notice)"
|
|
),
|
|
.trait(
|
|
name: "MaxLogLevelError",
|
|
description: "Error and above available (compiles out trace, debug, info, notice, warning)"
|
|
),
|
|
.trait(name: "MaxLogLevelCritical", description: "Only critical available (compiles out all except critical)"),
|
|
.trait(name: "MaxLogLevelNone", description: "All logging compiled out (no log levels available)"),
|
|
|
|
// By default, no traits are enabled (all log levels available)
|
|
.default(enabledTraits: []),
|
|
],
|
|
targets: [
|
|
.target(
|
|
name: "Logging",
|
|
dependencies: []
|
|
),
|
|
.target(
|
|
name: "InMemoryLogging",
|
|
dependencies: ["Logging"]
|
|
),
|
|
.testTarget(
|
|
name: "LoggingTests",
|
|
dependencies: ["Logging"]
|
|
),
|
|
.testTarget(
|
|
name: "InMemoryLoggingTests",
|
|
dependencies: ["InMemoryLogging", "Logging"]
|
|
),
|
|
]
|
|
)
|
|
|
|
for target in package.targets {
|
|
var settings = target.swiftSettings ?? []
|
|
settings.append(.enableExperimentalFeature("StrictConcurrency=complete"))
|
|
target.swiftSettings = settings
|
|
}
|
|
|
|
// --- STANDARD CROSS-REPO SETTINGS DO NOT EDIT --- //
|
|
for target in package.targets {
|
|
switch target.type {
|
|
case .regular, .test, .executable:
|
|
var settings = target.swiftSettings ?? []
|
|
// https://github.com/swiftlang/swift-evolution/blob/main/proposals/0444-member-import-visibility.md
|
|
settings.append(.enableUpcomingFeature("MemberImportVisibility"))
|
|
target.swiftSettings = settings
|
|
case .macro, .plugin, .system, .binary:
|
|
() // not applicable
|
|
@unknown default:
|
|
() // we don't know what to do here, do nothing
|
|
}
|
|
}
|
|
// --- END: STANDARD CROSS-REPO SETTINGS DO NOT EDIT --- //
|