Class: Cloudtasker::WorkerLogger
- Inherits:
-
Object
- Object
- Cloudtasker::WorkerLogger
- 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
-
.log_context_processor ⇒ Object
Returns the value of attribute log_context_processor.
Instance Attribute Summary collapse
-
#worker ⇒ Object
Returns the value of attribute worker.
Class Method Summary collapse
-
.truncate(payload, **kwargs) ⇒ Hash, Array
rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/MethodLength.
Instance Method Summary collapse
-
#context_processor ⇒ Proc
Return the Proc responsible for formatting the log payload.
-
#debug(msg, &block) ⇒ Object
Log an debut message.
-
#error(msg, &block) ⇒ Object
Log an error message.
-
#fatal(msg, &block) ⇒ Object
Log an fatal message.
-
#formatted_message(msg) ⇒ String
Format main log message.
-
#formatted_message_as_string(msg) ⇒ String
Format the log message as string.
-
#info(msg, &block) ⇒ Object
Log an info message.
-
#initialize(worker) ⇒ WorkerLogger
constructor
Build a new instance of the class.
-
#log_block ⇒ Proc
The block to pass to log messages.
-
#logger ⇒ Logger, any
Return the Cloudtasker logger.
-
#method_missing(name, *args, &block) ⇒ Any
Delegate all methods to the underlying logger.
-
#respond_to_missing?(name, include_private = false) ⇒ Boolean
Check if the class respond to a certain method.
Constructor Details
#initialize(worker) ⇒ WorkerLogger
Build a new instance of the class.
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.
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_processor ⇒ Object
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
#worker ⇒ Object
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.
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_processor ⇒ Proc
Return the Proc responsible for formatting the log payload.
78 79 80 81 82 |
# File 'lib/cloudtasker/worker_logger.rb', line 78 def context_processor @context_processor ||= worker.class.[:log_context_processor] || self.class.log_context_processor || DEFAULT_CONTEXT_PROCESSOR end |
#debug(msg, &block) ⇒ Object
Log an debut message.
174 175 176 |
# File 'lib/cloudtasker/worker_logger.rb', line 174 def debug(msg, &block) (:debug, msg, &block) end |
#error(msg, &block) ⇒ Object
Log an error message.
154 155 156 |
# File 'lib/cloudtasker/worker_logger.rb', line 154 def error(msg, &block) (:error, msg, &block) end |
#fatal(msg, &block) ⇒ Object
Log an fatal message.
164 165 166 |
# File 'lib/cloudtasker/worker_logger.rb', line 164 def fatal(msg, &block) (:fatal, msg, &block) end |
#formatted_message(msg) ⇒ String
Format main log message.
129 130 131 132 133 134 135 136 |
# File 'lib/cloudtasker/worker_logger.rb', line 129 def (msg) if msg.is_a?(String) (msg) else # Delegate object formatting to logger msg end end |
#formatted_message_as_string(msg) ⇒ String
Format the log message as string.
109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/cloudtasker/worker_logger.rb', line 109 def (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.
144 145 146 |
# File 'lib/cloudtasker/worker_logger.rb', line 144 def info(msg, &block) (:info, msg, &block) end |
#log_block ⇒ Proc
The block to pass to log messages.
89 90 91 |
# File 'lib/cloudtasker/worker_logger.rb', line 89 def log_block @log_block ||= proc { context_processor.call(worker) } end |
#logger ⇒ Logger, any
Return 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.
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 |