Module: Startback::Support::Robustness::Tools

Defined in:
lib/startback/support/robustness.rb

Overview

Included to avoid poluting the space of the including classes.

Class Method Summary collapse

Class Method Details

.default_loggerObject



42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/startback/support/robustness.rb', line 42

def default_logger
  @@default_logger ||= begin
    l = ::Logger.new(STDOUT)
    l.formatter = LogFormatter.new
    l.warn(op: "#{self}", op_data: {
      msg: "Using default logger to STDOUT",
      caller: caller
    })
    @@default_logger = l
  end
  @@default_logger
end

.logger_for(arg) ⇒ Object



56
57
58
59
60
61
# File 'lib/startback/support/robustness.rb', line 56

def logger_for(arg)
  return arg if arg.is_a?(::Logger)
  return arg.logger if arg.is_a?(Context) && arg.logger
  return logger_for(arg.context) if arg.respond_to?(:context, false)
  default_logger
end

.parse_args(log_msg, method = nil, context = nil, extra = nil) ⇒ Object



64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/startback/support/robustness.rb', line 64

def parse_args(log_msg, method = nil, context = nil, extra = nil)
  method, context, extra = nil, method, context unless method.is_a?(String)
  context, extra = nil, context if context.is_a?(Hash) || context.is_a?(String) && extra.nil?
  extra = { op_data: { message: extra } } if extra.is_a?(String)
  logger = logger_for(context) || logger_for(log_msg)
  log_msg = if log_msg.is_a?(Hash)
    log_msg.dup
  elsif log_msg.is_a?(String)
    log_msg = { op: "#{log_msg}#{method.nil? ? '' : '#'+method.to_s}" }
  elsif log_msg.is_a?(Exception)
    log_msg = { error: log_msg }
  else
    log_msg = log_msg.class unless log_msg.is_a?(Module)
    log_msg = { op: "#{log_msg.name}##{method}" }
  end
  log_msg.merge!(extra) if extra
  [ log_msg, logger ]
end