Use the new Log module in Crystal 0.34.0

This commit is contained in:
Alex Ling 2020-04-07 12:07:58 +00:00
parent c572c56a39
commit 2c31f594a4
7 changed files with 40 additions and 41 deletions

View File

@ -7,13 +7,13 @@ class Context
property config : Config property config : Config
property library : Library property library : Library
property storage : Storage property storage : Storage
property logger : MLogger property logger : Logger
property queue : MangaDex::Queue property queue : MangaDex::Queue
def initialize(@config, @logger, @library, @storage, @queue) def initialize(@config, @logger, @library, @storage, @queue)
end end
{% for lvl in LEVELS %} {% for lvl in Logger::LEVELS %}
def {{lvl.id}}(msg) def {{lvl.id}}(msg)
@logger.{{lvl.id}} msg @logger.{{lvl.id}} msg
end end

View File

@ -89,7 +89,7 @@ class Title
encoded_title : String, mtime : Time encoded_title : String, mtime : Time
def initialize(@dir : String, @parent_id, storage, def initialize(@dir : String, @parent_id, storage,
@logger : MLogger, @library : Library) @logger : Logger, @library : Library)
@id = storage.get_id @dir, true @id = storage.get_id @dir, true
@title = File.basename dir @title = File.basename dir
@encoded_title = URI.encode @title @encoded_title = URI.encode @title
@ -304,7 +304,7 @@ end
class Library class Library
property dir : String, title_ids : Array(String), scan_interval : Int32, property dir : String, title_ids : Array(String), scan_interval : Int32,
logger : MLogger, storage : Storage, title_hash : Hash(String, Title) logger : Logger, storage : Storage, title_hash : Hash(String, Title)
def initialize(@dir, @scan_interval, @logger, @storage) def initialize(@dir, @scan_interval, @logger, @storage)
# explicitly initialize @titles to bypass the compiler check. it will # explicitly initialize @titles to bypass the compiler check. it will

View File

@ -2,7 +2,7 @@ require "kemal"
require "./logger" require "./logger"
class LogHandler < Kemal::BaseLogHandler class LogHandler < Kemal::BaseLogHandler
def initialize(@logger : MLogger) def initialize(@logger : Logger)
end end
def call(env) def call(env)

View File

@ -1,59 +1,58 @@
require "./config" require "log"
require "logger"
require "colorize" require "colorize"
LEVELS = ["debug", "error", "fatal", "info", "warn"] class Logger
COLORS = [:light_cyan, :light_red, :red, :light_yellow, :light_magenta] LEVELS = ["debug", "error", "fatal", "info", "warn"]
SEVERITY_IDS = [0, 4, 5, 2, 3]
COLORS = [:light_cyan, :light_red, :red, :light_yellow, :light_magenta]
class MLogger @@severity : Log::Severity = :info
def initialize(config : Config)
@logger = Logger.new STDOUT
@log_off = false
log_level = config.log_level
if log_level == "off"
@log_off = true
return
end
def initialize(level : String)
{% begin %} {% begin %}
case log_level case level.downcase
{% for lvl in LEVELS %} when "off"
@@severity = :none
{% for lvl, i in LEVELS %}
when {{lvl}} when {{lvl}}
@logger.level = Logger::{{lvl.upcase.id}} @@severity = Log::Severity.new SEVERITY_IDS[{{i}}]
{% end %} {% end %}
else else
raise "Unknown log level #{log_level}" raise "Unknown log level #{level}"
end end
{% end %} {% end %}
@logger.formatter = Logger::Formatter.new do \ @log = Log.for("")
|severity, datetime, progname, message, io|
@backend = Log::IOBackend.new
@backend.formatter = ->(entry : Log::Entry, io : IO) do
color = :default color = :default
{% begin %} {% begin %}
case severity.to_s().downcase case entry.severity.label.to_s().downcase
{% for lvl, i in LEVELS %} {% for lvl, i in LEVELS %}
when {{lvl}} when {{lvl}}, "#{{{lvl}}}ing"
color = COLORS[{{i}}] color = COLORS[{{i}}]
{% end %} {% end %}
else
end end
{% end %} {% end %}
io << "[#{severity}]".ljust(8).colorize(color) io << "[#{entry.severity.label}]".ljust(10).colorize(color)
io << datetime.to_s("%Y/%m/%d %H:%M:%S") << " | " io << entry.timestamp.to_s("%Y/%m/%d %H:%M:%S") << " | "
io << message io << entry.message
end end
Log.builder.bind "*", @@severity, @backend
end
# Ignores @@severity and always log msg
def log(msg)
@backend.write Log::Entry.new "", Log::Severity::None, msg, nil
end end
{% for lvl in LEVELS %} {% for lvl in LEVELS %}
def {{lvl.id}}(msg) def {{lvl.id}}(msg)
return if @log_off @log.{{lvl.id}} { msg }
@logger.{{lvl.id}} msg
end end
{% end %} {% end %}
def to_json(json : JSON::Builder)
json.string self
end
end end

View File

@ -79,7 +79,7 @@ module MangaDex
class Queue class Queue
property downloader : Downloader? property downloader : Downloader?
def initialize(@path : String, @logger : MLogger) def initialize(@path : String, @logger : Logger)
dir = File.dirname path dir = File.dirname path
unless Dir.exists? dir unless Dir.exists? dir
@logger.info "The queue DB directory #{dir} does not exist. " \ @logger.info "The queue DB directory #{dir} does not exist. " \
@ -253,7 +253,7 @@ module MangaDex
def initialize(@queue : Queue, @api : API, @library_path : String, def initialize(@queue : Queue, @api : API, @library_path : String,
@wait_seconds : Int32, @retries : Int32, @wait_seconds : Int32, @retries : Int32,
@logger : MLogger) @logger : Logger)
@queue.downloader = self @queue.downloader = self
spawn do spawn do

View File

@ -25,7 +25,7 @@ parser = OptionParser.parse do |parser|
end end
config = Config.load config_path config = Config.load config_path
logger = MLogger.new config logger = Logger.new config.log_level
storage = Storage.new config.db_path, logger storage = Storage.new config.db_path, logger
library = Library.new config.library_path, config.scan_interval, logger, storage library = Library.new config.library_path, config.scan_interval, logger, storage
queue = MangaDex::Queue.new config.mangadex["download_queue_db_path"].to_s, queue = MangaDex::Queue.new config.mangadex["download_queue_db_path"].to_s,

View File

@ -16,7 +16,7 @@ def random_str
end end
class Storage class Storage
def initialize(@path : String, @logger : MLogger) def initialize(@path : String, @logger : Logger)
dir = File.dirname path dir = File.dirname path
unless Dir.exists? dir unless Dir.exists? dir
@logger.info "The DB directory #{dir} does not exist. " \ @logger.info "The DB directory #{dir} does not exist. " \
@ -48,7 +48,7 @@ class Storage
hash = hash_password random_pw hash = hash_password random_pw
db.exec "insert into users values (?, ?, ?, ?)", db.exec "insert into users values (?, ?, ?, ?)",
"admin", hash, nil, 1 "admin", hash, nil, 1
puts "Initial user created. You can log in with " \ @logger.log "Initial user created. You can log in with " \
"#{{"username" => "admin", "password" => random_pw}}" "#{{"username" => "admin", "password" => random_pw}}"
end end
end end