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:
@@ -110,6 +110,8 @@ AWS_REGION=
|
|||||||
RAILS_LOG_TO_STDOUT=true
|
RAILS_LOG_TO_STDOUT=true
|
||||||
LOG_LEVEL=info
|
LOG_LEVEL=info
|
||||||
LOG_SIZE=500
|
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
|
### This environment variables are only required if you are setting up social media channels
|
||||||
|
|
||||||
|
|||||||
28
Gemfile
28
Gemfile
@@ -149,7 +149,23 @@ gem 'net-imap', require: false
|
|||||||
gem 'net-pop', require: false
|
gem 'net-pop', require: false
|
||||||
gem 'net-smtp', 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
|
# we dont want request timing out in development while using byebug
|
||||||
gem 'rack-timeout'
|
gem 'rack-timeout'
|
||||||
end
|
end
|
||||||
@@ -206,13 +222,3 @@ group :development, :test do
|
|||||||
gem 'spring'
|
gem 'spring'
|
||||||
gem 'spring-watcher-listen'
|
gem 'spring-watcher-listen'
|
||||||
end
|
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'
|
|
||||||
|
|||||||
@@ -416,6 +416,11 @@ GEM
|
|||||||
llhttp-ffi (0.4.0)
|
llhttp-ffi (0.4.0)
|
||||||
ffi-compiler (~> 1.0)
|
ffi-compiler (~> 1.0)
|
||||||
rake (~> 13.0)
|
rake (~> 13.0)
|
||||||
|
lograge (0.12.0)
|
||||||
|
actionpack (>= 4)
|
||||||
|
activesupport (>= 4)
|
||||||
|
railties (>= 4)
|
||||||
|
request_store (~> 1.0)
|
||||||
loofah (2.19.1)
|
loofah (2.19.1)
|
||||||
crass (~> 1.0.2)
|
crass (~> 1.0.2)
|
||||||
nokogiri (>= 1.5.9)
|
nokogiri (>= 1.5.9)
|
||||||
@@ -568,6 +573,8 @@ GEM
|
|||||||
declarative (< 0.1.0)
|
declarative (< 0.1.0)
|
||||||
trailblazer-option (>= 0.1.1, < 0.2.0)
|
trailblazer-option (>= 0.1.1, < 0.2.0)
|
||||||
uber (< 0.2.0)
|
uber (< 0.2.0)
|
||||||
|
request_store (1.5.1)
|
||||||
|
rack (>= 1.4)
|
||||||
responders (3.0.1)
|
responders (3.0.1)
|
||||||
actionpack (>= 5.0)
|
actionpack (>= 5.0)
|
||||||
railties (>= 5.0)
|
railties (>= 5.0)
|
||||||
@@ -824,6 +831,7 @@ DEPENDENCIES
|
|||||||
line-bot-api
|
line-bot-api
|
||||||
liquid
|
liquid
|
||||||
listen
|
listen
|
||||||
|
lograge (~> 0.12.0)
|
||||||
maxminddb
|
maxminddb
|
||||||
mock_redis
|
mock_redis
|
||||||
net-imap
|
net-imap
|
||||||
|
|||||||
@@ -76,5 +76,4 @@ Rails.application.configure do
|
|||||||
Bullet.bullet_logger = true
|
Bullet.bullet_logger = true
|
||||||
Bullet.rails_logger = true
|
Bullet.rails_logger = true
|
||||||
end
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|||||||
28
config/initializers/lograge.rb
Normal file
28
config/initializers/lograge.rb
Normal 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
|
||||||
@@ -7,6 +7,7 @@ Sidekiq.configure_client do |config|
|
|||||||
end
|
end
|
||||||
|
|
||||||
Sidekiq.configure_server do |config|
|
Sidekiq.configure_server do |config|
|
||||||
|
config.logger.formatter = Sidekiq::Logger::Formatters::JSON.new
|
||||||
config.redis = Redis::Config.app
|
config.redis = Redis::Config.app
|
||||||
config.logger.level = Logger.const_get(ENV.fetch('LOG_LEVEL', 'info').upcase.to_s)
|
config.logger.level = Logger.const_get(ENV.fetch('LOG_LEVEL', 'info').upcase.to_s)
|
||||||
end
|
end
|
||||||
|
|||||||
Reference in New Issue
Block a user