smart_message 0.0.13 → 0.0.16
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +120 -0
- data/Gemfile.lock +3 -3
- data/README.md +71 -25
- data/docs/index.md +2 -0
- data/docs/reference/transports.md +46 -21
- data/docs/transports/memory-transport.md +2 -1
- data/docs/transports/multi-transport.md +484 -0
- data/examples/file/00_run_all_file_demos.rb +260 -0
- data/examples/file/01_basic_file_transport_demo.rb +237 -0
- data/examples/file/02_fifo_transport_demo.rb +289 -0
- data/examples/file/03_file_watching_demo.rb +332 -0
- data/examples/file/04_multi_transport_file_demo.rb +432 -0
- data/examples/file/README.md +257 -0
- data/examples/memory/00_run_all_demos.rb +317 -0
- data/examples/memory/01_message_deduplication_demo.rb +18 -30
- data/examples/memory/02_dead_letter_queue_demo.rb +9 -9
- data/examples/memory/03_point_to_point_orders.rb +3 -3
- data/examples/memory/04_publish_subscribe_events.rb +15 -15
- data/examples/memory/05_many_to_many_chat.rb +19 -19
- data/examples/memory/06_stdout_publish_only.rb +145 -0
- data/examples/memory/07_proc_handlers_demo.rb +13 -13
- data/examples/memory/08_custom_logger_demo.rb +136 -136
- data/examples/memory/09_error_handling_demo.rb +7 -7
- data/examples/memory/10_entity_addressing_basic.rb +25 -25
- data/examples/memory/11_entity_addressing_with_filtering.rb +32 -32
- data/examples/memory/12_regex_filtering_microservices.rb +10 -10
- data/examples/memory/14_global_configuration_demo.rb +12 -12
- data/examples/memory/README.md +34 -17
- data/examples/memory/log/demo_app.log.1 +100 -0
- data/examples/memory/log/demo_app.log.2 +100 -0
- data/examples/multi_transport_example.rb +114 -0
- data/examples/redis/01_smart_home_iot_demo.rb +20 -20
- data/examples/utilities/box_it.rb +12 -0
- data/examples/utilities/doing.rb +19 -0
- data/examples/utilities/temp.md +28 -0
- data/lib/smart_message/base.rb +5 -7
- data/lib/smart_message/errors.rb +3 -0
- data/lib/smart_message/header.rb +1 -1
- data/lib/smart_message/logger/default.rb +1 -1
- data/lib/smart_message/messaging.rb +36 -6
- data/lib/smart_message/plugins.rb +46 -4
- data/lib/smart_message/serializer/base.rb +1 -1
- data/lib/smart_message/serializer.rb +3 -2
- data/lib/smart_message/subscription.rb +18 -20
- data/lib/smart_message/transport/async_publish_queue.rb +284 -0
- data/lib/smart_message/transport/fifo_operations.rb +264 -0
- data/lib/smart_message/transport/file_operations.rb +200 -0
- data/lib/smart_message/transport/file_transport.rb +149 -0
- data/lib/smart_message/transport/file_watching.rb +72 -0
- data/lib/smart_message/transport/partitioned_files.rb +46 -0
- data/lib/smart_message/transport/stdout_transport.rb +50 -36
- data/lib/smart_message/transport/stdout_transport.rb.backup +88 -0
- data/lib/smart_message/version.rb +1 -1
- metadata +24 -10
- data/ideas/README.md +0 -41
- data/ideas/agents.md +0 -1001
- data/ideas/database_transport.md +0 -980
- data/ideas/improvement.md +0 -359
- data/ideas/meshage.md +0 -1788
- data/ideas/message_discovery.md +0 -178
- data/ideas/message_schema.md +0 -1381
- data/lib/smart_message/wrapper.rb.bak +0 -132
- /data/examples/memory/{06_pretty_print_demo.rb → 16_pretty_print_demo.rb} +0 -0
@@ -4,7 +4,7 @@
|
|
4
4
|
# Custom Logger Example: Comprehensive Message Logging
|
5
5
|
#
|
6
6
|
# This example demonstrates how to implement and use custom loggers in the SmartMessage
|
7
|
-
# framework. It shows different logging strategies including file logging, structured
|
7
|
+
# framework. It shows different logging strategies including file logging, structured
|
8
8
|
# logging, and audit trails for message processing workflows.
|
9
9
|
#
|
10
10
|
# IMPORTANT: Using the Default Logger or Custom Loggers
|
@@ -35,12 +35,12 @@
|
|
35
35
|
# end
|
36
36
|
#
|
37
37
|
# To create your own custom logger:
|
38
|
-
# 1. Create
|
39
|
-
# 2. Store the Ruby logger instance in your
|
38
|
+
# 1. Create an adapter class that inherits from SmartMessage::Logger::Base
|
39
|
+
# 2. Store the Ruby logger instance in your adapter
|
40
40
|
# 3. Implement the SmartMessage logging methods using your Ruby logger
|
41
41
|
#
|
42
42
|
# To use Rails.logger directly (without the default logger):
|
43
|
-
# 1. Create
|
43
|
+
# 1. Create an adapter that delegates to Rails.logger
|
44
44
|
# 2. Configure it at the class level: logger SmartMessage::Logger::RailsLogger.new
|
45
45
|
# 3. All messages will be logged to your Rails application logs
|
46
46
|
|
@@ -53,18 +53,18 @@ puts "=== SmartMessage Example: Custom Logger Implementation ==="
|
|
53
53
|
puts
|
54
54
|
|
55
55
|
# Custom File Logger Implementation
|
56
|
-
# This
|
56
|
+
# This adapter shows how to integrate Ruby's standard Logger class with SmartMessage.
|
57
57
|
# The same pattern works for Rails.logger, Semantic Logger, or any Ruby logger.
|
58
58
|
class SmartMessage::Logger::FileLogger < SmartMessage::Logger::Base
|
59
59
|
attr_reader :log_file_path, :logger
|
60
|
-
|
60
|
+
|
61
61
|
def initialize(log_file_path, level: Logger::INFO)
|
62
62
|
@log_file_path = log_file_path
|
63
|
-
|
63
|
+
|
64
64
|
# Ensure log directory exists
|
65
65
|
log_dir = File.dirname(@log_file_path)
|
66
66
|
FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
|
67
|
-
|
67
|
+
|
68
68
|
# This is Ruby's standard Logger class from the 'logger' library
|
69
69
|
# You could replace this with Rails.logger or any other logger:
|
70
70
|
# @logger = Rails.logger # For Rails applications
|
@@ -76,24 +76,24 @@ class SmartMessage::Logger::FileLogger < SmartMessage::Logger::Base
|
|
76
76
|
"[#{datetime.strftime('%Y-%m-%d %H:%M:%S.%3N')}] #{severity}: #{msg}\n"
|
77
77
|
end
|
78
78
|
end
|
79
|
-
|
79
|
+
|
80
80
|
# Standard logging methods
|
81
81
|
def debug(message = nil, &block)
|
82
82
|
@logger.debug(message || block.call)
|
83
83
|
end
|
84
|
-
|
84
|
+
|
85
85
|
def info(message = nil, &block)
|
86
86
|
@logger.info(message || block.call)
|
87
87
|
end
|
88
|
-
|
88
|
+
|
89
89
|
def warn(message = nil, &block)
|
90
90
|
@logger.warn(message || block.call)
|
91
91
|
end
|
92
|
-
|
92
|
+
|
93
93
|
def error(message = nil, &block)
|
94
94
|
@logger.error(message || block.call)
|
95
95
|
end
|
96
|
-
|
96
|
+
|
97
97
|
def fatal(message = nil, &block)
|
98
98
|
@logger.fatal(message || block.call)
|
99
99
|
end
|
@@ -102,15 +102,15 @@ end
|
|
102
102
|
# Structured JSON Logger Implementation
|
103
103
|
class SmartMessage::Logger::JSONLogger < SmartMessage::Logger::Base
|
104
104
|
attr_reader :log_file_path
|
105
|
-
|
105
|
+
|
106
106
|
def initialize(log_file_path)
|
107
107
|
@log_file_path = log_file_path
|
108
|
-
|
108
|
+
|
109
109
|
# Ensure log directory exists
|
110
110
|
log_dir = File.dirname(@log_file_path)
|
111
111
|
FileUtils.mkdir_p(log_dir) unless Dir.exist?(log_dir)
|
112
112
|
end
|
113
|
-
|
113
|
+
|
114
114
|
# Standard logging methods with JSON output
|
115
115
|
def debug(message = nil, &block)
|
116
116
|
write_log_entry({
|
@@ -119,7 +119,7 @@ class SmartMessage::Logger::JSONLogger < SmartMessage::Logger::Base
|
|
119
119
|
timestamp: Time.now.iso8601
|
120
120
|
})
|
121
121
|
end
|
122
|
-
|
122
|
+
|
123
123
|
def info(message = nil, &block)
|
124
124
|
write_log_entry({
|
125
125
|
level: 'INFO',
|
@@ -127,7 +127,7 @@ class SmartMessage::Logger::JSONLogger < SmartMessage::Logger::Base
|
|
127
127
|
timestamp: Time.now.iso8601
|
128
128
|
})
|
129
129
|
end
|
130
|
-
|
130
|
+
|
131
131
|
def warn(message = nil, &block)
|
132
132
|
write_log_entry({
|
133
133
|
level: 'WARN',
|
@@ -135,7 +135,7 @@ class SmartMessage::Logger::JSONLogger < SmartMessage::Logger::Base
|
|
135
135
|
timestamp: Time.now.iso8601
|
136
136
|
})
|
137
137
|
end
|
138
|
-
|
138
|
+
|
139
139
|
def error(message = nil, &block)
|
140
140
|
write_log_entry({
|
141
141
|
level: 'ERROR',
|
@@ -143,7 +143,7 @@ class SmartMessage::Logger::JSONLogger < SmartMessage::Logger::Base
|
|
143
143
|
timestamp: Time.now.iso8601
|
144
144
|
})
|
145
145
|
end
|
146
|
-
|
146
|
+
|
147
147
|
def fatal(message = nil, &block)
|
148
148
|
write_log_entry({
|
149
149
|
level: 'FATAL',
|
@@ -151,9 +151,9 @@ class SmartMessage::Logger::JSONLogger < SmartMessage::Logger::Base
|
|
151
151
|
timestamp: Time.now.iso8601
|
152
152
|
})
|
153
153
|
end
|
154
|
-
|
154
|
+
|
155
155
|
private
|
156
|
-
|
156
|
+
|
157
157
|
def write_log_entry(data)
|
158
158
|
File.open(@log_file_path, 'a') do |file|
|
159
159
|
file.puts(JSON.generate(data))
|
@@ -166,56 +166,56 @@ class SmartMessage::Logger::EmojiConsoleLogger < SmartMessage::Logger::Base
|
|
166
166
|
def debug(message = nil, &block)
|
167
167
|
puts "🐛 DEBUG: #{message || block&.call}"
|
168
168
|
end
|
169
|
-
|
169
|
+
|
170
170
|
def info(message = nil, &block)
|
171
171
|
puts "ℹ️ INFO: #{message || block&.call}"
|
172
172
|
end
|
173
|
-
|
173
|
+
|
174
174
|
def warn(message = nil, &block)
|
175
175
|
puts "⚠️ WARN: #{message || block&.call}"
|
176
176
|
end
|
177
|
-
|
177
|
+
|
178
178
|
def error(message = nil, &block)
|
179
179
|
puts "❌ ERROR: #{message || block&.call}"
|
180
180
|
end
|
181
|
-
|
181
|
+
|
182
182
|
def fatal(message = nil, &block)
|
183
183
|
puts "💀 FATAL: #{message || block&.call}"
|
184
184
|
end
|
185
185
|
end
|
186
186
|
|
187
|
-
# Example: Simple Ruby Logger
|
188
|
-
# This demonstrates the minimal
|
187
|
+
# Example: Simple Ruby Logger
|
188
|
+
# This demonstrates the minimal adapter needed for Ruby's standard Logger.
|
189
189
|
# Use this pattern when you want to integrate with existing logging infrastructure.
|
190
|
-
class SmartMessage::Logger::
|
190
|
+
class SmartMessage::Logger::RubyLogger < SmartMessage::Logger::Base
|
191
191
|
def initialize(ruby_logger = nil)
|
192
192
|
# Accept any Ruby logger instance, or create a default one
|
193
193
|
@logger = ruby_logger || Logger.new(STDOUT)
|
194
194
|
end
|
195
|
-
|
195
|
+
|
196
196
|
# Standard logging methods that delegate to the Ruby logger
|
197
197
|
def debug(message = nil, &block)
|
198
198
|
@logger.debug(message, &block)
|
199
199
|
end
|
200
|
-
|
200
|
+
|
201
201
|
def info(message = nil, &block)
|
202
202
|
@logger.info(message, &block)
|
203
203
|
end
|
204
|
-
|
204
|
+
|
205
205
|
def warn(message = nil, &block)
|
206
206
|
@logger.warn(message, &block)
|
207
207
|
end
|
208
|
-
|
208
|
+
|
209
209
|
def error(message = nil, &block)
|
210
210
|
@logger.error(message, &block)
|
211
211
|
end
|
212
|
-
|
212
|
+
|
213
213
|
def fatal(message = nil, &block)
|
214
214
|
@logger.fatal(message, &block)
|
215
215
|
end
|
216
216
|
end
|
217
217
|
|
218
|
-
# Example: Rails Logger
|
218
|
+
# Example: Rails Logger (for Rails applications)
|
219
219
|
# Uncomment and use this in your Rails application
|
220
220
|
# class SmartMessage::Logger::RailsLogger < SmartMessage::Logger::Base
|
221
221
|
# def debug(message = nil, &block)
|
@@ -223,25 +223,25 @@ end
|
|
223
223
|
# Rails.logger.debug(message || block&.call)
|
224
224
|
# end
|
225
225
|
# end
|
226
|
-
#
|
226
|
+
#
|
227
227
|
# def info(message = nil, &block)
|
228
228
|
# Rails.logger.tagged('SmartMessage') do
|
229
229
|
# Rails.logger.info(message || block&.call)
|
230
230
|
# end
|
231
231
|
# end
|
232
|
-
#
|
232
|
+
#
|
233
233
|
# def warn(message = nil, &block)
|
234
234
|
# Rails.logger.tagged('SmartMessage') do
|
235
235
|
# Rails.logger.warn(message || block&.call)
|
236
236
|
# end
|
237
237
|
# end
|
238
|
-
#
|
238
|
+
#
|
239
239
|
# def error(message = nil, &block)
|
240
240
|
# Rails.logger.tagged('SmartMessage') do
|
241
241
|
# Rails.logger.error(message || block&.call)
|
242
242
|
# end
|
243
243
|
# end
|
244
|
-
#
|
244
|
+
#
|
245
245
|
# def fatal(message = nil, &block)
|
246
246
|
# Rails.logger.tagged('SmartMessage') do
|
247
247
|
# Rails.logger.fatal(message || block&.call)
|
@@ -254,23 +254,23 @@ class SmartMessage::Logger::MultiLogger < SmartMessage::Logger::Base
|
|
254
254
|
def initialize(*loggers)
|
255
255
|
@loggers = loggers
|
256
256
|
end
|
257
|
-
|
257
|
+
|
258
258
|
def debug(message = nil, &block)
|
259
259
|
@loggers.each { |logger| logger.debug(message, &block) }
|
260
260
|
end
|
261
|
-
|
261
|
+
|
262
262
|
def info(message = nil, &block)
|
263
263
|
@loggers.each { |logger| logger.info(message, &block) }
|
264
264
|
end
|
265
|
-
|
265
|
+
|
266
266
|
def warn(message = nil, &block)
|
267
267
|
@loggers.each { |logger| logger.warn(message, &block) }
|
268
268
|
end
|
269
|
-
|
269
|
+
|
270
270
|
def error(message = nil, &block)
|
271
271
|
@loggers.each { |logger| logger.error(message, &block) }
|
272
272
|
end
|
273
|
-
|
273
|
+
|
274
274
|
def fatal(message = nil, &block)
|
275
275
|
@loggers.each { |logger| logger.fatal(message, &block) }
|
276
276
|
end
|
@@ -279,50 +279,50 @@ end
|
|
279
279
|
# Sample message class with comprehensive logging
|
280
280
|
class OrderProcessingMessage < SmartMessage::Base
|
281
281
|
description "Order processing messages with comprehensive multi-logger configuration"
|
282
|
-
|
283
|
-
property :order_id,
|
282
|
+
|
283
|
+
property :order_id,
|
284
284
|
description: "Unique identifier for the customer order"
|
285
|
-
property :customer_id,
|
285
|
+
property :customer_id,
|
286
286
|
description: "Identifier of the customer placing the order"
|
287
|
-
property :amount,
|
287
|
+
property :amount,
|
288
288
|
description: "Total monetary amount of the order"
|
289
|
-
property :status,
|
289
|
+
property :status,
|
290
290
|
description: "Current processing status of the order"
|
291
|
-
property :items,
|
291
|
+
property :items,
|
292
292
|
description: "Array of items included in the order"
|
293
|
-
|
293
|
+
|
294
294
|
config do
|
295
|
-
transport SmartMessage::Transport::
|
296
|
-
|
295
|
+
transport SmartMessage::Transport::MemoryTransport.new
|
296
|
+
|
297
297
|
# Configure multi-logger to demonstrate different logging approaches
|
298
298
|
logger SmartMessage::Logger::MultiLogger.new(
|
299
299
|
SmartMessage::Logger::EmojiConsoleLogger.new,
|
300
|
-
SmartMessage::Logger::FileLogger.new('
|
301
|
-
SmartMessage::Logger::JSONLogger.new('
|
300
|
+
SmartMessage::Logger::FileLogger.new('log/order_processing.log', level: Logger::DEBUG),
|
301
|
+
SmartMessage::Logger::JSONLogger.new('log/order_processing.json')
|
302
302
|
)
|
303
303
|
end
|
304
|
-
|
305
|
-
def
|
306
|
-
message_header, message_payload =
|
304
|
+
|
305
|
+
def process(message)
|
306
|
+
message_header, message_payload = message
|
307
307
|
# Simulate the logger being called during processing
|
308
308
|
if logger
|
309
309
|
logger.info { "[SmartMessage] Received: #{self.name} (#{message_payload.bytesize} bytes)" }
|
310
310
|
end
|
311
|
-
|
311
|
+
|
312
312
|
# Process the message
|
313
313
|
order_data = JSON.parse(message_payload)
|
314
314
|
result = "Order #{order_data['order_id']} processed successfully"
|
315
|
-
|
315
|
+
|
316
316
|
puts "💼 OrderProcessing: #{result}"
|
317
|
-
|
317
|
+
|
318
318
|
# Log processing completion
|
319
319
|
if logger
|
320
320
|
logger.info { "[SmartMessage] Processed: #{self.name} - #{result}" }
|
321
321
|
end
|
322
|
-
|
322
|
+
|
323
323
|
result
|
324
324
|
end
|
325
|
-
|
325
|
+
|
326
326
|
# Override publish to demonstrate logging hooks
|
327
327
|
def publish
|
328
328
|
# Log message creation
|
@@ -330,13 +330,13 @@ class OrderProcessingMessage < SmartMessage::Base
|
|
330
330
|
if logger_instance
|
331
331
|
logger_instance.debug { "[SmartMessage] Created: #{self.class.name}" }
|
332
332
|
end
|
333
|
-
|
333
|
+
|
334
334
|
# Log publishing
|
335
335
|
transport_instance = transport || self.class.transport
|
336
336
|
if logger_instance
|
337
337
|
logger_instance.info { "[SmartMessage] Published: #{self.class.name} via #{transport_instance.class.name.split('::').last}" }
|
338
338
|
end
|
339
|
-
|
339
|
+
|
340
340
|
# Call original publish method
|
341
341
|
super
|
342
342
|
rescue => error
|
@@ -351,38 +351,38 @@ end
|
|
351
351
|
# Notification message with different logger configuration
|
352
352
|
class NotificationMessage < SmartMessage::Base
|
353
353
|
description "User notifications with file-based logging configuration"
|
354
|
-
|
355
|
-
property :recipient,
|
354
|
+
|
355
|
+
property :recipient,
|
356
356
|
description: "Target recipient for the notification"
|
357
|
-
property :subject,
|
357
|
+
property :subject,
|
358
358
|
description: "Subject line or title of the notification"
|
359
|
-
property :body,
|
359
|
+
property :body,
|
360
360
|
description: "Main content body of the notification"
|
361
|
-
property :priority,
|
361
|
+
property :priority,
|
362
362
|
description: "Priority level of the notification (low, normal, high, urgent)"
|
363
|
-
|
363
|
+
|
364
364
|
config do
|
365
|
-
transport SmartMessage::Transport::
|
366
|
-
|
365
|
+
transport SmartMessage::Transport::MemoryTransport.new
|
366
|
+
|
367
367
|
# Use only file logger for notifications
|
368
|
-
logger SmartMessage::Logger::FileLogger.new('
|
368
|
+
logger SmartMessage::Logger::FileLogger.new('log/notifications.log', level: Logger::WARN)
|
369
369
|
end
|
370
|
-
|
371
|
-
def
|
372
|
-
message_header, message_payload =
|
370
|
+
|
371
|
+
def process(message)
|
372
|
+
message_header, message_payload = message
|
373
373
|
if logger
|
374
374
|
logger.info { "[SmartMessage] Received: #{self.name} (#{message_payload.bytesize} bytes)" }
|
375
375
|
end
|
376
|
-
|
376
|
+
|
377
377
|
notification_data = JSON.parse(message_payload)
|
378
378
|
result = "Notification sent to #{notification_data['recipient']}"
|
379
|
-
|
379
|
+
|
380
380
|
puts "📬 Notification: #{result}"
|
381
|
-
|
381
|
+
|
382
382
|
if logger
|
383
383
|
logger.info { "[SmartMessage] Processed: #{self.name} - #{result}" }
|
384
384
|
end
|
385
|
-
|
385
|
+
|
386
386
|
result
|
387
387
|
end
|
388
388
|
end
|
@@ -391,34 +391,34 @@ end
|
|
391
391
|
# This demonstrates how to use Ruby's standard Logger in production code
|
392
392
|
class StandardLoggerMessage < SmartMessage::Base
|
393
393
|
description "Demonstrates integration with standard Ruby Logger for production logging"
|
394
|
-
|
395
|
-
property :content,
|
394
|
+
|
395
|
+
property :content,
|
396
396
|
description: "Main content of the message to be logged"
|
397
|
-
property :level,
|
397
|
+
property :level,
|
398
398
|
description: "Logging level for the message (debug, info, warn, error)"
|
399
|
-
|
399
|
+
|
400
400
|
config do
|
401
|
-
transport SmartMessage::Transport::
|
402
|
-
|
401
|
+
transport SmartMessage::Transport::MemoryTransport.new
|
402
|
+
|
403
403
|
# Example 1: Using Ruby's standard Logger directly
|
404
404
|
# Create a standard Ruby logger that logs to STDOUT
|
405
405
|
ruby_logger = Logger.new(STDOUT)
|
406
406
|
ruby_logger.level = Logger::INFO
|
407
407
|
ruby_logger.progname = 'SmartMessage'
|
408
|
-
|
408
|
+
|
409
409
|
# Wrap it in our adapter
|
410
|
-
logger SmartMessage::Logger::
|
411
|
-
|
410
|
+
logger SmartMessage::Logger::RubyLogger.new(ruby_logger)
|
411
|
+
|
412
412
|
# Example 2: Using a file-based Ruby logger (commented out)
|
413
413
|
# file_logger = Logger.new('application.log', 'daily') # Rotate daily
|
414
|
-
# logger SmartMessage::Logger::
|
415
|
-
|
414
|
+
# logger SmartMessage::Logger::RubyLogger.new(file_logger)
|
415
|
+
|
416
416
|
# Example 3: In Rails, you would use Rails.logger (commented out)
|
417
|
-
# logger SmartMessage::Logger::
|
417
|
+
# logger SmartMessage::Logger::RubyLogger.new(Rails.logger)
|
418
418
|
end
|
419
|
-
|
420
|
-
def
|
421
|
-
message_header, message_payload =
|
419
|
+
|
420
|
+
def process(message)
|
421
|
+
message_header, message_payload = message
|
422
422
|
data = JSON.parse(message_payload)
|
423
423
|
puts "📝 Processing: #{data['content']}"
|
424
424
|
"Processed"
|
@@ -428,21 +428,21 @@ end
|
|
428
428
|
# Example: Message using the built-in Default Logger
|
429
429
|
class DefaultLoggerMessage < SmartMessage::Base
|
430
430
|
description "Demonstrates SmartMessage's built-in default logger with auto-detection"
|
431
|
-
|
432
|
-
property :message,
|
431
|
+
|
432
|
+
property :message,
|
433
433
|
description: "The message content to be logged using default logger"
|
434
|
-
property :level,
|
434
|
+
property :level,
|
435
435
|
description: "Log level (debug, info, warn, error, fatal)"
|
436
|
-
|
436
|
+
|
437
437
|
config do
|
438
|
-
transport SmartMessage::Transport::
|
439
|
-
|
438
|
+
transport SmartMessage::Transport::MemoryTransport.new
|
439
|
+
|
440
440
|
# Use the built-in default logger - simplest option!
|
441
441
|
logger SmartMessage::Logger::Default.new
|
442
442
|
end
|
443
|
-
|
444
|
-
def
|
445
|
-
message_header, message_payload =
|
443
|
+
|
444
|
+
def process(message)
|
445
|
+
message_header, message_payload = message
|
446
446
|
data = JSON.parse(message_payload)
|
447
447
|
puts "🎯 DefaultLogger: Processing #{data['message']}"
|
448
448
|
"Processed with default logger"
|
@@ -453,19 +453,19 @@ end
|
|
453
453
|
class PriorityOrderService
|
454
454
|
def initialize
|
455
455
|
puts "🚀 PriorityOrderService: Starting with custom logger..."
|
456
|
-
|
456
|
+
|
457
457
|
# Create a priority-specific logger
|
458
458
|
@priority_logger = SmartMessage::Logger::FileLogger.new(
|
459
|
-
'
|
459
|
+
'log/priority_orders.log',
|
460
460
|
level: Logger::DEBUG
|
461
461
|
)
|
462
462
|
end
|
463
|
-
|
463
|
+
|
464
464
|
def process_priority_order(order_data)
|
465
465
|
# Use class-level logger override for this specific processing
|
466
466
|
original_logger = OrderProcessingMessage.logger
|
467
467
|
OrderProcessingMessage.logger(@priority_logger)
|
468
|
-
|
468
|
+
|
469
469
|
begin
|
470
470
|
message = OrderProcessingMessage.new(**order_data, from: 'PriorityOrderService')
|
471
471
|
puts "⚡ Processing priority order with dedicated logger"
|
@@ -482,22 +482,22 @@ end
|
|
482
482
|
class LoggerDemo
|
483
483
|
def run
|
484
484
|
puts "🚀 Starting Custom Logger Demo\n"
|
485
|
-
|
485
|
+
|
486
486
|
# Clean up any existing log files for a fresh demo
|
487
487
|
FileUtils.rm_rf('logs') if Dir.exist?('logs')
|
488
|
-
|
488
|
+
|
489
489
|
# Subscribe to messages
|
490
490
|
OrderProcessingMessage.subscribe
|
491
491
|
NotificationMessage.subscribe
|
492
|
-
|
492
|
+
|
493
493
|
puts "\n" + "="*70
|
494
494
|
puts "Demonstrating Different Logger Configurations"
|
495
495
|
puts "="*70
|
496
|
-
|
496
|
+
|
497
497
|
# Demo 1: Using the built-in Default Logger (NEW!)
|
498
498
|
puts "\n--- Demo 1: Using SmartMessage Default Logger ---"
|
499
499
|
puts "The Default logger automatically uses Rails.logger or Ruby Logger"
|
500
|
-
|
500
|
+
|
501
501
|
# Use the DefaultLoggerMessage class defined above
|
502
502
|
DefaultLoggerMessage.subscribe
|
503
503
|
default_msg = DefaultLoggerMessage.new(
|
@@ -507,7 +507,7 @@ class LoggerDemo
|
|
507
507
|
)
|
508
508
|
default_msg.publish
|
509
509
|
sleep(0.5)
|
510
|
-
|
510
|
+
|
511
511
|
# Demo 2: Standard order with multi-logger
|
512
512
|
puts "\n--- Demo 2: Standard Order (Multi-Logger) ---"
|
513
513
|
order1 = OrderProcessingMessage.new(
|
@@ -520,7 +520,7 @@ class LoggerDemo
|
|
520
520
|
)
|
521
521
|
order1.publish
|
522
522
|
sleep(0.5)
|
523
|
-
|
523
|
+
|
524
524
|
# Demo 3: Notification with file-only logger
|
525
525
|
puts "\n--- Demo 3: Notification (File Logger Only) ---"
|
526
526
|
notification = NotificationMessage.new(
|
@@ -532,7 +532,7 @@ class LoggerDemo
|
|
532
532
|
)
|
533
533
|
notification.publish
|
534
534
|
sleep(0.5)
|
535
|
-
|
535
|
+
|
536
536
|
# Demo 4: Priority order with instance-level logger override
|
537
537
|
puts "\n--- Demo 4: Priority Order (Instance Logger Override) ---"
|
538
538
|
priority_service = PriorityOrderService.new
|
@@ -545,27 +545,27 @@ class LoggerDemo
|
|
545
545
|
from: 'PriorityOrderService'
|
546
546
|
)
|
547
547
|
sleep(0.5)
|
548
|
-
|
548
|
+
|
549
549
|
# Demo 5: Using standard Ruby logger
|
550
550
|
puts "\n--- Demo 5: Using Standard Ruby Logger ---"
|
551
|
-
|
551
|
+
|
552
552
|
# Use the StandardLoggerMessage class that demonstrates Ruby's standard logger
|
553
553
|
StandardLoggerMessage.subscribe
|
554
|
-
|
554
|
+
|
555
555
|
# Create and send a message - watch for the Ruby logger output
|
556
556
|
msg = StandardLoggerMessage.new(
|
557
557
|
content: "Testing with Ruby's standard logger",
|
558
558
|
level: "info",
|
559
559
|
from: 'LoggerDemo'
|
560
560
|
)
|
561
|
-
|
561
|
+
|
562
562
|
# The logger will output to STDOUT using Ruby's standard format
|
563
563
|
msg.publish
|
564
564
|
sleep(0.5)
|
565
|
-
|
565
|
+
|
566
566
|
puts "\nNote: The above used Ruby's standard Logger class wrapped for SmartMessage"
|
567
567
|
puts "You can use ANY Ruby logger this way: Logger.new, Rails.logger, etc."
|
568
|
-
|
568
|
+
|
569
569
|
# Demo 6: Error handling with logging
|
570
570
|
puts "\n--- Demo 6: Error Handling with Logging ---"
|
571
571
|
begin
|
@@ -577,24 +577,24 @@ class LoggerDemo
|
|
577
577
|
status: "error_demo",
|
578
578
|
from: 'LoggerDemo'
|
579
579
|
)
|
580
|
-
|
580
|
+
|
581
581
|
# Simulate an error during processing
|
582
582
|
if OrderProcessingMessage.logger
|
583
583
|
error = StandardError.new("Invalid order data provided")
|
584
584
|
OrderProcessingMessage.logger.error { "[SmartMessage] Error: Simulated error for demo - #{error.class.name}: #{error.message}" }
|
585
585
|
end
|
586
|
-
|
586
|
+
|
587
587
|
rescue => error
|
588
588
|
puts "🔍 Caught demonstration error: #{error.message}"
|
589
589
|
end
|
590
|
-
|
590
|
+
|
591
591
|
# Show log file contents
|
592
592
|
puts "\n" + "="*70
|
593
593
|
puts "📋 Log File Contents"
|
594
594
|
puts "="*70
|
595
|
-
|
595
|
+
|
596
596
|
show_log_contents
|
597
|
-
|
597
|
+
|
598
598
|
puts "\n✨ Demo completed!"
|
599
599
|
puts "\nThis example demonstrated:"
|
600
600
|
puts "• SmartMessage::Logger::Default - Built-in logger that auto-detects Rails/Ruby"
|
@@ -608,18 +608,18 @@ class LoggerDemo
|
|
608
608
|
puts "\nKEY TAKEAWAY: Use SmartMessage::Logger::Default.new for instant logging!"
|
609
609
|
puts "It automatically uses Rails.logger in Rails or creates a Ruby Logger otherwise."
|
610
610
|
end
|
611
|
-
|
611
|
+
|
612
612
|
private
|
613
|
-
|
613
|
+
|
614
614
|
def show_log_contents
|
615
|
-
log_files = Dir.glob('
|
616
|
-
|
615
|
+
log_files = Dir.glob('log/*.log') + Dir.glob('log/*.json')
|
616
|
+
|
617
617
|
log_files.each do |log_file|
|
618
618
|
next unless File.exist?(log_file)
|
619
|
-
|
619
|
+
|
620
620
|
puts "\n📁 #{log_file}:"
|
621
621
|
puts "-" * 50
|
622
|
-
|
622
|
+
|
623
623
|
content = File.read(log_file)
|
624
624
|
if content.length > 500
|
625
625
|
puts content[0..500] + "\n... (truncated, #{content.length} total characters)"
|
@@ -627,7 +627,7 @@ class LoggerDemo
|
|
627
627
|
puts content
|
628
628
|
end
|
629
629
|
end
|
630
|
-
|
630
|
+
|
631
631
|
if log_files.empty?
|
632
632
|
puts "⚠️ No log files found (they may not have been created yet)"
|
633
633
|
end
|
@@ -638,4 +638,4 @@ end
|
|
638
638
|
if __FILE__ == $0
|
639
639
|
demo = LoggerDemo.new
|
640
640
|
demo.run
|
641
|
-
end
|
641
|
+
end
|