Fix logging

This commit is contained in:
Roberto Alsina 2024-07-07 10:02:06 -03:00
parent 824c94bebc
commit c7188b87d7
4 changed files with 59 additions and 47 deletions

View File

@ -22,8 +22,8 @@
* Make more things configurable / remove hardcoded stuff * Make more things configurable / remove hardcoded stuff
* CD for binaries and images for at least arm64/x86 * CD for binaries and images for at least arm64/x86
* Multi-container docker logs [faaso logs -f FUNKO] * Multi-container docker logs [faaso logs -f FUNKO]
* Direct error and above to stderr, others to stdout, while * Direct error and above to stderr, others to stdout,
keeping logging level configurable while keeping logging level configurable
* ✅ Fix proxy reload / Make it reload on file changes * ✅ Fix proxy reload / Make it reload on file changes
# Things to do but not before release # Things to do but not before release

View File

@ -13,15 +13,15 @@ module Faaso
def local(options, name, scale) : Int32 def local(options, name, scale) : Int32
funko = Funko::Funko.from_names([name])[0] funko = Funko::Funko.from_names([name])[0]
# Asked about scale # Asked about scale
if funko.image_history.empty?
Log.error { "Unknown funko #{funko.name}" }
return 1
end
if !scale if !scale
Log.info { "Funko #{name} has a scale of #{funko.scale}" } Log.info { "Funko #{name} has a scale of #{funko.scale}" }
return 0 return 0
end end
# Asked to set scale # 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) funko.scale(scale.as(String).to_i)
0 0
end end

50
src/log.cr Normal file
View File

@ -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

View File

@ -1,4 +1,5 @@
require "./faaso.cr" require "./faaso.cr"
require "./log.cr"
require "colorize" require "colorize"
require "docopt" require "docopt"
require "rucksack" require "rucksack"
@ -7,45 +8,6 @@ macro version
"{{ `grep version shard.yml | cut -d: -f2` }}".strip() "{{ `grep version shard.yml | cut -d: -f2` }}".strip()
end 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 doc = <<-DOC
FaaSO CLI tool. FaaSO CLI tool.
@ -65,11 +27,11 @@ Options:
-l --local Run commands locally instead of against a FaaSO server -l --local Run commands locally instead of against a FaaSO server
--no-runtime Don't merge a runtime into the funko --no-runtime Don't merge a runtime into the funko
-r runtime Runtime for the new funko (use -r list for examples) -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 DOC
ans = Docopt.docopt(doc, ARGV) ans = Docopt.docopt(doc, ARGV)
LogFormat.setup(ans["-v"].to_s.to_i) Logging.setup(ans["-v"].to_s.to_i)
Log.debug { ans } Log.debug { ans }
status : Int32 = 0 status : Int32 = 0