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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(worker) ⇒ WorkerLogger

Build a new instance of the class.

Parameters:



69
70
71
# File 'lib/cloudtasker/worker_logger.rb', line 69

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



187
188
189
190
191
192
193
# File 'lib/cloudtasker/worker_logger.rb', line 187

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

Class Method Details

.truncate(payload, **kwargs) ⇒ Hash, Array

rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength

Truncate an array or hash payload.

This can be used to log arguments on jobs while still keeping logs to a reasonable size.

Parameters:

  • payload (Hash, Array)

    The payload to truncate

  • string_limit (Integer)

    The maximum size for strings. Set to -1 to disable.

  • array_limit (Integer)

    The maximum length for arrays. Set to -1 to disable.

  • max_depth (Hash)

    The maximum recursive depth. Set to -1 to disable.

Returns:

  • (Hash, Array)

    The truncated payload



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/cloudtasker/worker_logger.rb', line 26

def truncate(payload, **kwargs)
  depth = kwargs[:depth].to_i
  max_depth = kwargs[:max_depth] || 3
  string_limit = kwargs[:string_limit] || 64
  array_limit = kwargs[:array_limit] || 10

  case payload
  when Array
    if max_depth > -1 && depth > max_depth
      ["...#{payload.size} items..."]
    elsif array_limit > -1
      payload.take(array_limit).map { |e| truncate(e, **kwargs, depth: depth + 1) } +
        (payload.size > array_limit ? ["...#{payload.size - array_limit} items..."] : [])
    else
      payload.map { |e| truncate(e, **kwargs, depth: depth + 1) }
    end
  when Hash
    if max_depth > -1 && depth > max_depth
      '{hash}'
    else
      payload.transform_values { |e| truncate(e, **kwargs, depth: depth + 1) }
    end
  when String
    if string_limit > -1 && payload.size > string_limit
      payload.truncate(string_limit)
    else
      payload
    end
  else
    payload
  end
end

Instance Method Details

#context_processorProc

Return the Proc responsible for formatting the log payload.

Returns:

  • (Proc)

    The context processor.



78
79
80
81
82
# File 'lib/cloudtasker/worker_logger.rb', line 78

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.



174
175
176
# File 'lib/cloudtasker/worker_logger.rb', line 174

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.



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

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.



164
165
166
# File 'lib/cloudtasker/worker_logger.rb', line 164

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



129
130
131
132
133
134
135
136
# File 'lib/cloudtasker/worker_logger.rb', line 129

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



109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/cloudtasker/worker_logger.rb', line 109

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.



144
145
146
# File 'lib/cloudtasker/worker_logger.rb', line 144

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

#log_blockProc

The block to pass to log messages.

Returns:

  • (Proc)

    The log block.



89
90
91
# File 'lib/cloudtasker/worker_logger.rb', line 89

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

#loggerLogger, any

Return the Cloudtasker logger.

Returns:

  • (Logger, any)

    The cloudtasker logger.



98
99
100
# File 'lib/cloudtasker/worker_logger.rb', line 98

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.



203
204
205
# File 'lib/cloudtasker/worker_logger.rb', line 203

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