mirror of
https://github.com/iv-org/invidious.git
synced 2024-11-22 13:13:04 +05:30
Logger: Add color support for different log levels (#4931)
No related issue
This commit is contained in:
commit
a760b69cb6
@ -233,6 +233,17 @@ http_proxy:
|
|||||||
##
|
##
|
||||||
#log_level: Info
|
#log_level: Info
|
||||||
|
|
||||||
|
##
|
||||||
|
## Enables colors in logs. Useful for debugging purposes
|
||||||
|
## 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
|
||||||
|
##
|
||||||
|
## Accepted values: true, false
|
||||||
|
## Default: true
|
||||||
|
##
|
||||||
|
#colorize_logs: false
|
||||||
|
|
||||||
# -----------------------------
|
# -----------------------------
|
||||||
# Features
|
# Features
|
||||||
|
@ -122,6 +122,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|
|
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)
|
CONFIG.log_level = LogLevel.parse(log_level)
|
||||||
end
|
end
|
||||||
|
parser.on("-k", "--colorize", "Colorize logs") do
|
||||||
|
CONFIG.colorize_logs = true
|
||||||
|
end
|
||||||
parser.on("-v", "--version", "Print version") do
|
parser.on("-v", "--version", "Print version") do
|
||||||
puts SOFTWARE.to_pretty_json
|
puts SOFTWARE.to_pretty_json
|
||||||
exit
|
exit
|
||||||
@ -138,7 +141,7 @@ if CONFIG.output.upcase != "STDOUT"
|
|||||||
FileUtils.mkdir_p(File.dirname(CONFIG.output))
|
FileUtils.mkdir_p(File.dirname(CONFIG.output))
|
||||||
end
|
end
|
||||||
OUTPUT = CONFIG.output.upcase == "STDOUT" ? STDOUT : File.open(CONFIG.output, mode: "a")
|
OUTPUT = 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 integrity
|
# Check table integrity
|
||||||
Invidious::Database.check_integrity(CONFIG)
|
Invidious::Database.check_integrity(CONFIG)
|
||||||
|
@ -78,6 +78,8 @@ class Config
|
|||||||
property output : String = "STDOUT"
|
property output : String = "STDOUT"
|
||||||
# Default log level, valid YAML values are ints and strings, see src/invidious/helpers/logger.cr
|
# Default log level, valid YAML values are ints and strings, see src/invidious/helpers/logger.cr
|
||||||
property log_level : LogLevel = LogLevel::Info
|
property 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)
|
# Database configuration with separate parameters (username, hostname, etc)
|
||||||
property db : DBConfig? = nil
|
property db : DBConfig? = nil
|
||||||
|
|
||||||
|
@ -1,3 +1,5 @@
|
|||||||
|
require "colorize"
|
||||||
|
|
||||||
enum LogLevel
|
enum LogLevel
|
||||||
All = 0
|
All = 0
|
||||||
Trace = 1
|
Trace = 1
|
||||||
@ -10,7 +12,9 @@ enum LogLevel
|
|||||||
end
|
end
|
||||||
|
|
||||||
class Invidious::LogHandler < Kemal::BaseLogHandler
|
class Invidious::LogHandler < Kemal::BaseLogHandler
|
||||||
def initialize(@io : IO = STDOUT, @level = LogLevel::Debug)
|
def initialize(@io : IO = STDOUT, @level = LogLevel::Debug, use_color : Bool = true)
|
||||||
|
Colorize.enabled = use_color
|
||||||
|
Colorize.on_tty_only!
|
||||||
end
|
end
|
||||||
|
|
||||||
def call(context : HTTP::Server::Context)
|
def call(context : HTTP::Server::Context)
|
||||||
@ -39,10 +43,22 @@ class Invidious::LogHandler < Kemal::BaseLogHandler
|
|||||||
@io.flush
|
@io.flush
|
||||||
end
|
end
|
||||||
|
|
||||||
|
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) %}
|
{% for level in %w(trace debug info warn error fatal) %}
|
||||||
def {{level.id}}(message : String)
|
def {{level.id}}(message : String)
|
||||||
if LogLevel::{{level.id.capitalize}} >= @level
|
if LogLevel::{{level.id.capitalize}} >= @level
|
||||||
puts("#{Time.utc} [{{level.id}}] #{message}")
|
puts("#{Time.utc} [{{level.id}}] #{message}".colorize(color(LogLevel::{{level.id.capitalize}})))
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
{% end %}
|
{% end %}
|
||||||
|
Loading…
Reference in New Issue
Block a user