Skip to content

Commit

Permalink
feat: add lograge to improve logging (#5423)
Browse files Browse the repository at this point in the history
- 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>
  • Loading branch information
vishnu-narayanan and sojan-official authored Apr 7, 2023
1 parent a521762 commit 71c5a1e
Show file tree
Hide file tree
Showing 6 changed files with 56 additions and 12 deletions.
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
28 changes: 17 additions & 11 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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'
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -824,6 +831,7 @@ DEPENDENCIES
line-bot-api
liquid
listen
lograge (~> 0.12.0)
maxminddb
mock_redis
net-imap
Expand Down
1 change: 0 additions & 1 deletion config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,5 +76,4 @@
Bullet.bullet_logger = true
Bullet.rails_logger = true
end

end
28 changes: 28 additions & 0 deletions config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions config/initializers/sidekiq.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
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
Expand Down

0 comments on commit 71c5a1e

Please sign in to comment.