File tree Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Expand file tree Collapse file tree 2 files changed +43
-1
lines changed Original file line number Diff line number Diff line change @@ -30,6 +30,12 @@ module Protocol
30
30
# @api semipublic
31
31
class Query < Message
32
32
33
+ # Constant for the max number of characters to print when inspecting
34
+ # a query field.
35
+ #
36
+ # @since 2.0.3
37
+ LOG_STRING_LIMIT = 250
38
+
33
39
# Creates a new Query message
34
40
#
35
41
# @example Find all users named Tyler.
@@ -78,7 +84,7 @@ def log_message
78
84
fields = [ ]
79
85
fields << [ "%s |" , query_type ]
80
86
fields << [ "namespace=%s" , namespace ]
81
- fields << [ "selector=%s" , selector . inspect ]
87
+ fields << [ "selector=%s" , formatted_selector ]
82
88
fields << [ "flags=%s" , flags . inspect ]
83
89
fields << [ "limit=%s" , limit . inspect ]
84
90
fields << [ "skip=%s" , skip . inspect ]
@@ -111,6 +117,13 @@ def query_type
111
117
namespace . include? ( Database ::COMMAND ) ? 'COMMAND' : 'QUERY'
112
118
end
113
119
120
+ def formatted_selector
121
+ ( ( str = selector . inspect ) . length > LOG_STRING_LIMIT ) ?
122
+ "#{ str [ 0 ..LOG_STRING_LIMIT ] } ..." : str
123
+ rescue ArgumentError
124
+ '<Could not inspect selector>'
125
+ end
126
+
114
127
# Available flags for a Query message.
115
128
FLAGS = [
116
129
:reserved ,
Original file line number Diff line number Diff line change 282
282
end
283
283
end
284
284
end
285
+
286
+ describe '#log_message' do
287
+
288
+ context 'when the selector is greater than LOG_STRING_LIMIT characters' do
289
+
290
+ let ( :selector ) do
291
+ 'z' *260
292
+ end
293
+
294
+ it 'Only prints LOG_STRING_LIMIT number of characters' do
295
+ expect ( message . log_message . scan ( /z/ ) . length ) . to eq ( Mongo ::Protocol ::Query ::LOG_STRING_LIMIT )
296
+ end
297
+ end
298
+
299
+ context 'when the selector cannot be inspected' do
300
+
301
+ let ( :selector ) do
302
+ 'invalid string'
303
+ end
304
+
305
+ before do
306
+ allow ( selector ) . to receive ( :inspect ) . and_raise ( ArgumentError )
307
+ end
308
+
309
+ it 'Does not include the selector in the log message' do
310
+ expect ( message . log_message . scan ( /invalid string/ ) . length ) . to eq ( 0 )
311
+ end
312
+ end
313
+ end
285
314
end
You can’t perform that action at this time.
0 commit comments