Created
April 4, 2011 21:07
-
-
Save codatory/902442 to your computer and use it in GitHub Desktop.
Rails Deprecation Logger
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Stick this in config/initializers and restart your app to install | |
# Then you can log your deprecations to log/deprecations.log quickly and easily | |
# To log with this, just call: | |
# DeprecationLogger.notify('StringDescriptionOfMethod', DeprecationLogger.caller_method) | |
class DeprecationLogger < Logger | |
@logfile = File.open(Rails.root.join('log','deprecations.log'), 'a') | |
@logfile.sync = true | |
def format_message(severity,timestamp,progname,msg) | |
"#{timestamp.to_formatted_s(:db)} #{msg}\n" | |
end | |
def self.log | |
self.new(@logfile) | |
end | |
def self.notify(method, called_by) | |
self.log.info("Deprecated method #{method} was called \n #{called_by}") | |
end | |
def self.caller_method(depth=1) | |
parse_caller(caller(depth+1).first) | |
end | |
private | |
def self.parse_caller(at) | |
if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at | |
file = Regexp.last_match[1] | |
line = Regexp.last_match[2].to_i | |
method = Regexp.last_match[3] | |
"Method: #{method} @ #{file} : #{line}" | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment