Class: Motion::LogHelper

Inherits:
Object
  • Object
show all
Defined in:
lib/motion/log_helper.rb

Constant Summary collapse

BACKTRACE_FRAMES =
5
DEFAULT_TAG =
"Motion"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger: nil, tag: nil) ⇒ LogHelper

Returns a new instance of LogHelper.



23
24
25
26
# File 'lib/motion/log_helper.rb', line 23

def initialize(logger: nil, tag: nil)
  @logger = logger || Rails.logger
  @tag = tag || DEFAULT_TAG
end

Instance Attribute Details

#loggerObject (readonly)

Returns the value of attribute logger.



21
22
23
# File 'lib/motion/log_helper.rb', line 21

def logger
  @logger
end

#tagObject (readonly)

Returns the value of attribute tag.



21
22
23
# File 'lib/motion/log_helper.rb', line 21

def tag
  @tag
end

Class Method Details

.for_channel(channel, logger: channel.connection.logger) ⇒ Object



13
14
15
# File 'lib/motion/log_helper.rb', line 13

def self.for_channel(channel, logger: channel.connection.logger)
  new(logger: logger, tag: DEFAULT_TAG)
end

.for_component(component, logger: nil) ⇒ Object



17
18
19
# File 'lib/motion/log_helper.rb', line 17

def self.for_component(component, logger: nil)
  new(logger: logger, tag: "#{component.class}:#{component.object_id}")
end

Instance Method Details

#error(message, error: nil) ⇒ Object



28
29
30
31
32
33
34
# File 'lib/motion/log_helper.rb', line 28

def error(message, error: nil)
  error_info = error ? ":\n#{indent(format_exception(error))}" : ""

  logger.error("[#{tag}] #{message}#{error_info}")

  Motion.notify_error(error, message)
end

#for_component(component) ⇒ Object



50
51
52
# File 'lib/motion/log_helper.rb', line 50

def for_component(component)
  self.class.for_component(component, logger: logger)
end

#info(message) ⇒ Object



36
37
38
# File 'lib/motion/log_helper.rb', line 36

def info(message)
  logger.info("[#{tag}] #{message}")
end

#timing(message) ⇒ Object



40
41
42
43
44
45
46
47
48
# File 'lib/motion/log_helper.rb', line 40

def timing(message)
  start_time = Time.now
  result = yield
  end_time = Time.now

  info("#{message} (in #{format_duration(end_time - start_time)})")

  result
end