Logging all method calls in a Rails app
It's easy to do. Just add 5 lines of code into your script/server:
#!/usr/bin/env ruby
set_trace_func proc {
|event, file, line, id, binding, classname|
if event == "call" or event == "return"
printf "%8s %s:%-2d %10s %8s\n", event, file, line, id, classname
end
}
require File.expand_path('../../config/boot', __FILE__)
require 'commands/server'
It's described at http://phrogz.net/ProgrammingRuby/ospace.html#tracingyourprogramsexecution
Your application will become quite slow and you might get more output than you want. You can easily add more conditions on file/class/function names to avoid printing unwanted stuff.
How do you log the callback methods being called?
Think about it:
module Log
def log *args
args.each do |m|
alias_method m.to_s + '_old', m
define_method m do |*args|
send(m.to_s + '_old', *args)
puts "#{m} is called"
end
end
end
end
class C
def m1
puts 'm1'
end
def m2
puts 'm2'
end
extend Log
log :m1, :m2
end
C.new.m1
C.new.m2
Provides:
m1
m1 is called
m2
m2 is called
How to make logging in rails libs works like in models and controllers
At the end I found a better way, I just need to include Logging.globally on top of the module where I want that behavior:
# lib/custom_lib.rb
class CustomLib
include Logging.globally
def initialize
logger.info 'foo'
end
end
Ruby - share logger instance among module/classes
With the design you've laid out, it looks like the easiest solution is to give Crawler a module method that returns a module ivar.
module Crawler
def self.logger
@logger
end
def self.logger=(logger)
@logger = logger
end
end
Or you could use "class <<self
magic" if you wanted:
module Crawler
class <<self
attr_accessor :logger
end
end
It does the exact same thing.
How to decouple functionality and logging in a ruby method
I think Avdi Grimm has a good explanation of the technique you could use. The idea is to extract logging (or anything else) to the listener class and publish events to that listener, basic example would be
class Task
# ...
def add_listener(listener)
(@listeners ||= []) << listener
end
# ...
def notify_listeners(event_name, *args)
@listeners && @listeners.each do |listener|
if listener.respond_to?(event_name)
listener.public_send(event_name, self, *args)
end
end
end
end
and do sth like
task = Task.new
task.add_lestener(YourLoggerClass.new)
task.notify_listeners(:start_logging)
task.notify_listeners(:end_logging)
In Rails, how can one log the entirety of each incoming HTTP request?
I appreciate your response, gunderson, even though it didn't quite lead me to a solution. I ended up using a third party tool, PacketPeeper, to attach to my loopback device and report all TCP traffic.
My issue was caused by an oversight in the client-side networking library I was using (that erroneously reported my XML/JSON as a multipart form).
Rails: Log stuff to rails app logs from any ruby class
If you want to create your own logger:
class MyClass
def initialize
@app_logger = Logger.new("#{Rails.root}/log/app.log")
end
def othermethod
@app_logger.info("anything here")
end
end
Related Topics
Count Number of Days Between Two Dates
When to Use a Lambda in Ruby on Rails
Embedding JSON Data into Yaml File
Rspec: How to Stub an Instance Method Called by Constructor
What Does the Fail Keyword Do in Ruby
Ruby: Extracting Words from String
Dynamically Creating a Multi-Dimensional Hash in Ruby
Creating a Capistrano Task That Performs Different Tasks Based on Role
Factorygirl Association Model Trouble: "Systemstackerror: Stack Level Too Deep"
Rspec: How to Test If a Method Was Called
Change the Context/Binding Inside a Block in Ruby
Preferred Ruby Plugin for Eclipse
How to Get the Line of Code That Triggers a Query
How to Check If a Variable Is a Number or a String
Ruby: Proc.New { 'Waffles' } VS. Proc { 'Waffles' }
How to Replace the Unicode Gem on Ruby 1.9
Redis or Mongo for Determining If a Number Falls Within Ranges