Class: Cloudenvoy::LoggerWrapper

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudenvoy/logger_wrapper.rb

Overview

Add contextual information to logs generated by subscribers/publishers.

This class is a base class which aims at being inherited by object-specific logger wrappers. See Cloudenvoy::SubscriberLogger and Cloudenvoy::PublisherLogger.

Direct Known Subclasses

PublisherLogger, SubscriberLogger

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(loggable) ⇒ LoggerWrapper

Build a new instance of the class.

Parameters:

  • loggable (Any)

    The loggable to wrap for logging.



32
33
34
# File 'lib/cloudenvoy/logger_wrapper.rb', line 32

def initialize(loggable)
  @loggable = loggable
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Any

Delegate all methods to the underlying logger.

Parameters:

  • name (String, Symbol)

    The method to delegate.

  • *args (Array<any>)

    The list of method arguments.

  • &block (Proc)

    Block passed to the method.

Returns:

  • (Any)

    The method return value



125
126
127
128
129
130
131
# File 'lib/cloudenvoy/logger_wrapper.rb', line 125

def method_missing(name, *args, &block)
  if logger.respond_to?(name)
    logger.send(name, *args, &block)
  else
    super
  end
end

Class Attribute Details

.log_context_processorObject

Returns the value of attribute log_context_processor.



14
15
16
# File 'lib/cloudenvoy/logger_wrapper.rb', line 14

def log_context_processor
  @log_context_processor
end

Instance Attribute Details

#loggableObject

Returns the value of attribute loggable.



11
12
13
# File 'lib/cloudenvoy/logger_wrapper.rb', line 11

def loggable
  @loggable
end

Class Method Details

.default_context_processorProc

The default context processor. Aims at being overriden by child classes.

Returns:

  • (Proc)

    The context processor proc.



23
24
25
# File 'lib/cloudenvoy/logger_wrapper.rb', line 23

def self.default_context_processor
  @default_context_processor ||= ->(_) { {} }
end

Instance Method Details

#context_processorProc

Return the Proc responsible for formatting the log payload.

Returns:

  • (Proc)

    The context processor.



41
42
43
44
45
# File 'lib/cloudenvoy/logger_wrapper.rb', line 41

def context_processor
  @context_processor ||= loggable.class.cloudenvoy_options_hash[:log_context_processor] ||
                         self.class.log_context_processor ||
                         self.class.default_context_processor
end

#debug(msg, &block) ⇒ Object

Log an debut message.

Parameters:

  • msg (String)

    The message to log.

  • &block (Proc)

    Optional context block.



112
113
114
# File 'lib/cloudenvoy/logger_wrapper.rb', line 112

def debug(msg, &block)
  log_message(:debug, msg, &block)
end

#error(msg, &block) ⇒ Object

Log an error message.

Parameters:

  • msg (String)

    The message to log.

  • &block (Proc)

    Optional context block.



92
93
94
# File 'lib/cloudenvoy/logger_wrapper.rb', line 92

def error(msg, &block)
  log_message(:error, msg, &block)
end

#fatal(msg, &block) ⇒ Object

Log an fatal message.

Parameters:

  • msg (String)

    The message to log.

  • &block (Proc)

    Optional context block.



102
103
104
# File 'lib/cloudenvoy/logger_wrapper.rb', line 102

def fatal(msg, &block)
  log_message(:fatal, msg, &block)
end

#formatted_message(msg) ⇒ String

Format main log message.

Parameters:

  • msg (String)

    The message to log.

Returns:

  • (String)

    The formatted log message



72
73
74
# File 'lib/cloudenvoy/logger_wrapper.rb', line 72

def formatted_message(msg)
  "[Cloudenvoy][#{loggable.class}] #{msg}"
end

#info(msg, &block) ⇒ Object

Log an info message.

Parameters:

  • msg (String)

    The message to log.

  • &block (Proc)

    Optional context block.



82
83
84
# File 'lib/cloudenvoy/logger_wrapper.rb', line 82

def info(msg, &block)
  log_message(:info, msg, &block)
end

#log_blockProc

The block to pass to log messages.

Returns:

  • (Proc)

    The log block.



52
53
54
# File 'lib/cloudenvoy/logger_wrapper.rb', line 52

def log_block
  @log_block ||= proc { context_processor.call(loggable) }
end

#loggerLogger, any

Return the Cloudenvoy logger.

Returns:

  • (Logger, any)

    The cloudenvoy logger.



61
62
63
# File 'lib/cloudenvoy/logger_wrapper.rb', line 61

def logger
  Cloudenvoy.logger
end

#respond_to_missing?(name, include_private = false) ⇒ Boolean

Check if the class respond to a certain method.

Parameters:

  • name (String, Symbol)

    The name of the method.

  • include_private (Boolean) (defaults to: false)

    Whether to check private methods or not. Default to false.

Returns:

  • (Boolean)

    Return true if the class respond to this method.



141
142
143
# File 'lib/cloudenvoy/logger_wrapper.rb', line 141

def respond_to_missing?(name, include_private = false)
  logger.respond_to?(name) || super
end