Browse Source

efficient logging in dark theme

main
pvincent 1 year ago
parent
commit
d03391bdf9
  1. 31
      app/controllers/welcome_controller.rb
  2. 2
      config/application.rb
  3. 6
      config/environments/development.rb
  4. 12
      config/environments/production.rb
  5. 2
      lib/formatters/basic_formatter.rb

31
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

2
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

6
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

12
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

2
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

Loading…
Cancel
Save