module Loggun::Helpers

Constants

DEFAULT_TYPE
SKIPPED_METHODS

Public Class Methods

included(klass) click to toggle source
# File lib/loggun/helpers.rb, line 10
def self.included(klass)
  klass.extend(InitMethods)
  klass.loggun_init
  klass.extend(ClassMethods)
end

Public Instance Methods

generate_log_transaction_id() click to toggle source
# File lib/loggun/helpers.rb, line 160
def generate_log_transaction_id
  if self.class.log_transaction_generator
    return self.class.log_transaction_generator.call(self)
  end

  "#{SecureRandom.uuid[0..7]}_#{DateTime.now.strftime('%Q')}"
end
in_log_transaction(current_type = nil, current_transaction_id = nil) { || ... } click to toggle source
# File lib/loggun/helpers.rb, line 112
def in_log_transaction(current_type = nil, current_transaction_id = nil)
  current_transaction_id ||= generate_log_transaction_id
  previous_transaction_id = self.parent_transaction_id
  previous_type = self.parent_type

  self.parent_transaction_id = self.transaction_id
  self.parent_type = self.type

  self.transaction_id = current_transaction_id
  self.type = current_type if current_type
  yield
ensure
  self.transaction_id = self.parent_transaction_id
  self.type = self.parent_type

  self.parent_transaction_id = previous_transaction_id
  self.parent_type = previous_type
end
log_type(type, method_name) click to toggle source
# File lib/loggun/helpers.rb, line 139
def log_type(type, method_name)
  klass = self.class
  type ||= DEFAULT_TYPE.dup
  type_as_arr = type.split('.')

  if type_as_arr.size == 1
    log_entity_name = klass.log_entity_name if klass.respond_to?(:log_entity_name)

    log_entity_name ||= underscore(klass.name.dup)
    type_as_arr << log_entity_name
  end

  return type unless klass.respond_to?(:log_entity_action)

  if klass.log_entity_action && klass.log_entity_action == :method_name && type_as_arr.size < 3 && method_name
    type_as_arr << method_name
  end

  type_as_arr.join('.')
end
with_log_type(current_type) { || ... } click to toggle source
# File lib/loggun/helpers.rb, line 131
def with_log_type(current_type)
  previous_type = self.type
  self.type = current_type
  yield
ensure
  self.type = previous_type
end

Private Instance Methods

logger() click to toggle source
# File lib/loggun/helpers.rb, line 186
def logger
  Loggun.logger
end
normalize(type) click to toggle source
# File lib/loggun/helpers.rb, line 170
def normalize(type)
  return unless type

  type = type.to_s.strip.tr(" \t\r\n", ' ').squeeze(' ')
  type.empty? ? nil : type
end
underscore(word) click to toggle source
# File lib/loggun/helpers.rb, line 177
def underscore(word)
  word.gsub!(/::/, '__')
  word.gsub!(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
  word.gsub!(/([a-z\d])([A-Z])/, '\1_\2')
  word.tr!('-', '_')
  word.downcase!
  word
end