Class: Motion::LogHelper
- Inherits:
-
Object
- Object
- Motion::LogHelper
- Defined in:
- lib/motion/log_helper.rb
Constant Summary collapse
- BACKTRACE_FRAMES =
5
- DEFAULT_TAG =
"Motion"
Instance Attribute Summary collapse
-
#logger ⇒ Object
readonly
Returns the value of attribute logger.
-
#tag ⇒ Object
readonly
Returns the value of attribute tag.
Class Method Summary collapse
- .for_channel(channel, logger: channel.connection.logger) ⇒ Object
- .for_component(component, logger: nil) ⇒ Object
Instance Method Summary collapse
- #error(message, error: nil) ⇒ Object
- #for_component(component) ⇒ Object
- #info(message) ⇒ Object
-
#initialize(logger: nil, tag: nil) ⇒ LogHelper
constructor
A new instance of LogHelper.
- #timing(message) ⇒ Object
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
#logger ⇒ Object (readonly)
Returns the value of attribute logger.
21 22 23 |
# File 'lib/motion/log_helper.rb', line 21 def logger @logger end |
#tag ⇒ Object (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(, error: nil) error_info = error ? ":\n#{indent(format_exception(error))}" : "" logger.error("[#{tag}] #{}#{error_info}") Motion.notify_error(error, ) 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() logger.info("[#{tag}] #{}") end |
#timing(message) ⇒ Object
40 41 42 43 44 45 46 47 48 |
# File 'lib/motion/log_helper.rb', line 40 def timing() start_time = Time.now result = yield end_time = Time.now info("#{} (in #{format_duration(end_time - start_time)})") result end |