From d03391bdf9c890d54d6d42e4c9a967ff11872162 Mon Sep 17 00:00:00 2001 From: pvincent Date: Fri, 26 May 2023 23:26:15 +0400 Subject: [PATCH] efficient logging in dark theme --- app/controllers/welcome_controller.rb | 31 ++++++++++++++------------- config/application.rb | 2 -- config/environments/development.rb | 6 +++++- config/environments/production.rb | 12 +++++++---- lib/formatters/basic_formatter.rb | 2 +- 5 files changed, 30 insertions(+), 23 deletions(-) diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index 41de7f5..e137d1a 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -1,22 +1,23 @@ 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_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 + # 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 + # 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 + logger.info("efficient logging in practice works better on dark theme") + # raise "exception" + # end end end diff --git a/config/application.rb b/config/application.rb index 259ff9f..d08f5d0 100644 --- a/config/application.rb +++ b/config/application.rb @@ -11,8 +11,6 @@ class Application < Rails::Application 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 = true diff --git a/config/environments/development.rb b/config/environments/development.rb index 8500f45..a74006d 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -25,7 +25,7 @@ Rails.application.configure do config.cache_store = :memory_store config.public_file_server.headers = { - "Cache-Control" => "public, max-age=#{2.days.to_i}" + "Cache-Control" => "public, max-age=#{2.days.to_i}", } else config.action_controller.perform_caching = false @@ -67,4 +67,8 @@ Rails.application.configure do # Uncomment if you wish to allow Action Cable access from any origin. # config.action_cable.disable_request_forgery_protection = true + + # Semantic Logger in console log only! + require_relative "../../lib/formatters/basic_formatter" + config.semantic_logger.add_appender(io: $stdout, formatter: BasicFormatter.new) end diff --git a/config/environments/production.rb b/config/environments/production.rb index 7a93dfe..cd9eaa8 100644 --- a/config/environments/production.rb +++ b/config/environments/production.rb @@ -13,7 +13,7 @@ Rails.application.configure do config.eager_load = true # Full error reports are disabled and caching is turned on. - config.consider_all_requests_local = false + config.consider_all_requests_local = false config.action_controller.perform_caching = true # Ensures that a master key has been made available in either ENV["RAILS_MASTER_KEY"] @@ -53,7 +53,7 @@ Rails.application.configure do config.log_level = :info # Prepend all log lines with the following tags. - config.log_tags = [ :request_id ] + config.log_tags = [:request_id] # Use a different cache store in production. # config.cache_store = :mem_cache_store @@ -83,11 +83,15 @@ Rails.application.configure do # config.logger = ActiveSupport::TaggedLogging.new(Syslog::Logger.new "app-name") if ENV["RAILS_LOG_TO_STDOUT"].present? - logger = ActiveSupport::Logger.new(STDOUT) + logger = ActiveSupport::Logger.new(STDOUT) logger.formatter = config.log_formatter - config.logger = ActiveSupport::TaggedLogging.new(logger) + config.logger = ActiveSupport::TaggedLogging.new(logger) end # Do not dump schema after migrations. config.active_record.dump_schema_after_migration = false + + # Semantic Logger in console log only! + require_relative "../../lib/formatters/basic_formatter" + config.semantic_logger.add_appender(file_name: "log/production.log", formatter: BasicFormatter.new) end diff --git a/lib/formatters/basic_formatter.rb b/lib/formatters/basic_formatter.rb index 7ccb437..9890607 100644 --- a/lib/formatters/basic_formatter.rb +++ b/lib/formatters/basic_formatter.rb @@ -31,7 +31,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color log.message end if log.name == "Rails" || log.name == "ActionView::Base" - "#{prefix} #{color}#{message}#{color_map.clear}" + "#{prefix} #{ANSI_GRAY}#{message}#{color_map.clear}" else "#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{message}#{color_map.clear}" end