Skip to content

Commit 2493ee6

Browse files
authored
Разделение log message на части (#32)
* Разделение log message на части * Linter * Add dots * Lint * Move duplicate to helper * Lint * module * Update version and changelog * Update Gemfile.lock
1 parent 207c46c commit 2493ee6

File tree

9 files changed

+57
-8
lines changed

9 files changed

+57
-8
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
# Changelog
22
All notable changes to this project will be documented in this file.
33

4+
## [1.5.0] - 2025-05-19
5+
### Added
6+
- Added ability to split log message into parts
7+
- Optional `logger_message_size_limit` config for message size limit
8+
49
## [1.4.0] - 2025-03-10
510
### Added
611
- Introduced new configuration attributes for connection reset handling:

Gemfile.lock

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ GIT
88
PATH
99
remote: .
1010
specs:
11-
rabbit_messaging (1.4.0)
11+
rabbit_messaging (1.5.0)
1212
bunny (~> 2.0)
1313
kicks
1414
tainbox

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,14 @@ require "rabbit_messaging"
151151
```ruby
152152
config.connection_reset_timeout = 0.2
153153
```
154+
155+
- `logger_message_size_limit` (`Integer`)
156+
157+
Maximum logger message size. Split message to parts if message is more than limit. Default: 9500.
158+
159+
```ruby
160+
config.logger_message_size_limit = 9500
161+
```
154162
---
155163
156164
### Client

lib/rabbit.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ class Config
3333
attribute :connection_reset_max_retries, Integer, default: 10
3434
attribute :connection_reset_timeout, Float, default: 0.2
3535
attribute :connection_reset_exceptions, Array, default: [Bunny::ConnectionClosedError]
36+
attribute :logger_message_size_limit, Integer, default: 9_500
3637

3738
attribute :receive_logger, default: lambda {
3839
Logger.new(Rabbit.root.join("log", "incoming_rabbit_messages.log"))

lib/rabbit/helper.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# frozen_string_literal: true
2+
3+
module Rabbit
4+
module Helper
5+
def self.generate_message(message_part, parts, index)
6+
if parts == 1
7+
message_part
8+
elsif index.zero?
9+
"#{message_part}..."
10+
elsif index == parts - 1
11+
"...#{message_part}"
12+
else
13+
"...#{message_part}..."
14+
end
15+
end
16+
end
17+
end

lib/rabbit/publishing.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,13 @@ def log(message)
6666
message.event, message.confirm_select? ? "confirm" : "no-confirm"
6767
]
6868

69-
@logger.debug "#{metadata.join ' / '}: #{JSON.dump(message.data)}"
69+
message_parts = JSON.dump(message.data)
70+
.scan(/.{1,#{Rabbit.config.logger_message_size_limit}}/)
71+
72+
message_parts.each_with_index do |message_part, index|
73+
message = Rabbit::Helper.generate_message(message_part, message_parts.size, index)
74+
@logger.debug "#{metadata.join ' / '}: #{message}"
75+
end
7076
end
7177

7278
def reinitialize_channels_pool

lib/rabbit/receiving/receive.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
require "rabbit"
66
require "rabbit/receiving/queue"
77
require "rabbit/receiving/job"
8+
require "rabbit/helper"
89

910
class Rabbit::Receiving::Receive
1011
include Tainbox
@@ -21,9 +22,15 @@ def call
2122
end
2223

2324
def log!
24-
Rabbit.config.receive_logger.debug(
25-
[message, delivery_info, arguments].join(" / "),
26-
)
25+
message_parts = message.scan(/.{1,#{Rabbit.config.logger_message_size_limit}}/)
26+
27+
message_parts.each_with_index do |message_part, index|
28+
message = Rabbit::Helper.generate_message(message_part, message_parts.size, index)
29+
30+
Rabbit.config.receive_logger.debug(
31+
[message, delivery_info, arguments].join(" / "),
32+
)
33+
end
2734
end
2835

2936
def process_message

lib/rabbit/version.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# frozen_string_literal: true
22

33
module Rabbit
4-
VERSION = "1.4.0"
4+
VERSION = "1.5.0"
55
end

spec/units/rabbit_spec.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
allow(channel).to receive(:open?).and_return(true)
3131

3232
allow(Rabbit.config).to receive(:publish_logger) { publish_logger }
33+
allow(Rabbit.config).to receive(:logger_message_size_limit).and_return(10)
3334

3435
expect(channel).to receive(:confirm_select).once
3536
allow(channel).to receive(:wait_for_confirms).and_return(true)
@@ -70,9 +71,13 @@
7071
expect(Rabbit::Publishing::Job).not_to receive(:perform_later)
7172
end
7273

73-
expect(publish_logger).to receive(:debug).with(<<~MSG.strip).once
74+
expect(publish_logger).to receive(:debug).with(<<~MSG.strip)
7475
test_group_id.test_project_id.some_exchange / some_queue / {"foo":"bar"} / some_event / \
75-
confirm: {"hello":"world"}
76+
confirm: {"hello":"...
77+
MSG
78+
expect(publish_logger).to receive(:debug).with(<<~MSG.strip)
79+
test_group_id.test_project_id.some_exchange / some_queue / {"foo":"bar"} / some_event / \
80+
confirm: ...world"}
7681
MSG
7782
described_class.publish(message_options)
7883
end

0 commit comments

Comments
 (0)