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.
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.
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.
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_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 |
Instance Method Details
#context_processor ⇒ Proc
Return the Proc responsible for formatting the log payload.
30 31 32 33 34 |
# File 'lib/cloudtasker/worker_logger.rb', line 30 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.
126 127 128 |
# File 'lib/cloudtasker/worker_logger.rb', line 126 def debug(msg, &block) (:debug, msg, &block) end |
#error(msg, &block) ⇒ Object
Log an error message.
106 107 108 |
# File 'lib/cloudtasker/worker_logger.rb', line 106 def error(msg, &block) (:error, msg, &block) end |
#fatal(msg, &block) ⇒ Object
Log an fatal message.
116 117 118 |
# File 'lib/cloudtasker/worker_logger.rb', line 116 def fatal(msg, &block) (:fatal, msg, &block) end |
#formatted_message(msg) ⇒ String
Format main log message.
81 82 83 84 85 86 87 88 |
# File 'lib/cloudtasker/worker_logger.rb', line 81 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.
61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/cloudtasker/worker_logger.rb', line 61 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.
96 97 98 |
# File 'lib/cloudtasker/worker_logger.rb', line 96 def info(msg, &block) (:info, msg, &block) end |
#log_block ⇒ Proc
The block to pass to log messages.
41 42 43 |
# File 'lib/cloudtasker/worker_logger.rb', line 41 def log_block @log_block ||= proc { context_processor.call(worker) } end |
#logger ⇒ Logger, any
Return 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.
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 |