Skip to content

Commit

Permalink
fixup! Logger: Add color support for different log levels
Browse files Browse the repository at this point in the history
  • Loading branch information
Fijxu committed Oct 8, 2024
1 parent 17b525f commit d2edd4b
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion config/config.example.yml
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ https_only: false

##
## Enables colors in logs. Useful for debugging purposes
## This is overridden if "-k" or "--colorize"
## This is overridden if "-k" or "--colorize"
## are passed on the command line.
## Colors are also disabled if the environment variable
## NO_COLOR is present and has any value
Expand Down
6 changes: 4 additions & 2 deletions src/invidious/helpers/logger.cr
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ enum LogLevel
end

class Invidious::LogHandler < Kemal::BaseLogHandler
def initialize(@io : IO = STDOUT, @level = LogLevel::Debug, @use_color : Bool = true)
def initialize(@io : IO = STDOUT, @level = LogLevel::Debug, use_color : Bool = true)
Colorize.enabled = use_color
Colorize.on_tty_only!
end

def call(context : HTTP::Server::Context)
Expand Down Expand Up @@ -56,7 +58,7 @@ class Invidious::LogHandler < Kemal::BaseLogHandler
{% 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}".colorize(color(LogLevel::{{level.id.capitalize}})).toggle(@use_color))
puts("#{Time.utc} [{{level.id}}] #{message}".colorize(color(LogLevel::{{level.id.capitalize}})))
end
end
{% end %}
Expand Down

0 comments on commit d2edd4b

Please sign in to comment.