Module: Net::TCPClient::Logging::InstanceMethods

Defined in:
lib/net/tcp_client/logging.rb

Instance Method Summary collapse

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, message, 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
        message          = "#{message} -- Exception: #{exception.class}: #{exception.message}"
        logged_exception = nil
      else
        logged_exception = nil
      end
      send(level, format_log_message(level, message, 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, format_log_message(level, message, 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 format_log_message(level, message=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)
      message = message.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

  tags_str = tags.collect { |tag| "[#{tag}]" }.join(" ") + " " if tags && (tags.size > 0)

  message = message.to_s.dup
  message << ' -- ' << payload.inspect if payload
  message << ' -- Exception: ' << "#{exception.class}: #{exception.message}\n#{(exception.backtrace || []).join("\n")}" if exception

  duration_str = duration ? "(#{'%.1f' % duration}ms) " : ''

  "#{tags_str}#{duration_str} #{message}"
end

#payloadObject



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 pop_tags(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 push_tags *tags
  # Need to flatten and reject empties to support calls from Rails 4
  new_tags = tags.flatten.collect(&:to_s).reject(&:empty?)
  t        = Thread.current[:semantic_logger_tags]

  Thread.current[:semantic_logger_tags] = t.nil? ? new_tags : t.concat(new_tags)
  new_tags
end

#tagged(*tags) ⇒ Object Also known as: with_tags



147
148
149
150
151
152
# File 'lib/net/tcp_client/logging.rb', line 147

def tagged(*tags)
  new_tags = push_tags(*tags)
  yield self
ensure
  pop_tags(new_tags.size)
end

#tagsObject



157
158
159
160
161
# File 'lib/net/tcp_client/logging.rb', line 157

def tags
  # 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

Returns:

  • (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