class Ably::Logger

Logger unifies logging for debug, info, warn, error, and fatal messages. A new Ably client uses this Logger and sets the appropriate log level. A custom Logger can be configured when instantiating the client, refer to the {Ably::Rest::Client} and {Ably::Realtime::Client} documentation

Attributes

custom_logger[R]

If a custom logger is being used with this Logger, this property is not nil @return {nil,Object}

log_level[R]

The log level ranging from DEBUG to FATAL, refer to www.ruby-doc.org/stdlib-3.1.1/libdoc/logger/rdoc/Logger.html @return {Integer}

log_mutex[R]
logger[R]

The logger used by this class, defaults to {www.ruby-doc.org/stdlib-3.1.1/libdoc/logger/rdoc/Logger.html Ruby Logger} @return {Object,Logger}

Public Class Methods

new(client, log_level, custom_logger = nil) click to toggle source

@param client [Ably::Rest::Client,Ably::Realtime::Client] Rest or Realtime Ably client @param log_level [Integer] {www.ruby-doc.org/stdlib-3.1.1/libdoc/logger/rdoc/Logger.html Ruby Logger} log level @param custom_logger [nil,Object] A custom logger can optionally be used instead of the,

however it must provide a {http://www.ruby-doc.org/stdlib-3.1.1/libdoc/logger/rdoc/Logger.html Ruby Logger} compatible interface.
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 14
def initialize(client, log_level, custom_logger = nil)
  @client        = client
  @custom_logger = custom_logger
  @logger        = custom_logger || default_logger
  @log_level     = log_level

  ensure_logger_interface_is_valid

  @logger.level = log_level

  @log_mutex = Mutex.new
end

Private Instance Methods

client() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 55
def client
  @client
end
color(color_value, string) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 59
def color(color_value, string)
  "\033[#{color_value}m#{string}\033[0m"
end
connection_id() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 75
def connection_id
  if realtime?
    if client.connection.id
      "[#{cyan(client.connection.id)}] "
    else
      "[ #{cyan('--')} ] "
    end
  end
end
cyan(string) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 71
def cyan(string)
  color(36, string)
end
default_logger() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 89
def default_logger
  ::Logger.new(STDOUT).tap do |logger|
    logger.formatter = lambda do |severity, datetime, progname, msg|
      severity = ::Logger::SEV_LABEL.index(severity) if severity.kind_of?(String)

      formatted_date = if severity == ::Logger::DEBUG
        datetime.strftime("%H:%M:%S.%L")
      else
        datetime.strftime("%Y-%m-%d %H:%M:%S.%L")
      end

      severity_label = if severity <= ::Logger::INFO
        magenta(::Logger::SEV_LABEL[severity])
      else
        red(::Logger::SEV_LABEL[severity])
      end

      "#{formatted_date} #{severity_label} #{connection_id}#{msg}\n"
    end
  end
end
ensure_logger_interface_is_valid() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 111
def ensure_logger_interface_is_valid
  %w(fatal error warn info debug level level=).each do |method|
    unless logger.respond_to?(method)
      raise ArgumentError, "The custom Logger's interface does not provide the method '#{method}'"
    end
  end
end
magenta(string) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 67
def magenta(string)
  color(35, string)
end
realtime?() click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 85
def realtime?
  defined?(Ably::Realtime::Client) && client.kind_of?(Ably::Realtime::Client)
end
red(string) click to toggle source
# File lib/submodules/ably-ruby/lib/ably/logger.rb, line 63
def red(string)
  color(31, string)
end