From f1039f4b63e9a14f01d4d94ce52c6581f5c9e81d Mon Sep 17 00:00:00 2001 From: mishamyrt Date: Sat, 13 Jul 2024 15:58:46 +0300 Subject: [PATCH] feat: set log level from arguments --- Sources/Commands/Run.swift | 8 ++++---- Sources/Logger/LogLevel.swift | 26 +++++++++++++++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/Sources/Commands/Run.swift b/Sources/Commands/Run.swift index 42f309b..fbf9dc3 100644 --- a/Sources/Commands/Run.swift +++ b/Sources/Commands/Run.swift @@ -6,11 +6,11 @@ extension RunOn { static var configuration = CommandConfiguration(abstract: "Start event observer.") - @Flag( + @Option( name: .shortAndLong, - help: "Print more information for debugging." + help: "Logging level." ) - var verbose = false + var log = LogLevel.error @Option( name: [.customLong("config"), .customShort("c")], help: "Configuration file path." @@ -22,7 +22,7 @@ extension RunOn { } mutating func run() throws { - logger.setLevel(verbose ? .debug : .error) + logger.setLevel(log) let config = try Config(fromContentsOf: configUrl) let handler = ConfigHandler(with: config) let activeSources = sources.filter { source in diff --git a/Sources/Logger/LogLevel.swift b/Sources/Logger/LogLevel.swift index 69121a3..bad55ef 100644 --- a/Sources/Logger/LogLevel.swift +++ b/Sources/Logger/LogLevel.swift @@ -1,3 +1,5 @@ +import ArgumentParser + /// Logger log level enum LogLevel { case debug @@ -7,7 +9,7 @@ enum LogLevel { case warning } -extension LogLevel: Comparable, Equatable { +extension LogLevel: Comparable, Equatable, ExpressibleByArgument { /// Return log level as integer var integerValue: Int { switch self { @@ -28,6 +30,28 @@ extension LogLevel: Comparable, Equatable { } } + init?(argument: String) { + switch argument.lowercased() { + case "debug": + self = .debug + + case "disabled": + self = .disabled + + case "error": + self = .error + + case "info": + self = .info + + case "warning": + self = .warning + + default: + return nil + } + } + static func < (lhs: LogLevel, rhs: LogLevel) -> Bool { lhs.integerValue < rhs.integerValue }