Skip to content

Commit

Permalink
feat: set log level from arguments
Browse files Browse the repository at this point in the history
  • Loading branch information
mishamyrt committed Jul 13, 2024
1 parent 914aad7 commit f1039f4
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
8 changes: 4 additions & 4 deletions Sources/Commands/Run.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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."
Expand All @@ -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
Expand Down
26 changes: 25 additions & 1 deletion Sources/Logger/LogLevel.swift
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import ArgumentParser

/// Logger log level
enum LogLevel {
case debug
Expand All @@ -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 {
Expand All @@ -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
}
Expand Down

0 comments on commit f1039f4

Please sign in to comment.