Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lograge to improve logging #5423

Merged
merged 13 commits into from
Apr 7, 2023
Prev Previous commit
Next Next commit
chore: refactor
  • Loading branch information
sojan-official committed Apr 7, 2023
commit 6ec24bf1317f7d8a4d5b09aa48af447d8913997a
2 changes: 2 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
3 changes: 2 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,8 @@ gem 'net-imap', require: false
gem 'net-pop', require: false
gem 'net-smtp', require: false

gem 'lograge', '~> 0.12.0'
# Include logrange conditionally in intializer using env variable
gem 'lograge', '~> 0.12.0', require: false

# worked with microsoft refresh token
gem 'omniauth-oauth2'
Expand Down
44 changes: 24 additions & 20 deletions config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,28 @@
Rails.application.configure do
config.lograge.enabled = true
config.lograge.keep_original_rails_log = true
config.lograge.formatter = Lograge::Formatters::Json.new
config.colorize_logging = false
config.lograge.custom_payload do |controller|
{
host: controller.request.host,
remote_ip: controller.request.remote_ip,
user_id: controller.current_user.try(:id)
}
end
if ActiveModel::Type::Boolean.new.cast(ENV.fetch('LOGRAGE_ENABLED', false)).present?
require 'lograge'

config.lograge.custom_options = lambda do |event|
{
level: event.payload[:level]
}
end
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'
config.lograge.ignore_custom = lambda do |event|
# ignore update_presence events in log
return true if event.payload[:channel_class] == 'RoomChannel'
end
end
end