Skip to content

Instantly share code, notes, and snippets.

@Boztown
Created September 4, 2018 23:32
Show Gist options
  • Save Boztown/1a0538d3be73526091f451cad5c2fd81 to your computer and use it in GitHub Desktop.
Save Boztown/1a0538d3be73526091f451cad5c2fd81 to your computer and use it in GitHub Desktop.
Good old TaggedLogger!
module TaggedLogger
extend ActiveSupport::Concern
def log(message, additional_tags = [], log_level = :info)
begin
caller_name = caller[0][/`.*'/][1..-2].to_s
rescue StandardError
caller_name = "error_getting_method_name"
end
tags = []
tags += self.class::DEFAULT_LOG_TAGS if self.class.const_defined? :DEFAULT_LOG_TAGS
tags += [self.class.to_s, caller_name]
tags += additional_tags
Rails.logger.tagged(tags) do
Rails.logger.public_send log_level.to_s, message
end
end
module ClassMethods
def log(message, additional_tags = [], log_level = :info)
begin
caller_name = caller[0][/`.*'/][1..-2].to_s
rescue StandardError
caller_name = "error_getting_method_name"
end
tags = []
tags += self.class::DEFAULT_LOG_TAGS if self.class.const_defined? :DEFAULT_LOG_TAGS
tags += [self.name, caller_name]
tags += additional_tags
Rails.logger.tagged(tags) do
Rails.logger.public_send log_level.to_s, message
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment