diff --git a/TODO.md b/TODO.md index d7526ee..b8e6b5e 100644 --- a/TODO.md +++ b/TODO.md @@ -22,8 +22,8 @@ * Make more things configurable / remove hardcoded stuff * CD for binaries and images for at least arm64/x86 * Multi-container docker logs [faaso logs -f FUNKO] -* Direct error and above to stderr, others to stdout, while - keeping logging level configurable +* ✅ Direct error and above to stderr, others to stdout, + while keeping logging level configurable * ✅ Fix proxy reload / Make it reload on file changes # Things to do but not before release diff --git a/src/commands/scale.cr b/src/commands/scale.cr index cf3deb3..7d49fdd 100644 --- a/src/commands/scale.cr +++ b/src/commands/scale.cr @@ -13,15 +13,15 @@ module Faaso def local(options, name, scale) : Int32 funko = Funko::Funko.from_names([name])[0] # Asked about scale + if funko.image_history.empty? + Log.error { "Unknown funko #{funko.name}" } + return 1 + end if !scale Log.info { "Funko #{name} has a scale of #{funko.scale}" } return 0 end # Asked to set scale - if funko.image_history.empty? - Log.error { "Error: no images available for #{funko.name}:latest" } - return 1 - end funko.scale(scale.as(String).to_i) 0 end diff --git a/src/log.cr b/src/log.cr new file mode 100644 index 0000000..70958fb --- /dev/null +++ b/src/log.cr @@ -0,0 +1,50 @@ +module Logging + extend self + + class LogBackend < Log::IOBackend + @stdout = Log::IOBackend.new(io: STDOUT, formatter: LogFormat) + @stderr = Log::IOBackend.new(io: STDERR, formatter: LogFormat) + + def write(entry : Log::Entry) + if entry.severity >= Log::Severity::Error + @stderr.write entry + else + @stdout.write entry + end + end + end + + struct LogFormat < Log::StaticFormatter + @@colors = { + "FATAL" => :red, + "ERROR" => :red, + "WARN" => :yellow, + "NOTICE" => :yellow, + "INFO" => :green, + "DEBUG" => :blue, + "TRACE" => :light_blue, + } + + def run + string "#{@entry.message}".colorize(@@colors[@entry.severity.label]) + end + end + + def self.setup(verbosity) + Colorize.on_tty_only! + verbosity = [0, verbosity].max + verbosity = [6, verbosity].min + severity = [ + Log::Severity::Fatal, + Log::Severity::Error, + Log::Severity::Warn, + Log::Severity::Notice, + Log::Severity::Info, + Log::Severity::Debug, + Log::Severity::Trace, + ][verbosity] + Log.setup( + severity, + LogBackend.new) + end +end diff --git a/src/main.cr b/src/main.cr index 5c29e31..d2cb339 100644 --- a/src/main.cr +++ b/src/main.cr @@ -1,4 +1,5 @@ require "./faaso.cr" +require "./log.cr" require "colorize" require "docopt" require "rucksack" @@ -7,45 +8,6 @@ macro version "{{ `grep version shard.yml | cut -d: -f2` }}".strip() end -struct LogFormat < Log::StaticFormatter - @@colors = { - "FATAL" => :red, - "ERROR" => :red, - "WARN" => :yellow, - "INFO" => :green, - "DEBUG" => :blue, - "TRACE" => :light_blue, - } - - def run - string "#{@entry.message}".colorize(@@colors[@entry.severity.label]) - end - - def self.setup(verbosity) - Colorize.on_tty_only! - if verbosity < 3 - _verbosity = [ - Log::Severity::Fatal, - Log::Severity::Error, - Log::Severity::Warn, - ][[verbosity, 2].min] - Log.setup( - _verbosity, - Log::IOBackend.new(io: STDERR, formatter: LogFormat) - ) - end - - _verbosity = [Log::Severity::Info, - Log::Severity::Debug, - Log::Severity::Trace, - ][[verbosity - 3, 3].min] - Log.setup( - _verbosity, - Log::IOBackend.new(io: STDOUT, formatter: LogFormat) - ) - end -end - doc = <<-DOC FaaSO CLI tool. @@ -65,11 +27,11 @@ Options: -l --local Run commands locally instead of against a FaaSO server --no-runtime Don't merge a runtime into the funko -r runtime Runtime for the new funko (use -r list for examples) - -v level Control the logging verbosity, 0 to 5 [default: 3] + -v level Control the logging verbosity, 0 to 6 [default: 4] DOC ans = Docopt.docopt(doc, ARGV) -LogFormat.setup(ans["-v"].to_s.to_i) +Logging.setup(ans["-v"].to_s.to_i) Log.debug { ans } status : Int32 = 0