Skip to content

Commit

Permalink
Remove Logstash integration
Browse files Browse the repository at this point in the history
We’ve implemented log shipping at the platform level and this custom
code to send our logs to Logstash is no longer required.
  • Loading branch information
malcolmbaig committed May 30, 2024
1 parent b5dcf87 commit 0fe71e8
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 172 deletions.
6 changes: 4 additions & 2 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ gem "clockwork"

# Generate JSON Web Tokens
gem "jwt"
gem "logstash-logger"
gem "rails_semantic_logger"

gem 'activerecord-session_store'

Expand Down Expand Up @@ -95,3 +93,7 @@ end
group :development, :test, :review do
gem "factory_bot_rails"
end

group :development, :production do
gem "rails_semantic_logger"
end
4 changes: 0 additions & 4 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -251,9 +251,6 @@ GEM
language_server-protocol (3.17.0.3)
launchy (2.5.2)
addressable (~> 2.8)
logstash-event (1.2.02)
logstash-logger (0.26.1)
logstash-event (~> 1.2)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand Down Expand Up @@ -602,7 +599,6 @@ DEPENDENCIES
jsbundling-rails
jwt
launchy
logstash-logger
okcomputer
omniauth-oauth2 (~> 1.8)
omniauth-rails_csrf_protection
Expand Down
4 changes: 4 additions & 0 deletions config/environments/development.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,4 +59,8 @@

# Uncomment if you wish to allow Action Cable access from any origin.
# config.action_cable.disable_request_forgery_protection = true
#
config.log_level = :debug # Or :info
config.log_format = :color # Console colorised non-json output
config.semantic_logger.backtrace_level = :debug # Show file and line number (expensive: not for production)
end
5 changes: 5 additions & 0 deletions config/environments/production.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,4 +80,9 @@

# Do not dump schema after migrations.
config.active_record.dump_schema_after_migration = false

config.log_level = :info # Or :warn, or :error
config.log_format = :json # For parsing in Logit
config.rails_semantic_logger.add_file_appender = false # Don't log to file
config.active_record.logger = nil # Don't log SQL
end
8 changes: 0 additions & 8 deletions config/initializers/logstash.rb

This file was deleted.

54 changes: 7 additions & 47 deletions config/initializers/semantic_logger.rb
Original file line number Diff line number Diff line change
@@ -1,50 +1,10 @@
class LogStashFormatter < SemanticLogger::Formatters::Raw
def call(log, logger)
super(log, logger)
return unless defined?(SemanticLogger)

format_exception
format_stacktrace

hash.to_json
end

def format_exception
exception_message = hash.dig(:exception, :message)
return if exception_message.blank?

hash[:message] = "Exception occured: #{exception_message}"
end

def format_stacktrace
stack_trace = hash.dig(:exception, :stack_trace)
return if stack_trace.blank?

hash[:stacktrace] = stack_trace.first(3)
hash[:exception].delete(:stack_trace)
end
Rails.application.configure do
config.semantic_logger.application = "" # This is added by logstash from its tags
config.log_tags = [:request_id] # Prepend all log lines with the following tags
end

if ENV["LOGSTASH_HOST"] && ENV["LOGSTASH_PORT"]
warn("logstash configured, sending logs there")

# For some reason logstash / elasticsearch drops events where the payload
# is a hash. These are more conveniently accessed at the top level of the
# event, anyway, so we move it there.
customize_event = ->(event) do
if event["payload"].present?
event.append(event["payload"])
event["payload"] = nil
end
end

logger =
LogStashLogger.new(
{
host: ENV["LOGSTASH_HOST"],
port: ENV["LOGSTASH_PORT"],
ssl_enable: true,
type: :tcp
}.merge(customize_event:)
)
SemanticLogger.add_appender(logger:, level: :info, formatter: LogStashFormatter.new)
end
SemanticLogger.add_appender(io: $stdout, level: Rails.application.config.log_level,
formatter: Rails.application.config.log_format)
Rails.application.config.logger.info('Application logging to STDOUT')
111 changes: 0 additions & 111 deletions spec/config/initializers/log_stash_formatter_spec.rb

This file was deleted.

0 comments on commit 0fe71e8

Please sign in to comment.