Invidious is an alternative front-end to YouTube
Logger: Add color support for different log levels
Fijxuf8ec3123286e
diff --git a/config/config.example.yml b/config/config.example.ymlindex 219aa03f..37b932ea 100644--- a/config/config.example.yml+++ b/config/config.example.yml@@ -222,6 +222,15 @@ https_only: false###log_level: Info+##+## Enables colors in logs. Useful for debugging purposes+## This is overridden if "-k" or "--colorize"+## are passed on the command line.+##+## Accepted values: true, false+## Default: false+##+#colorize_logs: false# -----------------------------# Featuresdiff --git a/src/invidious.cr b/src/invidious.crindex 3804197e..d9a479d1 100644--- a/src/invidious.cr+++ b/src/invidious.cr@@ -117,6 +117,9 @@ Kemal.config.extra_options do |parser|parser.on("-l LEVEL", "--log-level=LEVEL", "Log level, one of #{LogLevel.values} (default: #{CONFIG.log_level})") do |log_level|CONFIG.log_level = LogLevel.parse(log_level)end+ parser.on("-k", "--colorize", "Colorize logs") do+ CONFIG.colorize_logs = true+ endparser.on("-v", "--version", "Print version") doputs SOFTWARE.to_pretty_jsonexit@@ -133,7 +136,7 @@ if CONFIG.output.upcase != "STDOUT"FileUtils.mkdir_p(File.dirname(CONFIG.output))endOUTPUT = CONFIG.output.upcase == "STDOUT" ? STDOUT : File.open(CONFIG.output, mode: "a")-LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level)+LOGGER = Invidious::LogHandler.new(OUTPUT, CONFIG.log_level, CONFIG.colorize_logs)# Check table integrityInvidious::Database.check_integrity(CONFIG)diff --git a/src/invidious/config.cr b/src/invidious/config.crindex c4ddcdb3..d8543d35 100644--- a/src/invidious/config.cr+++ b/src/invidious/config.cr@@ -68,6 +68,8 @@ class Configproperty output : String = "STDOUT"# Default log level, valid YAML values are ints and strings, see src/invidious/helpers/logger.crproperty log_level : LogLevel = LogLevel::Info+ # Enables colors in logs. Useful for debugging purposes+ property colorize_logs : Bool = false# Database configuration with separate parameters (username, hostname, etc)property db : DBConfig? = nildiff --git a/src/invidious/helpers/logger.cr b/src/invidious/helpers/logger.crindex b443073e..36a3a7f9 100644--- a/src/invidious/helpers/logger.cr+++ b/src/invidious/helpers/logger.cr@@ -1,3 +1,5 @@+require "colorize"+enum LogLevelAll = 0Trace = 1@@ -10,7 +12,7 @@ enum LogLevelendclass Invidious::LogHandler < Kemal::BaseLogHandler- def initialize(@io : IO = STDOUT, @level = LogLevel::Debug)+ def initialize(@io : IO = STDOUT, @level = LogLevel::Debug, @color : Bool = true)enddef call(context : HTTP::Server::Context)@@ -39,10 +41,23 @@ class Invidious::LogHandler < Kemal::BaseLogHandler@io.flushend+ def color(level)+ case level+ when LogLevel::Trace then :cyan+ when LogLevel::Debug then :green+ when LogLevel::Info then :white+ when LogLevel::Warn then :yellow+ when LogLevel::Error then :red+ when LogLevel::Fatal then :magenta+ else :default+ end+ end+{% for level in %w(trace debug info warn error fatal) %}def {{level.id}}(message : String)if LogLevel::{{level.id.capitalize}} >= @level- puts("#{Time.utc} [{{level.id}}] #{message}")+ puts("#{Time.utc} [{{level.id}}] #{message}".colorize(color(LogLevel::{{level.id.capitalize}})).toggle(@color))+endend{% end %}