feat: add lograge to improve logging (#5423)

- Add lograge gem to improve rails logging using `LOGRAGE_ENABLED` env variable
- When enabled Single line log for requests in JSON formatting
- Switch sidekiq also to use JSON formatting

Fixes: chatwoot/product#437
---------

Co-authored-by: Sojan Jose <sojan@pepalo.com>
This commit is contained in:
Vishnu Narayanan
2023-04-07 13:44:30 +05:30
committed by GitHub
parent a521762dd6
commit 71c5a1e1d4
6 changed files with 56 additions and 12 deletions

View File

@@ -110,6 +110,8 @@ AWS_REGION=
RAILS_LOG_TO_STDOUT=true
LOG_LEVEL=info
LOG_SIZE=500
# Configure this environment variable if you want to use lograge instead of rails logger
#LOGRAGE_ENABLED=true
### This environment variables are only required if you are setting up social media channels

28
Gemfile
View File

@@ -149,7 +149,23 @@ gem 'net-imap', require: false
gem 'net-pop', require: false
gem 'net-smtp', require: false
group :production, :staging do
# Include logrange conditionally in intializer using env variable
gem 'lograge', '~> 0.12.0', require: false
# worked with microsoft refresh token
gem 'omniauth-oauth2'
gem 'audited', '~> 5.2'
# need for google auth
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'omniauth-rails_csrf_protection', '~> 1.0'
### Gems required only in specific deployment environments ###
##############################################################
group :production do
# we dont want request timing out in development while using byebug
gem 'rack-timeout'
end
@@ -206,13 +222,3 @@ group :development, :test do
gem 'spring'
gem 'spring-watcher-listen'
end
# worked with microsoft refresh token
gem 'omniauth-oauth2'
gem 'audited', '~> 5.2'
# need for google auth
gem 'omniauth'
gem 'omniauth-google-oauth2'
gem 'omniauth-rails_csrf_protection', '~> 1.0'

View File

@@ -416,6 +416,11 @@ GEM
llhttp-ffi (0.4.0)
ffi-compiler (~> 1.0)
rake (~> 13.0)
lograge (0.12.0)
actionpack (>= 4)
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
loofah (2.19.1)
crass (~> 1.0.2)
nokogiri (>= 1.5.9)
@@ -568,6 +573,8 @@ GEM
declarative (< 0.1.0)
trailblazer-option (>= 0.1.1, < 0.2.0)
uber (< 0.2.0)
request_store (1.5.1)
rack (>= 1.4)
responders (3.0.1)
actionpack (>= 5.0)
railties (>= 5.0)
@@ -824,6 +831,7 @@ DEPENDENCIES
line-bot-api
liquid
listen
lograge (~> 0.12.0)
maxminddb
mock_redis
net-imap

View File

@@ -76,5 +76,4 @@ Rails.application.configure do
Bullet.bullet_logger = true
Bullet.rails_logger = true
end
end

View File

@@ -0,0 +1,28 @@
if ActiveModel::Type::Boolean.new.cast(ENV.fetch('LOGRAGE_ENABLED', false)).present?
require 'lograge'
Rails.application.configure do
config.lograge.enabled = true
config.lograge.formatter = Lograge::Formatters::Json.new
config.lograge.custom_payload do |controller|
{
host: controller.request.host,
remote_ip: controller.request.remote_ip,
user_id: controller.current_user.try(:id)
}
end
config.lograge.custom_options = lambda do |event|
param_exceptions = %w[controller action format id]
{
params: event.payload[:params]&.except(*param_exceptions)
}
end
config.lograge.ignore_custom = lambda do |event|
# ignore update_presence events in log
return true if event.payload[:channel_class] == 'RoomChannel'
end
end
end

View File

@@ -7,6 +7,7 @@ Sidekiq.configure_client do |config|
end
Sidekiq.configure_server do |config|
config.logger.formatter = Sidekiq::Logger::Formatters::JSON.new
config.redis = Redis::Config.app
config.logger.level = Logger.const_get(ENV.fetch('LOG_LEVEL', 'info').upcase.to_s)
end