Module: Net::TCPClient::Logging::InstanceMethods
- Defined in:
- lib/net/tcp_client/logging.rb
Instance Method Summary collapse
-
#benchmark(level, message, params, &block) ⇒ Object
Measure the supplied block and log the message.
- #format_log_message(level, message = nil, payload = nil, exception = nil, duration = nil, &block) ⇒ Object
- #payload ⇒ Object
- #pop_tags(quantity = 1) ⇒ Object
- #push_tags(*tags) ⇒ Object
- #tagged(*tags) ⇒ Object (also: #with_tags)
- #tags ⇒ Object
-
#trace(*args) ⇒ Object
Log trace level messages as debug.
- #trace? ⇒ Boolean
- #trace_benchmark(*args) ⇒ Object
- #with_payload(payload) ⇒ Object
Instance Method Details
#benchmark(level, message, params, &block) ⇒ Object
Measure the supplied block and log the message
97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
# File 'lib/net/tcp_client/logging.rb', line 97 def benchmark(level, , params, &block) start = Time.now begin rc = block.call(params) if block exception = params[:exception] rc rescue Exception => exc exception = exc ensure end_time = Time.now # Extract options after block completes so that block can modify any of the options log_exception = params[:log_exception] || :partial on_exception_level = params[:on_exception_level] min_duration = params[:min_duration] || 0.0 payload = params[:payload] metric = params[:metric] duration = if block_given? 1000.0 * (end_time - start) else params[:duration] || raise('Mandatory block missing when :duration option is not supplied') end # Add scoped payload if self.payload payload = payload.nil? ? self.payload : self.payload.merge(payload) end if exception logged_exception = exception case log_exception when :full # On exception change the log level level = on_exception_level if on_exception_level when :partial # On exception change the log level level = on_exception_level if on_exception_level = "#{} -- Exception: #{exception.class}: #{exception.}" logged_exception = nil else logged_exception = nil end send(level, (level, , payload, logged_exception, duration, &block)) raise exception elsif duration >= min_duration # Only log if the block took longer than 'min_duration' to complete send(level, (level, , payload, logged_exception, duration, &block)) end end end |
#format_log_message(level, message = nil, payload = nil, exception = nil, duration = nil, &block) ⇒ Object
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/net/tcp_client/logging.rb', line 64 def (level, =nil, payload=nil, exception=nil, duration=nil, &block) if exception.nil? && payload && payload.is_a?(Exception) exception = payload payload = nil end if block && (result = block.call) if result.is_a?(String) = .nil? ? result : "\#{message} -- \#{result}" elsif payload && payload.respond_to?(:merge) payload.merge(result) else payload = result end end # Add scoped payload if self.payload payload = payload.nil? ? self.payload : self.payload.merge(payload) end = .collect { |tag| "[#{tag}]" }.join(" ") + " " if && (.size > 0) = .to_s.dup << ' -- ' << payload.inspect if payload << ' -- Exception: ' << "#{exception.class}: #{exception.}\n#{(exception.backtrace || []).join("\n")}" if exception duration_str = duration ? "(#{'%.1f' % duration}ms) " : '' "#{}#{duration_str} #{}" end |
#payload ⇒ Object
185 186 187 |
# File 'lib/net/tcp_client/logging.rb', line 185 def payload Thread.current[:semantic_logger_payload] end |
#pop_tags(quantity = 1) ⇒ Object
172 173 174 175 |
# File 'lib/net/tcp_client/logging.rb', line 172 def (quantity=1) t = Thread.current[:semantic_logger_tags] t.pop(quantity) unless t.nil? end |
#push_tags(*tags) ⇒ Object
163 164 165 166 167 168 169 170 |
# File 'lib/net/tcp_client/logging.rb', line 163 def * # Need to flatten and reject empties to support calls from Rails 4 = .flatten.collect(&:to_s).reject(&:empty?) t = Thread.current[:semantic_logger_tags] Thread.current[:semantic_logger_tags] = t.nil? ? : t.concat() end |
#tagged(*tags) ⇒ Object Also known as:
147 148 149 150 151 152 |
# File 'lib/net/tcp_client/logging.rb', line 147 def tagged(*) = (*) yield self ensure (.size) end |
#tags ⇒ Object
157 158 159 160 161 |
# File 'lib/net/tcp_client/logging.rb', line 157 def # Since tags are stored on a per thread basis this list is thread-safe t = Thread.current[:semantic_logger_tags] t.nil? ? [] : t.clone end |
#trace(*args) ⇒ Object
Log trace level messages as debug
52 53 54 |
# File 'lib/net/tcp_client/logging.rb', line 52 def trace(*args) debug(*args) end |
#trace? ⇒ Boolean
56 57 58 |
# File 'lib/net/tcp_client/logging.rb', line 56 def trace? debug? end |
#trace_benchmark(*args) ⇒ Object
60 61 62 |
# File 'lib/net/tcp_client/logging.rb', line 60 def trace_benchmark(*args) debug_benchmark(*args) end |
#with_payload(payload) ⇒ Object
177 178 179 180 181 182 183 |
# File 'lib/net/tcp_client/logging.rb', line 177 def with_payload(payload) current_payload = self.payload Thread.current[:semantic_logger_payload] = current_payload ? current_payload.merge(payload) : payload yield ensure Thread.current[:semantic_logger_payload] = current_payload end |