smart_message 0.0.12 ā 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 +155 -1
- data/Gemfile.lock +6 -6
- data/README.md +71 -25
- data/docs/core-concepts/architecture.md +5 -10
- data/docs/getting-started/examples.md +0 -12
- data/docs/getting-started/quick-start.md +4 -9
- data/docs/index.md +6 -4
- data/docs/reference/serializers.md +160 -488
- data/docs/reference/transports.md +47 -146
- data/docs/transports/memory-transport.md +2 -1
- data/docs/transports/multi-transport.md +484 -0
- data/docs/transports/redis-transport-comparison.md +215 -350
- data/docs/transports/redis-transport.md +3 -22
- data/examples/README.md +6 -9
- data/examples/city_scenario/README.md +1 -1
- data/examples/city_scenario/messages/emergency_911_message.rb +0 -1
- data/examples/city_scenario/messages/emergency_resolved_message.rb +0 -1
- data/examples/city_scenario/messages/fire_dispatch_message.rb +0 -1
- data/examples/city_scenario/messages/fire_emergency_message.rb +0 -1
- data/examples/city_scenario/messages/health_check_message.rb +0 -1
- data/examples/city_scenario/messages/health_status_message.rb +0 -1
- data/examples/city_scenario/messages/police_dispatch_message.rb +0 -1
- data/examples/city_scenario/messages/silent_alarm_message.rb +0 -1
- 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 -32
- data/examples/memory/02_dead_letter_queue_demo.rb +9 -12
- data/examples/memory/03_point_to_point_orders.rb +3 -5
- data/examples/memory/04_publish_subscribe_events.rb +15 -16
- data/examples/memory/05_many_to_many_chat.rb +19 -22
- data/examples/memory/06_stdout_publish_only.rb +145 -0
- data/examples/memory/07_proc_handlers_demo.rb +13 -14
- data/examples/memory/08_custom_logger_demo.rb +136 -140
- data/examples/memory/09_error_handling_demo.rb +7 -10
- data/examples/memory/10_entity_addressing_basic.rb +25 -31
- data/examples/memory/11_entity_addressing_with_filtering.rb +32 -36
- data/examples/memory/12_regex_filtering_microservices.rb +10 -11
- data/examples/memory/13_header_block_configuration.rb +0 -5
- data/examples/memory/14_global_configuration_demo.rb +12 -14
- data/examples/memory/15_logger_demo.rb +0 -1
- data/examples/memory/README.md +37 -20
- 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 -24
- data/examples/redis/README.md +0 -2
- 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 +24 -17
- data/lib/smart_message/configuration.rb +2 -23
- data/lib/smart_message/dead_letter_queue.rb +1 -1
- 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 +37 -66
- data/lib/smart_message/plugins.rb +42 -41
- 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/base.rb +42 -8
- 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/memory_transport.rb +23 -4
- data/lib/smart_message/transport/partitioned_files.rb +46 -0
- data/lib/smart_message/transport/redis_transport.rb +11 -0
- data/lib/smart_message/transport/registry.rb +0 -1
- data/lib/smart_message/transport/stdout_transport.rb +73 -41
- data/lib/smart_message/transport/stdout_transport.rb.backup +88 -0
- data/lib/smart_message/transport.rb +0 -1
- data/lib/smart_message/version.rb +1 -1
- metadata +25 -37
- data/docs/guides/redis-queue-getting-started.md +0 -697
- data/docs/guides/redis-queue-patterns.md +0 -889
- data/docs/guides/redis-queue-production.md +0 -1091
- data/docs/transports/redis-enhanced-transport.md +0 -524
- data/docs/transports/redis-queue-transport.md +0 -1304
- data/examples/redis_enhanced/README.md +0 -319
- data/examples/redis_enhanced/enhanced_01_basic_patterns.rb +0 -233
- data/examples/redis_enhanced/enhanced_02_fluent_api.rb +0 -331
- data/examples/redis_enhanced/enhanced_03_dual_publishing.rb +0 -281
- data/examples/redis_enhanced/enhanced_04_advanced_routing.rb +0 -419
- data/examples/redis_queue/01_basic_messaging.rb +0 -221
- data/examples/redis_queue/01_comprehensive_examples.rb +0 -508
- data/examples/redis_queue/02_pattern_routing.rb +0 -405
- data/examples/redis_queue/03_fluent_api.rb +0 -422
- data/examples/redis_queue/04_load_balancing.rb +0 -486
- data/examples/redis_queue/05_microservices.rb +0 -735
- data/examples/redis_queue/06_emergency_alerts.rb +0 -777
- data/examples/redis_queue/07_queue_management.rb +0 -587
- data/examples/redis_queue/README.md +0 -366
- data/examples/redis_queue/enhanced_01_basic_patterns.rb +0 -233
- data/examples/redis_queue/enhanced_02_fluent_api.rb +0 -331
- data/examples/redis_queue/enhanced_03_dual_publishing.rb +0 -281
- data/examples/redis_queue/enhanced_04_advanced_routing.rb +0 -419
- data/examples/redis_queue/redis_queue_architecture.svg +0 -148
- 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/transport/redis_enhanced_transport.rb +0 -399
- data/lib/smart_message/transport/redis_queue_transport.rb +0 -555
- 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,51 +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
|
-
|
297
|
-
|
295
|
+
transport SmartMessage::Transport::MemoryTransport.new
|
296
|
+
|
298
297
|
# Configure multi-logger to demonstrate different logging approaches
|
299
298
|
logger SmartMessage::Logger::MultiLogger.new(
|
300
299
|
SmartMessage::Logger::EmojiConsoleLogger.new,
|
301
|
-
SmartMessage::Logger::FileLogger.new('
|
302
|
-
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')
|
303
302
|
)
|
304
303
|
end
|
305
|
-
|
306
|
-
def
|
307
|
-
message_header, message_payload =
|
304
|
+
|
305
|
+
def process(message)
|
306
|
+
message_header, message_payload = message
|
308
307
|
# Simulate the logger being called during processing
|
309
308
|
if logger
|
310
309
|
logger.info { "[SmartMessage] Received: #{self.name} (#{message_payload.bytesize} bytes)" }
|
311
310
|
end
|
312
|
-
|
311
|
+
|
313
312
|
# Process the message
|
314
313
|
order_data = JSON.parse(message_payload)
|
315
314
|
result = "Order #{order_data['order_id']} processed successfully"
|
316
|
-
|
315
|
+
|
317
316
|
puts "š¼ OrderProcessing: #{result}"
|
318
|
-
|
317
|
+
|
319
318
|
# Log processing completion
|
320
319
|
if logger
|
321
320
|
logger.info { "[SmartMessage] Processed: #{self.name} - #{result}" }
|
322
321
|
end
|
323
|
-
|
322
|
+
|
324
323
|
result
|
325
324
|
end
|
326
|
-
|
325
|
+
|
327
326
|
# Override publish to demonstrate logging hooks
|
328
327
|
def publish
|
329
328
|
# Log message creation
|
@@ -331,13 +330,13 @@ class OrderProcessingMessage < SmartMessage::Base
|
|
331
330
|
if logger_instance
|
332
331
|
logger_instance.debug { "[SmartMessage] Created: #{self.class.name}" }
|
333
332
|
end
|
334
|
-
|
333
|
+
|
335
334
|
# Log publishing
|
336
335
|
transport_instance = transport || self.class.transport
|
337
336
|
if logger_instance
|
338
337
|
logger_instance.info { "[SmartMessage] Published: #{self.class.name} via #{transport_instance.class.name.split('::').last}" }
|
339
338
|
end
|
340
|
-
|
339
|
+
|
341
340
|
# Call original publish method
|
342
341
|
super
|
343
342
|
rescue => error
|
@@ -352,39 +351,38 @@ end
|
|
352
351
|
# Notification message with different logger configuration
|
353
352
|
class NotificationMessage < SmartMessage::Base
|
354
353
|
description "User notifications with file-based logging configuration"
|
355
|
-
|
356
|
-
property :recipient,
|
354
|
+
|
355
|
+
property :recipient,
|
357
356
|
description: "Target recipient for the notification"
|
358
|
-
property :subject,
|
357
|
+
property :subject,
|
359
358
|
description: "Subject line or title of the notification"
|
360
|
-
property :body,
|
359
|
+
property :body,
|
361
360
|
description: "Main content body of the notification"
|
362
|
-
property :priority,
|
361
|
+
property :priority,
|
363
362
|
description: "Priority level of the notification (low, normal, high, urgent)"
|
364
|
-
|
363
|
+
|
365
364
|
config do
|
366
|
-
transport SmartMessage::Transport::
|
367
|
-
|
368
|
-
|
365
|
+
transport SmartMessage::Transport::MemoryTransport.new
|
366
|
+
|
369
367
|
# Use only file logger for notifications
|
370
|
-
logger SmartMessage::Logger::FileLogger.new('
|
368
|
+
logger SmartMessage::Logger::FileLogger.new('log/notifications.log', level: Logger::WARN)
|
371
369
|
end
|
372
|
-
|
373
|
-
def
|
374
|
-
message_header, message_payload =
|
370
|
+
|
371
|
+
def process(message)
|
372
|
+
message_header, message_payload = message
|
375
373
|
if logger
|
376
374
|
logger.info { "[SmartMessage] Received: #{self.name} (#{message_payload.bytesize} bytes)" }
|
377
375
|
end
|
378
|
-
|
376
|
+
|
379
377
|
notification_data = JSON.parse(message_payload)
|
380
378
|
result = "Notification sent to #{notification_data['recipient']}"
|
381
|
-
|
379
|
+
|
382
380
|
puts "š¬ Notification: #{result}"
|
383
|
-
|
381
|
+
|
384
382
|
if logger
|
385
383
|
logger.info { "[SmartMessage] Processed: #{self.name} - #{result}" }
|
386
384
|
end
|
387
|
-
|
385
|
+
|
388
386
|
result
|
389
387
|
end
|
390
388
|
end
|
@@ -393,35 +391,34 @@ end
|
|
393
391
|
# This demonstrates how to use Ruby's standard Logger in production code
|
394
392
|
class StandardLoggerMessage < SmartMessage::Base
|
395
393
|
description "Demonstrates integration with standard Ruby Logger for production logging"
|
396
|
-
|
397
|
-
property :content,
|
394
|
+
|
395
|
+
property :content,
|
398
396
|
description: "Main content of the message to be logged"
|
399
|
-
property :level,
|
397
|
+
property :level,
|
400
398
|
description: "Logging level for the message (debug, info, warn, error)"
|
401
|
-
|
399
|
+
|
402
400
|
config do
|
403
|
-
transport SmartMessage::Transport::
|
404
|
-
|
405
|
-
|
401
|
+
transport SmartMessage::Transport::MemoryTransport.new
|
402
|
+
|
406
403
|
# Example 1: Using Ruby's standard Logger directly
|
407
404
|
# Create a standard Ruby logger that logs to STDOUT
|
408
405
|
ruby_logger = Logger.new(STDOUT)
|
409
406
|
ruby_logger.level = Logger::INFO
|
410
407
|
ruby_logger.progname = 'SmartMessage'
|
411
|
-
|
408
|
+
|
412
409
|
# Wrap it in our adapter
|
413
|
-
logger SmartMessage::Logger::
|
414
|
-
|
410
|
+
logger SmartMessage::Logger::RubyLogger.new(ruby_logger)
|
411
|
+
|
415
412
|
# Example 2: Using a file-based Ruby logger (commented out)
|
416
413
|
# file_logger = Logger.new('application.log', 'daily') # Rotate daily
|
417
|
-
# logger SmartMessage::Logger::
|
418
|
-
|
414
|
+
# logger SmartMessage::Logger::RubyLogger.new(file_logger)
|
415
|
+
|
419
416
|
# Example 3: In Rails, you would use Rails.logger (commented out)
|
420
|
-
# logger SmartMessage::Logger::
|
417
|
+
# logger SmartMessage::Logger::RubyLogger.new(Rails.logger)
|
421
418
|
end
|
422
|
-
|
423
|
-
def
|
424
|
-
message_header, message_payload =
|
419
|
+
|
420
|
+
def process(message)
|
421
|
+
message_header, message_payload = message
|
425
422
|
data = JSON.parse(message_payload)
|
426
423
|
puts "š Processing: #{data['content']}"
|
427
424
|
"Processed"
|
@@ -431,22 +428,21 @@ end
|
|
431
428
|
# Example: Message using the built-in Default Logger
|
432
429
|
class DefaultLoggerMessage < SmartMessage::Base
|
433
430
|
description "Demonstrates SmartMessage's built-in default logger with auto-detection"
|
434
|
-
|
435
|
-
property :message,
|
431
|
+
|
432
|
+
property :message,
|
436
433
|
description: "The message content to be logged using default logger"
|
437
|
-
property :level,
|
434
|
+
property :level,
|
438
435
|
description: "Log level (debug, info, warn, error, fatal)"
|
439
|
-
|
436
|
+
|
440
437
|
config do
|
441
|
-
transport SmartMessage::Transport::
|
442
|
-
|
443
|
-
|
438
|
+
transport SmartMessage::Transport::MemoryTransport.new
|
439
|
+
|
444
440
|
# Use the built-in default logger - simplest option!
|
445
441
|
logger SmartMessage::Logger::Default.new
|
446
442
|
end
|
447
|
-
|
448
|
-
def
|
449
|
-
message_header, message_payload =
|
443
|
+
|
444
|
+
def process(message)
|
445
|
+
message_header, message_payload = message
|
450
446
|
data = JSON.parse(message_payload)
|
451
447
|
puts "šÆ DefaultLogger: Processing #{data['message']}"
|
452
448
|
"Processed with default logger"
|
@@ -457,19 +453,19 @@ end
|
|
457
453
|
class PriorityOrderService
|
458
454
|
def initialize
|
459
455
|
puts "š PriorityOrderService: Starting with custom logger..."
|
460
|
-
|
456
|
+
|
461
457
|
# Create a priority-specific logger
|
462
458
|
@priority_logger = SmartMessage::Logger::FileLogger.new(
|
463
|
-
'
|
459
|
+
'log/priority_orders.log',
|
464
460
|
level: Logger::DEBUG
|
465
461
|
)
|
466
462
|
end
|
467
|
-
|
463
|
+
|
468
464
|
def process_priority_order(order_data)
|
469
465
|
# Use class-level logger override for this specific processing
|
470
466
|
original_logger = OrderProcessingMessage.logger
|
471
467
|
OrderProcessingMessage.logger(@priority_logger)
|
472
|
-
|
468
|
+
|
473
469
|
begin
|
474
470
|
message = OrderProcessingMessage.new(**order_data, from: 'PriorityOrderService')
|
475
471
|
puts "ā” Processing priority order with dedicated logger"
|
@@ -486,22 +482,22 @@ end
|
|
486
482
|
class LoggerDemo
|
487
483
|
def run
|
488
484
|
puts "š Starting Custom Logger Demo\n"
|
489
|
-
|
485
|
+
|
490
486
|
# Clean up any existing log files for a fresh demo
|
491
487
|
FileUtils.rm_rf('logs') if Dir.exist?('logs')
|
492
|
-
|
488
|
+
|
493
489
|
# Subscribe to messages
|
494
490
|
OrderProcessingMessage.subscribe
|
495
491
|
NotificationMessage.subscribe
|
496
|
-
|
492
|
+
|
497
493
|
puts "\n" + "="*70
|
498
494
|
puts "Demonstrating Different Logger Configurations"
|
499
495
|
puts "="*70
|
500
|
-
|
496
|
+
|
501
497
|
# Demo 1: Using the built-in Default Logger (NEW!)
|
502
498
|
puts "\n--- Demo 1: Using SmartMessage Default Logger ---"
|
503
499
|
puts "The Default logger automatically uses Rails.logger or Ruby Logger"
|
504
|
-
|
500
|
+
|
505
501
|
# Use the DefaultLoggerMessage class defined above
|
506
502
|
DefaultLoggerMessage.subscribe
|
507
503
|
default_msg = DefaultLoggerMessage.new(
|
@@ -511,7 +507,7 @@ class LoggerDemo
|
|
511
507
|
)
|
512
508
|
default_msg.publish
|
513
509
|
sleep(0.5)
|
514
|
-
|
510
|
+
|
515
511
|
# Demo 2: Standard order with multi-logger
|
516
512
|
puts "\n--- Demo 2: Standard Order (Multi-Logger) ---"
|
517
513
|
order1 = OrderProcessingMessage.new(
|
@@ -524,7 +520,7 @@ class LoggerDemo
|
|
524
520
|
)
|
525
521
|
order1.publish
|
526
522
|
sleep(0.5)
|
527
|
-
|
523
|
+
|
528
524
|
# Demo 3: Notification with file-only logger
|
529
525
|
puts "\n--- Demo 3: Notification (File Logger Only) ---"
|
530
526
|
notification = NotificationMessage.new(
|
@@ -536,7 +532,7 @@ class LoggerDemo
|
|
536
532
|
)
|
537
533
|
notification.publish
|
538
534
|
sleep(0.5)
|
539
|
-
|
535
|
+
|
540
536
|
# Demo 4: Priority order with instance-level logger override
|
541
537
|
puts "\n--- Demo 4: Priority Order (Instance Logger Override) ---"
|
542
538
|
priority_service = PriorityOrderService.new
|
@@ -549,27 +545,27 @@ class LoggerDemo
|
|
549
545
|
from: 'PriorityOrderService'
|
550
546
|
)
|
551
547
|
sleep(0.5)
|
552
|
-
|
548
|
+
|
553
549
|
# Demo 5: Using standard Ruby logger
|
554
550
|
puts "\n--- Demo 5: Using Standard Ruby Logger ---"
|
555
|
-
|
551
|
+
|
556
552
|
# Use the StandardLoggerMessage class that demonstrates Ruby's standard logger
|
557
553
|
StandardLoggerMessage.subscribe
|
558
|
-
|
554
|
+
|
559
555
|
# Create and send a message - watch for the Ruby logger output
|
560
556
|
msg = StandardLoggerMessage.new(
|
561
557
|
content: "Testing with Ruby's standard logger",
|
562
558
|
level: "info",
|
563
559
|
from: 'LoggerDemo'
|
564
560
|
)
|
565
|
-
|
561
|
+
|
566
562
|
# The logger will output to STDOUT using Ruby's standard format
|
567
563
|
msg.publish
|
568
564
|
sleep(0.5)
|
569
|
-
|
565
|
+
|
570
566
|
puts "\nNote: The above used Ruby's standard Logger class wrapped for SmartMessage"
|
571
567
|
puts "You can use ANY Ruby logger this way: Logger.new, Rails.logger, etc."
|
572
|
-
|
568
|
+
|
573
569
|
# Demo 6: Error handling with logging
|
574
570
|
puts "\n--- Demo 6: Error Handling with Logging ---"
|
575
571
|
begin
|
@@ -581,24 +577,24 @@ class LoggerDemo
|
|
581
577
|
status: "error_demo",
|
582
578
|
from: 'LoggerDemo'
|
583
579
|
)
|
584
|
-
|
580
|
+
|
585
581
|
# Simulate an error during processing
|
586
582
|
if OrderProcessingMessage.logger
|
587
583
|
error = StandardError.new("Invalid order data provided")
|
588
584
|
OrderProcessingMessage.logger.error { "[SmartMessage] Error: Simulated error for demo - #{error.class.name}: #{error.message}" }
|
589
585
|
end
|
590
|
-
|
586
|
+
|
591
587
|
rescue => error
|
592
588
|
puts "š Caught demonstration error: #{error.message}"
|
593
589
|
end
|
594
|
-
|
590
|
+
|
595
591
|
# Show log file contents
|
596
592
|
puts "\n" + "="*70
|
597
593
|
puts "š Log File Contents"
|
598
594
|
puts "="*70
|
599
|
-
|
595
|
+
|
600
596
|
show_log_contents
|
601
|
-
|
597
|
+
|
602
598
|
puts "\n⨠Demo completed!"
|
603
599
|
puts "\nThis example demonstrated:"
|
604
600
|
puts "⢠SmartMessage::Logger::Default - Built-in logger that auto-detects Rails/Ruby"
|
@@ -612,18 +608,18 @@ class LoggerDemo
|
|
612
608
|
puts "\nKEY TAKEAWAY: Use SmartMessage::Logger::Default.new for instant logging!"
|
613
609
|
puts "It automatically uses Rails.logger in Rails or creates a Ruby Logger otherwise."
|
614
610
|
end
|
615
|
-
|
611
|
+
|
616
612
|
private
|
617
|
-
|
613
|
+
|
618
614
|
def show_log_contents
|
619
|
-
log_files = Dir.glob('
|
620
|
-
|
615
|
+
log_files = Dir.glob('log/*.log') + Dir.glob('log/*.json')
|
616
|
+
|
621
617
|
log_files.each do |log_file|
|
622
618
|
next unless File.exist?(log_file)
|
623
|
-
|
619
|
+
|
624
620
|
puts "\nš #{log_file}:"
|
625
621
|
puts "-" * 50
|
626
|
-
|
622
|
+
|
627
623
|
content = File.read(log_file)
|
628
624
|
if content.length > 500
|
629
625
|
puts content[0..500] + "\n... (truncated, #{content.length} total characters)"
|
@@ -631,7 +627,7 @@ class LoggerDemo
|
|
631
627
|
puts content
|
632
628
|
end
|
633
629
|
end
|
634
|
-
|
630
|
+
|
635
631
|
if log_files.empty?
|
636
632
|
puts "ā ļø No log files found (they may not have been created yet)"
|
637
633
|
end
|
@@ -642,4 +638,4 @@ end
|
|
642
638
|
if __FILE__ == $0
|
643
639
|
demo = LoggerDemo.new
|
644
640
|
demo.run
|
645
|
-
end
|
641
|
+
end
|