54 lines
1.4 KiB
Swift
54 lines
1.4 KiB
Swift
import Dependencies
|
|
import Logging
|
|
|
|
extension Logger.Level {
|
|
|
|
/// Set the log level based on the user's options supplied.
|
|
init(globals: BasicGlobalOptions, quietOnlyPlaybook: Bool) {
|
|
if quietOnlyPlaybook || !globals.quiet {
|
|
switch globals.verbose {
|
|
case 0:
|
|
self = .info
|
|
case 1:
|
|
self = .debug
|
|
case 2...:
|
|
self = .trace
|
|
default:
|
|
self = .info
|
|
}
|
|
}
|
|
self = .info
|
|
}
|
|
}
|
|
|
|
func withSetupLogger(
|
|
commandName: String,
|
|
globals: BasicGlobalOptions,
|
|
quietOnlyPlaybook: Bool = false,
|
|
dependencies setupDependencies: (inout DependencyValues) -> Void = { _ in },
|
|
operation: @escaping () async throws -> Void
|
|
) async rethrows {
|
|
try await withDependencies {
|
|
$0.logger = .init(label: "\("hpa".yellow)")
|
|
$0.logger.logLevel = .init(globals: globals, quietOnlyPlaybook: quietOnlyPlaybook)
|
|
$0.logger[metadataKey: "command"] = "\(commandName.blue)"
|
|
} operation: {
|
|
try await operation()
|
|
}
|
|
}
|
|
|
|
func withSetupLogger(
|
|
commandName: String,
|
|
globals: GlobalOptions,
|
|
dependencies setupDependencies: (inout DependencyValues) -> Void = { _ in },
|
|
operation: @escaping () async throws -> Void
|
|
) async rethrows {
|
|
try await withSetupLogger(
|
|
commandName: commandName,
|
|
globals: globals.basic,
|
|
quietOnlyPlaybook: globals.quietOnlyPlaybook,
|
|
dependencies: setupDependencies,
|
|
operation: operation
|
|
)
|
|
}
|