What is the best way to prevent rail processing logs from interleaving records from different requests?

Prior to rails 3.2, this was prevented by default. With rails 3.2, there seems to be no clear solution. In the comments in the commit commit, Aaron suggested marking the log lines with the pid process and / or request uuid, which does not satisfy our Operations staff. I saw some random solutions, and I don't know how much they solve the problem. This seems like a pretty commonplace problem that everyone will have; What solutions do others recommend? Do other people just rely on NewRelic to log the data they care about?

Aaron commit

+4
source share
2 answers

We ended up writing a slightly awful patch to get back to the old behavior:

config.after_initialize do # Reverse the deprecation of flush in BufferedLogger module ActiveSupport class BufferedLogger def flush @log_dest.flush end def respond_to?(method, include_private = false) super end end end # Let the OS buffer the log Rails.logger.instance_variable_get(:@logger).instance_variable_get(:@log_dest).sync = false end 
+1
source

Further information here: https://github.com/rails/rails/issues/5388

The above answer did not clear the log lines after each request (Rails 3.2.18). Therefore, I used an initializer based on this post :

 class NonInterleavedLoggingMiddleware def initialize(app, options = {}) @log = Rails.logger .instance_variable_get(:@logger) .instance_variable_get(:@log) .instance_variable_get(:@logdev) .instance_variable_get(:@dev) @log.sync = false @app = app end def call(env) @app.call(env) ensure @log.flush # Rails.logger.flush has no effect / is deprecated end end YourAppName::Application.config.middleware.insert_before(Rails::Rack::Logger, NonInterleavedLoggingMiddleware) 
0
source

All Articles