Module: Loggun::Helpers

Included in:
Loggun
Defined in:
lib/loggun/helpers.rb

Defined Under Namespace

Modules: ClassMethods, InitMethods

Constant Summary collapse

SKIPPED_METHODS =
%i[
  initialize loggun logger log_modified_methods loggun_init in_log_transaction with_log_type
].freeze
DEFAULT_TYPE =
'class'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(klass) ⇒ Object



10
11
12
13
14
# File 'lib/loggun/helpers.rb', line 10

def self.included(klass)
  klass.extend(InitMethods)
  klass.loggun_init
  klass.extend(ClassMethods)
end

Instance Method Details

#generate_log_transaction_idObject



160
161
162
163
164
165
166
# 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) ⇒ Object



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
# 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) ⇒ Object



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
# 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) ⇒ Object



131
132
133
134
135
136
137
# 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