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

Make sure all logs in production has JSON format #4694

Merged
merged 5 commits into from
Nov 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
# frozen_string_literal: true

return unless defined? SemanticLogger

class CustomLogFormatter < SemanticLogger::Formatters::Raw
class CustomLogFormatter < SemanticLogger::Formatters::Json
def call(log, logger)
super

format_job_data
format_exception
format_json_message_context
format_backtrace
remove_post_params

hash.to_json
end

Expand Down Expand Up @@ -57,14 +57,3 @@ def method_is_post_or_put_or_patch?
hash.dig(:payload, :method).in?(%w[PUT POST PATCH])
end
end

unless Rails.env.local?
Rails.application.configure do
config.semantic_logger.application = Settings.application_name
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed this line because Settings.application_name is nil in all environments.

config.log_tags = [:request_id]
config.log_level = Settings.log_level
end

SemanticLogger.add_appender(io: $stdout, level: Settings.log_level, formatter: CustomLogFormatter.new)
Rails.application.config.logger.info('Application logging to STDOUT')
end
3 changes: 3 additions & 0 deletions config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,5 +68,8 @@ class Application < Rails::Application

config.exceptions_app = routes
config.active_job.queue_adapter = :sidekiq

config.log_tags = [:request_id]
config.log_level = Settings.log_level
end
end
7 changes: 7 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# frozen_string_literal: true

require "active_support/core_ext/integer/time"
require_dependency Rails.root.join('app/lib/custom_log_formatter')

Rails.application.configure do
config.x.read_only_database_url = ENV.fetch('DATABASE_URL', nil)
Expand Down Expand Up @@ -48,6 +49,12 @@

# Logging
config.rails_semantic_logger.add_file_appender = false
config.rails_semantic_logger.format = CustomLogFormatter.new
config.semantic_logger.add_appender(
io: $stdout,
level: config.log_level,
formatter: CustomLogFormatter.new
)

config.active_record.logger = nil # Don't log SQL in production

Expand Down
Loading