What are the pros and cons to each of the following ways of logging a function call? This code is written in Ruby but I feel the question applies to programming in general.
Responsibility belongs to the callee
class Foo
def do_cool_stuff
# Do some cool stuff
Logger.info("I did some cool stuff.")
end
end
class Bar
def self.trigger_cool_stuff
foo = Foo.new
foo.do_cool_stuff
end
end
Bar.trigger_cool_stuff
Responsibility belongs to the caller
class Foo
def do_cool_stuff
# Do some cool stuff
end
end
class Bar
def self.trigger_cool_stuff
foo = Foo.new
foo.do_cool_stuff
Logger.info("I did some cool stuff.")
end
end
Bar.trigger_cool_stuff
My initial thought is that it's a better idea for the callee to log its own actions, because it's just clearer that your logs are coming from the bit of code that is actually performing the actions that you're logging about in the first place. However, it does seem to violate Single Responsibility Principle. Additionally, if I wanted to control some state regarding the logs, like whether or not the log happens at all, the callee now must also be aware of that, which also violates SRP.
Is there a generally accepted way of doing this, or is it more down to a matter of opinion/preference?