From d40c8573132b8926657038829125a9a0855175fb Mon Sep 17 00:00:00 2001 From: pvincent Date: Fri, 26 May 2023 22:25:35 +0400 Subject: [PATCH] colorized logs --- app/controllers/welcome_controller.rb | 18 ++++++++ config/application.rb | 17 +++---- lib/formatters/basic_formatter.rb | 65 +++++++++++++++++++++++---- 3 files changed, 80 insertions(+), 20 deletions(-) diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index f9b859b..41de7f5 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,4 +1,22 @@ class WelcomeController < ApplicationController def index + logger.measure_error "Took too long to complete", min_duration: 3000 do + logger.debug("Debugging information to aid with problem determination") + logger.info("Informational message such as request received\nline 2\nline3") + logger.warn("Warn about something in the system") + logger.error("An error occurred during processing") + logger.fatal("Oh no something really bad happened") + + logger.measure_info "Called external interface" do + sleep 0.1 # Code to call external service ... + end + + SemanticLogger.tagged(user: "Jack", zip_code: 12345) do + # All log entries in this block will include the above named tags + logger.debug("Hello World") + end + + # raise "exception" + end end end diff --git a/config/application.rb b/config/application.rb index b1603c5..259ff9f 100644 --- a/config/application.rb +++ b/config/application.rb @@ -10,19 +10,12 @@ class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. config.load_defaults 7.0 + # Efficient logging with Semantic Logger + require_relative "../lib/formatters/basic_formatter" + config.semantic_logger.add_appender(io: $stdout, formatter: BasicFormatter.new) + config.rails_semantic_logger.add_file_appender = false config.rails_semantic_logger.semantic = false - config.rails_semantic_logger.started = false + config.rails_semantic_logger.started = true config.rails_semantic_logger.processing = true config.rails_semantic_logger.rendered = true - - config.semantic_logger.backtrace_level = :info - config.rails_semantic_logger.ap_options = { multiline: true } - config.colorize_logging = false - config.rails_semantic_logger.console_logger = false - - $stdout.sync = true - config.rails_semantic_logger.add_file_appender = false - - require_relative "../lib/formatters/basic_formatter" - config.semantic_logger.add_appender(io: $stdout, formatter: BasicFormatter.new) end diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index 264067f..bdda1ac 100644 --- a/lib/formatters/basic_formatter.rb +++ b/lib/formatters/basic_formatter.rb @@ -1,15 +1,64 @@ # My Custom colorized formatter class BasicFormatter < SemanticLogger::Formatters::Color - ANSI_REVERSED_ERROR = "\e[1m\e[7m\e[91m".freeze + ANSI_REVERSED_WARNING = "\e[0;30;43m".freeze + ANSI_REVERSED_ERROR = "\e[1;30;41m".freeze + ANSI_GRAY = "\e[90m".freeze # Return the complete log level name in uppercase def initialize - # super(ap: { multiline: true }, - # time_format: "%H:%M:%S", - # color_map: { - # info: SemanticLogger::AnsiColors::RED, - # warn: SemanticLogger::AnsiColors::YELLOW, - # }) - super(time_format: "%H:%M:%S", color_map: ColorMap.new(info: SemanticLogger::AnsiColors::RED, warn: SemanticLogger::AnsiColors::YELLOW)) + super(time_format: "%H:%M:%S", + color_map: ColorMap.new( + debug: ANSI_GRAY, + info: SemanticLogger::AnsiColors::GREEN, + warn: SemanticLogger::AnsiColors::YELLOW, + )) + end + + def time + "#{color}#{format_time(log.time)}#{color_map.clear}" if time_format + end + + def message + return unless log.message + + prefix = "#{color}--#{color_map.clear}" + + case log.level + when :info + message = if log.name == "Rails" && log.message.starts_with?("Started") + log.message.split("for")[0] + else + log.message + end + "#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{message}#{color_map.clear}" + when :warn + "#{prefix} #{ANSI_REVERSED_WARNING}#{log.message}#{color_map.clear}" + when :error, :fatal + "#{prefix} #{ANSI_REVERSED_ERROR}#{log.message}#{color_map.clear}" + else + "#{prefix} #{color}#{log.message}#{color_map.clear}" + end + end + + def tags; end + + def process_info + fname = file_name_and_line + "#{color}[#{fname}]#{color_map.clear}" if fname + end + + def exception + return unless log.exception + + root_path = Rails.root.to_s + backtrace = log.exception.backtrace.select do |line| + line.starts_with?(root_path) + end + + if backtrace.count.positive? + "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}#{color_map.clear} #{color}#{log.exception.message}#{color_map.clear}#{SemanticLogger::AnsiColors::WHITE}\n\t#{backtrace.join("\n\t")}#{color_map.clear}\n\n" + else + "-- #{ANSI_REVERSED_ERROR}#{log.exception.class}: #{log.exception.message}#{color_map.clear}\n\n" + end end end