luminate/Sources/Luminate/LogBootstrap.swift

47 lines
1.8 KiB
Swift

//
// LogBootstrap.swift
//
// Copyright 2026 Brendan Szymanski <hello@bscubed.dev>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
// SPDX-License-Identifier: GPL-3.0-or-later
//
import Foundation
import Logging
import LuminateCore
/// Installs Luminate's process-wide logging backend.
package enum LogBootstrap {
/// Environment variable naming the minimum level to emit.
package static let logLevelVariable = "LUMINATE_LOG_LEVEL"
/// Routes every logger in the process to standard error at the configured level.
///
/// `LoggingSystem.bootstrap` may be called at most once per process, so this must be called
/// exactly once during application initialization and never from a test.
///
/// The level comes from `LUMINATE_LOG_LEVEL` and defaults to `info` when unset or unparseable.
package static func bootstrap() {
let level =
ProcessInfo.processInfo.environment[logLevelVariable]
.flatMap(Logger.Level.init(rawValue:)) ?? .info
LoggingSystem.bootstrap { label in
var handler = StreamLogHandler.standardError(label: label)
handler.logLevel = level
return handler
}
}
}