Skip to content

Commit

Permalink
added lograge for opensearch
Browse files Browse the repository at this point in the history
  • Loading branch information
briri committed Jan 29, 2025
1 parent d706c70 commit 6403f36
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 0 deletions.
6 changes: 6 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,12 @@ gem 'daemons'
# See: https://github.com/igorkasyanchuk/active_storage_validations
gem 'active_storage_validations'

# Lograge is an attempt to bring sanity to Rails' noisy and unusable, unparsable and, in the
# context of running multiple processes and servers, unreadable default logging output.
#
# See: https://github.com/roidrage/lograge
gem 'lograge'

# ================================= #
# ENVIRONMENT SPECIFIC DEPENDENCIES #
# ================================= #
Expand Down
8 changes: 8 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,11 @@ GEM
rb-inotify (~> 0.9, >= 0.9.10)
locale (2.1.4)
logger (1.6.1)
lograge (0.14.0)
actionpack (>= 4)
activesupport (>= 4)
railties (>= 4)
request_store (~> 1.0)
loofah (2.23.1)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -563,6 +568,8 @@ GEM
regexp_parser (2.9.2)
reline (0.5.10)
io-console (~> 0.5)
request_store (1.7.0)
rack (>= 1.4)
responders (3.1.1)
actionpack (>= 5.2)
railties (>= 5.2)
Expand Down Expand Up @@ -767,6 +774,7 @@ DEPENDENCIES
kaminari
ledermann-rails-settings
listen
lograge
mail
mimemagic
mocha
Expand Down
37 changes: 37 additions & 0 deletions config/initializers/lograge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
Rails.application.configure do
config.lograge.enabled = true

# Use the LogStash format
config.lograge.formatter = Lograge::Formatters::Logstash.new

# Include controller info in the available log payload
config.lograge.custom_payload do |controller|
{
host: controller.request.host,
user_id: controller.current_user.try(:id)
params: controller.params
}
end

# Include the custom info from the event and payload
config.lograge.custom_options = lambda do |event|
param_exceptions = %w(controller action format id)

{
# Timestamp
time: event.time,
# Controller params
params: event.payload[:params].except(*param_exceptions),
# The current user
user: event.payload[:user_id],
# Caller
host: event.payload[:host]
}
end

# Continue creating the basic Rails logs
config.lograge.keep_original_rails_log = true

# Define the location of the Lograge format
config.lograge.logger = ActiveSupport::Logger.new "#{Rails.root}/log/lograge_#{Rails.env}.log"
end

0 comments on commit 6403f36

Please sign in to comment.