3

How do I properly log something from my own middleware which runs in a context of Rails application? Or, more generally, how do I detect that there's some logging middleware on the stack and use it?

synapse
  • 5,588
  • 6
  • 35
  • 65
  • i asked a similar/same question here: http://stackoverflow.com/questions/17331549/can-the-rails-logger-be-accessed-from-within-a-rack-middleware – John Bachir Jun 26 '13 at 22:28

1 Answers1

0

Try using the rack common logger for loggin http://rack.rubyforge.org/doc/Rack/CommonLogger.html

Raghu
  • 2,543
  • 1
  • 19
  • 24
  • why do I need this if I have a full-fledged Rails app? I could just use Rails.logger (that's what I'm doing now). I'm asking about a proper way to do it in a standalone Rack middleware which obviously shouldn't depend on a specific logging framework – synapse Oct 19 '12 at 18:24
  • Ohh i misunderstood the question. for a standalone app you will need to use the standard ruby output $stdout . What I would do is , create a log file with a path pointing to the root of your rack app and log the standard output to that file which looks something like this log = File.new("rack.log", "a+") $stdout.reopen(log) $stderr.reopen(log) – Raghu Oct 19 '12 at 18:36
  • 1
    That link is useless. It doesn't explain how to use the logger. – B Seven Jun 26 '13 at 20:28
  • @Raghu but the point is to use whatever logger Rails is already using. So is your answer "this is not possible"? – John Bachir Jun 26 '13 at 22:24
  • @john - Actually i checked again and the common logger wont work but In another stackoverflow thread guy has written his own custom logger method for this middleware to get the logging done. See if this thread will be of your help http://stackoverflow.com/questions/2239240/use-rackcommonlogger-in-sinatra – Raghu Jun 26 '13 at 23:23
  • @Raghu that's cool -- but for my purposes the goal is to use the same logger, so that if it happens to change in the rails app, it will change everywhere (like development vs. production config, and all the various tags and level configs that go along with that) – John Bachir Jun 27 '13 at 05:37
  • @synapse I've just now run into a Rack middleware library where the prior developers used the Rails logger. Unfortunately I'm now having to replace / stub the entire Rails logger setup throughout the library as they didn't write any specs, but I don't want the entire Rails overhead just to run the test suite. Just some food for thought. Perhaps a configurable logger that takes a block might be preferred to solve both use cases? – stevenhaddox Oct 22 '14 at 18:32