Class: Cloudtasker::WorkerLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/cloudtasker/worker_logger.rb

Overview

Add contextual information to logs generated by workers

Constant Summary collapse

DEFAULT_CONTEXT_PROCESSOR =

Only log the job meta information by default (exclude arguments)

->(worker) { worker.to_h.slice(:worker, :job_id, :job_meta, :job_queue, :task_id) }

Class Attribute Summary collapse

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker) ⇒ WorkerLogger

Build a new instance of the class.

Parameters:



21
22
23
# File 'lib/cloudtasker/worker_logger.rb', line 21

def initialize(worker)
  @worker = worker
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



139
140
141
142
143
144
145
# File 'lib/cloudtasker/worker_logger.rb', line 139

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.



10
11
12
# File 'lib/cloudtasker/worker_logger.rb', line 10

def log_context_processor
  @log_context_processor
end

Instance Attribute Details

#workerObject

Returns the value of attribute worker.



7
8
9
# File 'lib/cloudtasker/worker_logger.rb', line 7

def worker
  @worker
end

Instance Method Details

#context_processorProc

Return the Proc responsible for formatting the log payload.

Returns:

  • (Proc)

    The context processor.



30
31
32
33
34
# File 'lib/cloudtasker/worker_logger.rb', line 30

def context_processor
  @context_processor ||= worker.class.cloudtasker_options_hash[:log_context_processor] ||
                         self.class.log_context_processor ||
                         DEFAULT_CONTEXT_PROCESSOR
end

#debug(msg, &block) ⇒ Object

Log an debut message.

Parameters:

  • msg (String)

    The message to log.

  • &block (Proc)

    Optional context block.



126
127
128
# File 'lib/cloudtasker/worker_logger.rb', line 126

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.



106
107
108
# File 'lib/cloudtasker/worker_logger.rb', line 106

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.



116
117
118
# File 'lib/cloudtasker/worker_logger.rb', line 116

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



81
82
83
84
85
86
87
88
# File 'lib/cloudtasker/worker_logger.rb', line 81

def formatted_message(msg)
  if msg.is_a?(String)
    formatted_message_as_string(msg)
  else
    # Delegate object formatting to logger
    msg
  end
end

#formatted_message_as_string(msg) ⇒ String

Format the log message as string.

Parameters:

  • msg (Object)

    The log message or object.

Returns:

  • (String)

    The formatted message



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/cloudtasker/worker_logger.rb', line 61

def formatted_message_as_string(msg)
  # Format message
  msg_content = if msg.is_a?(Exception)
                  [msg.inspect, msg.backtrace].flatten(1).join("\n")
                elsif msg.is_a?(String)
                  msg
                else
                  msg.inspect
                end

  "[Cloudtasker][#{worker.class}][#{worker.job_id}] #{msg_content}"
end

#info(msg, &block) ⇒ Object

Log an info message.

Parameters:

  • msg (String)

    The message to log.

  • &block (Proc)

    Optional context block.



96
97
98
# File 'lib/cloudtasker/worker_logger.rb', line 96

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

#log_blockProc

The block to pass to log messages.

Returns:

  • (Proc)

    The log block.



41
42
43
# File 'lib/cloudtasker/worker_logger.rb', line 41

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

#loggerLogger, any

Return the Cloudtasker logger.

Returns:

  • (Logger, any)

    The cloudtasker logger.



50
51
52
# File 'lib/cloudtasker/worker_logger.rb', line 50

def logger
  Cloudtasker.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.



155
156
157
# File 'lib/cloudtasker/worker_logger.rb', line 155

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