Browse Source

clean logging

main
pvincent 1 year ago
parent
commit
24c8240e6b
  1. 2
      README.md
  2. 9
      app/controllers/application_controller.rb
  3. 2
      app/controllers/main_controller.rb
  4. 1
      config/environments/production.rb
  5. 2
      lib/formatters/basic_formatter.rb

2
README.md

@ -11,6 +11,8 @@
## Interesting Gems to try out ## Interesting Gems to try out
* [ ] ViewComponent ??
* [ ] Devise Vs Authentication-Zero
* [ ] Devise * [ ] Devise
* [ ] **Noticed** * [ ] **Noticed**
* [ ] CANCANCAN * [ ] CANCANCAN

9
app/controllers/application_controller.rb

@ -2,8 +2,15 @@ class ApplicationController < ActionController::Base
MAX_DURATION_TO_COMPLETE_IN_SECONDS = 3 MAX_DURATION_TO_COMPLETE_IN_SECONDS = 3
around_action do |_, action| around_action do |_, action|
logger.measure_error "Took too long to complete, please review your code as asynchronous task", min_duration: MAX_DURATION_TO_COMPLETE_IN_SECONDS * 1000 do
start = Process.clock_gettime(Process::CLOCK_MONOTONIC)
begin
action.call action.call
ensure
ActionView::Base.logger.level = :info
duration = Process.clock_gettime(Process::CLOCK_MONOTONIC) - start
if duration > MAX_DURATION_TO_COMPLETE_IN_SECONDS
logger.warn("processing took too long to complete, please review your code as asynchronous job!", duration: duration * 1_000)
end
end end
end end
end end

2
app/controllers/main_controller.rb

@ -8,6 +8,8 @@ class MainController < ApplicationController
logger.info "line1\nline2\nline3" logger.info "line1\nline2\nline3"
logger.warn "a warning" logger.warn "a warning"
logger.error "an error" logger.error "an error"
# raise "this is an error"
sleep 3 sleep 3
end end
end end

1
config/environments/production.rb

@ -92,6 +92,5 @@ Rails.application.configure do
config.active_record.dump_schema_after_migration = false config.active_record.dump_schema_after_migration = false
# Semantic Logger in console log only! # 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) config.semantic_logger.add_appender(file_name: "log/production.log", formatter: BasicFormatter.new)
end end

2
lib/formatters/basic_formatter.rb

@ -75,7 +75,7 @@ class BasicFormatter < SemanticLogger::Formatters::Color
if @time_format if @time_format
[time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(" ") [time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
else else
[level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
[tags, named_tags, duration, name, message, payload, exception].compact.join(" ")
end end
end end
end end
Loading…
Cancel
Save