Created
April 2, 2012 13:34
-
-
Save timruffles/2283452 to your computer and use it in GitHub Desktop.
ideas on tagging logs for rails
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
module PrefixedLogger | |
def prefixed_logger prefix | |
class << self | |
attr_reader :logger | |
end | |
@logger = ActiveSupport.TaggedLogger.new(Rails.logger) | |
@logger.instance_eval do | |
define_method :add do |severity, msg, *args, &block| | |
msg = "#{prefix} #{msg}" | |
super severity, msg, *args, &block | |
end | |
end | |
end | |
def tag_logs with, &block | |
if with < ActiveRecord::Base | |
with = "#{with.class}.#{with.id}" | |
end | |
ActiveSupport.TaggedLogger.new(Rails.logger).tagged(with, &block) | |
end | |
module ControllerMacros | |
def tags_logs_with action, &block | |
prefix = instance_eval(block) | |
override = Module.new | |
define_method action do |*args| | |
ActiveRecord::TaggedLogger.new(Rails.logger).tagged(prefix) do | |
super | |
end | |
end | |
end | |
self.send :include, override | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment