Browse Source

beginning of colored logging

pagy
pvincent 12 months ago
parent
commit
5ce4b0c704
  1. 2
      .rubocop.yml
  2. 2
      Gemfile
  3. 1
      Gemfile.lock
  4. 6
      app/controllers/scores_controller.rb
  5. 9
      config/application.rb
  6. 13
      config/environments/development.rb
  7. 20
      lib/formatter/colored_formatter.rb

2
.rubocop.yml

@ -0,0 +1,2 @@
Style/FrozenStringLiteralComment:
Enabled: false

2
Gemfile

@ -61,6 +61,7 @@ group :development do
gem 'error_highlight', '>= 0.4.0', platforms: [:ruby]
gem 'rainbow'
gem 'rubocop', require: false
gem 'rubocop-packaging'
gem 'rubocop-performance'
@ -68,7 +69,6 @@ group :development do
gem 'rubocop-shopify'
gem 'rubocop-thread_safety'
gem 'ruby-lsp-rails'
# gem 'sorbet'
end
group :test do

1
Gemfile.lock

@ -321,6 +321,7 @@ DEPENDENCIES
pg (~> 1.1)
puma (>= 5.0)
rails (~> 7.1.2)
rainbow
rubocop
rubocop-packaging
rubocop-performance

6
app/controllers/scores_controller.rb

@ -5,7 +5,11 @@ class ScoresController < ApplicationController
# GET /scores
def index
@scores = Score.all
puts 'hello'
logger.info 'this is an information'
logger.warn 'this is a warning'
logger.error 'this is an error'
logger.debug 'this is a debug message'
logger.info @scores
end
# GET /scores/1

9
config/application.rb

@ -1,12 +1,12 @@
require_relative "boot"
require "rails/all"
require_relative 'boot'
require 'rails/all'
# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)
module Debug
# Main Application
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 7.1
@ -14,7 +14,7 @@ module Debug
# Please, add to the `ignore` list any other `lib` subdirectories that do
# not contain `.rb` files, or that should not be reloaded or eager loaded.
# Common ones are `templates`, `generators`, or `middleware`, for example.
config.autoload_lib(ignore: %w(assets tasks))
config.autoload_lib(ignore: %w[assets tasks])
# Configuration for the application, engines, and railties goes here.
#
@ -23,5 +23,6 @@ module Debug
#
# config.time_zone = "Central Time (US & Canada)"
# config.eager_load_paths << Rails.root.join("extras")
#
end
end

13
config/environments/development.rb

@ -1,6 +1,8 @@
require "active_support/core_ext/integer/time"
require 'active_support/core_ext/integer/time'
require_relative '../../lib/formatter/colored_formatter'
Rainbow.enabled = true
Rails.application.configure do
Rails.application.configure do # rubocop:disable Metrics/BlockLength
# Settings specified here will take precedence over those in config/application.rb.
# In the development environment your application's code is reloaded any time
@ -19,13 +21,13 @@ Rails.application.configure do
# Enable/disable caching. By default caching is disabled.
# Run rails dev:cache to toggle caching.
if Rails.root.join("tmp/caching-dev.txt").exist?
if Rails.root.join('tmp/caching-dev.txt').exist?
config.action_controller.perform_caching = true
config.action_controller.enable_fragment_cache_logging = true
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
@ -73,4 +75,7 @@ Rails.application.configure do
# Raise error when a before_action's only/except options reference missing actions
config.action_controller.raise_on_missing_callback_actions = true
# Colorful Simple Logger Formatter
config.log_formatter = ColoredFormatter.new
end

20
lib/formatter/colored_formatter.rb

@ -0,0 +1,20 @@
require 'rainbow'
# ColoredFormatter outputs colored message according to severity
class ColoredFormatter < Logger::Formatter
def call(severity, _time, _program_name, message) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength
full_message = message.is_a?(String) ? message : message.inspect
case severity
when 'DEBUG'
"#{Rainbow(severity).gray.bright.underline.bg(:yellow)} - #{Rainbow(full_message).gray}\n"
when 'INFO'
"#{Rainbow(severity).green.bright.underline.bg(:yellow)} - #{Rainbow(full_message).green}\n"
when 'WARN'
"#{Rainbow(severity).orange.bright.underline} - #{Rainbow(full_message).orange}\n"
when 'ERROR'
"#{Rainbow(severity).red.bright.underline} - #{Rainbow(full_message).red}\n"
else
"unknown severity #{severity}\n"
end
end
end
Loading…
Cancel
Save