Browse Source

basic_formatter for prod purpose

main
pvincent 6 months ago
parent
commit
fcf44f8ab0
  1. 8
      Gemfile
  2. 2
      Gemfile.lock
  3. 6
      app/javascript/application.js
  4. 1
      app/javascript/controllers/application.js
  5. 82
      lib/formatters/basic_formatter.rb

8
Gemfile

@ -2,6 +2,7 @@ source 'https://rubygems.org'
ruby '3.1.2'
gem 'bootsnap', require: false
gem 'dotenv-rails'
gem 'importmap-rails'
gem 'pagy'
gem 'pg'
@ -12,11 +13,11 @@ gem 'rails_semantic_logger'
gem 'sprockets-rails'
gem 'stimulus-rails'
gem 'tailwindcss-rails'
gem 'turbo-rails'
group :development do
gem 'amazing_print'
gem 'debug'
gem 'dotenv-rails'
gem 'error_highlight'
gem 'htmlbeautifier'
gem 'rails_live_reload'
@ -28,5 +29,8 @@ group :development do
gem 'rubocop-shopify'
gem 'rubocop-thread_safety'
gem 'ruby-lsp-rails'
gem 'turbo-rails'
end
group :production do
gem 'syslog_protocol'
end

2
Gemfile.lock

@ -269,6 +269,7 @@ GEM
railties (>= 6.0.0)
stringio (3.1.1)
strscan (3.1.0)
syslog_protocol (0.9.2)
tailwindcss-rails (2.6.1)
railties (>= 7.0.0)
tailwindcss-rails (2.6.1-aarch64-linux)
@ -329,6 +330,7 @@ DEPENDENCIES
ruby-lsp-rails
sprockets-rails
stimulus-rails
syslog_protocol
tailwindcss-rails
turbo-rails

6
app/javascript/application.js

@ -1,3 +1,6 @@
// Js Controllers
import "controllers"
// Turbo
import "@hotwired/turbo-rails"
Turbo.setProgressBarDelay(300)
@ -6,9 +9,6 @@ Turbo.setProgressBarDelay(300)
import Toastify from 'toastify-js'
window.Toastify = Toastify
// Js Controllers
import "controllers"
// Font Awesome
import { far } from "@fortawesome/free-regular-svg-icons"
import { fas } from "@fortawesome/free-solid-svg-icons"

1
app/javascript/controllers/application.js

@ -1,5 +1,4 @@
import { Application } from "@hotwired/stimulus"
const application = Application.start()
// Configure Stimulus development experience

82
lib/formatters/basic_formatter.rb

@ -0,0 +1,82 @@
# My Custom colorized formatter
class BasicFormatter < SemanticLogger::Formatters::Color
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(time_format: '%H:%M:%S',
color_map: ColorMap.new(
debug: ANSI_GRAY,
info: SemanticLogger::AnsiColors::GREEN,
warn: SemanticLogger::AnsiColors::YELLOW
))
@time_format = nil if File.exist?(File.join(Rails.root, 'Procfile.dev'))
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 = log.message
message = message&.rstrip if log.name == 'Rails' && message.starts_with?('Completed')
if log.name == 'Rails' && message.starts_with?('Started')
message = message.split('for')[0]
puts '' if Rails.env.development?
end
if log.name == 'Rails' || log.name == 'ActionView::Base'
"#{prefix} #{ANSI_GRAY}#{message}#{color_map.clear}"
else
"#{prefix} #{SemanticLogger::AnsiColors::WHITE}#{message}#{color_map.clear}"
end
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
def call(log, logger)
self.color = color_map[log.level]
self.log = log
self.logger = logger
if @time_format
[time, level, process_info, tags, named_tags, duration, name, message, payload, exception].compact.join(' ')
else
[tags, named_tags, duration, name, message, payload, exception].compact.join(' ')
end
end
end
Loading…
Cancel
Save