Module: Bones::RPC::Loggable

Included in:
Bones::RPC
Defined in:
lib/bones/rpc/loggable.rb

Overview

Contains behaviour for logging.

Since:

  • 1.0.0

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.debug(prefix, payload, runtime) ⇒ Object

Log the payload to debug.

Examples:

Log to debug.

Loggable.debug("BONES-RPC", payload "30.012ms")

Parameters:

  • prefix (String)

    The log prefix.

  • payload (String)

    The log operations.

  • runtime (String)

    The runtime in formatted ms.

Since:

  • 2.0.0


42
43
44
# File 'lib/bones/rpc/loggable.rb', line 42

def self.debug(prefix, payload, runtime)
  Bones::RPC.logger.debug([ prefix, payload, "runtime: #{runtime}" ].join(' '))
end

.log_operations(prefix, ops, runtime) ⇒ Object

Log the provided operations.

Examples:

Log the operations.

Loggable.log_operations("BONES-RPC", {}, 30)

Parameters:

  • prefix (String)

    The prefix for all operations in the log.

  • ops (Array)

    The operations.

  • runtime (String)

    The runtime in formatted ms.

Since:

  • 2.0.0


20
21
22
23
24
25
26
27
28
29
30
# File 'lib/bones/rpc/loggable.rb', line 20

def self.log_operations(prefix, ops, runtime)
  indent  = " "*prefix.length
  if ops.length == 1
    Bones::RPC.logger.debug([ prefix, ops.first.log_inspect, "runtime: #{runtime}" ].join(' '))
  else
    first, *middle, last = ops
    Bones::RPC.logger.debug([ prefix, first.log_inspect ].join(' '))
    middle.each { |m| Bones::RPC.logger.debug([ indent, m.log_inspect ].join(' ')) }
    Bones::RPC.logger.debug([ indent, last.log_inspect, "runtime: #{runtime}" ].join(' '))
  end
end

.warn(prefix, payload, runtime) ⇒ Object

Log the payload to warn.

Examples:

Log to warn.

Loggable.warn("BONES-RPC", payload "30.012ms")

Parameters:

  • prefix (String)

    The log prefix.

  • payload (String)

    The log operations.

  • runtime (String)

    The runtime in formatted ms.

Since:

  • 2.0.0


56
57
58
# File 'lib/bones/rpc/loggable.rb', line 56

def self.warn(prefix, payload, runtime)
  Bones::RPC.logger.warn([ prefix, payload, "runtime: #{runtime}" ].join(' '))
end

Instance Method Details

#default_loggerLogger

Get the default logger.

Examples:

Get the default logger.

Loggable.default_logger

Returns:

  • (Logger)

    The default logger.

Since:

  • 1.0.0


93
94
95
96
97
# File 'lib/bones/rpc/loggable.rb', line 93

def default_logger
  logger = Logger.new(STDOUT)
  logger.level = Logger::INFO
  logger
end

#loggerLogger

Get the logger.

Examples:

Get the logger.

Loggable.logger

Returns:

  • (Logger)

    The logger.

Since:

  • 1.0.0


68
69
70
71
# File 'lib/bones/rpc/loggable.rb', line 68

def logger
  return @logger if defined?(@logger)
  @logger = rails_logger || default_logger
end

#logger=(logger) ⇒ Logger

Set the logger.

Examples:

Set the logger.

Loggable.logger = logger

Returns:

  • (Logger)

    The logger.

Since:

  • 1.0.0


107
108
109
# File 'lib/bones/rpc/loggable.rb', line 107

def logger=(logger)
  @logger = logger
end

#rails_loggerLogger

Get the rails logger.

Examples:

Get the rails logger.

Loggable.rails_logger

Returns:

  • (Logger)

    The Rails logger.

Since:

  • 1.0.0


81
82
83
# File 'lib/bones/rpc/loggable.rb', line 81

def rails_logger
  defined?(Rails) && Rails.respond_to?(:logger) && Rails.logger
end