Class: Logster::Message
- Inherits:
-
Object
- Object
- Logster::Message
- Defined in:
- lib/logster/message.rb
Constant Summary collapse
- LOGSTER_ENV =
"_logster_env".freeze
- ALLOWED_ENV =
%w{ HTTP_HOST REQUEST_URI REQUEST_METHOD HTTP_USER_AGENT HTTP_ACCEPT HTTP_REFERER HTTP_X_FORWARDED_FOR HTTP_X_REAL_IP hostname process_id application_version }
Instance Attribute Summary collapse
-
#backtrace ⇒ Object
Returns the value of attribute backtrace.
-
#count ⇒ Object
Returns the value of attribute count.
-
#env ⇒ Object
Returns the value of attribute env.
-
#first_timestamp ⇒ Object
Returns the value of attribute first_timestamp.
-
#key ⇒ Object
Returns the value of attribute key.
-
#message ⇒ Object
Returns the value of attribute message.
-
#progname ⇒ Object
Returns the value of attribute progname.
-
#protected ⇒ Object
Returns the value of attribute protected.
-
#severity ⇒ Object
Returns the value of attribute severity.
-
#timestamp ⇒ Object
Returns the value of attribute timestamp.
Class Method Summary collapse
- .default_env ⇒ Object
- .from_json(json) ⇒ Object
- .hostname ⇒ Object
- .populate_env_helper(env) ⇒ Object
- .populate_from_env(env) ⇒ Object
Instance Method Summary collapse
- #<=>(other) ⇒ Object
- #=~(pattern) ⇒ Object
-
#grouping_hash ⇒ Object
in its own method so it can be overridden.
-
#grouping_key ⇒ Object
todo - memoize?.
-
#initialize(severity, progname, message, timestamp = nil, key = nil, count: 1) ⇒ Message
constructor
A new instance of Message.
- #is_similar?(other) ⇒ Boolean
- #merge_similar_message(other) ⇒ Object
- #populate_from_env(env) ⇒ Object
-
#solved_keys ⇒ Object
todo - memoize?.
- #to_h(exclude_env: false) ⇒ Object
- #to_json(opts = nil) ⇒ Object
Constructor Details
#initialize(severity, progname, message, timestamp = nil, key = nil, count: 1) ⇒ Message
Returns a new instance of Message.
26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/logster/message.rb', line 26 def initialize(severity, progname, , = nil, key = nil, count: 1) @timestamp = || @severity = severity @progname = progname @message = @key = key || SecureRandom.hex @backtrace = nil @count = count || 1 @protected = false @first_timestamp = nil end |
Instance Attribute Details
#backtrace ⇒ Object
Returns the value of attribute backtrace.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def backtrace @backtrace end |
#count ⇒ Object
Returns the value of attribute count.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def count @count end |
#env ⇒ Object
Returns the value of attribute env.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def env @env end |
#first_timestamp ⇒ Object
Returns the value of attribute first_timestamp.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def @first_timestamp end |
#key ⇒ Object
Returns the value of attribute key.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def key @key end |
#message ⇒ Object
Returns the value of attribute message.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def @message end |
#progname ⇒ Object
Returns the value of attribute progname.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def progname @progname end |
#protected ⇒ Object
Returns the value of attribute protected.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def protected @protected end |
#severity ⇒ Object
Returns the value of attribute severity.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def severity @severity end |
#timestamp ⇒ Object
Returns the value of attribute timestamp.
24 25 26 |
# File 'lib/logster/message.rb', line 24 def @timestamp end |
Class Method Details
.default_env ⇒ Object
92 93 94 95 96 97 98 99 |
# File 'lib/logster/message.rb', line 92 def self.default_env env = { "hostname" => hostname, "process_id" => Process.pid } env["application_version"] = Logster.config.application_version if Logster.config.application_version env end |
.from_json(json) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/logster/message.rb', line 61 def self.from_json(json) parsed = ::JSON.parse(json) msg = new(parsed["severity"], parsed["progname"], parsed["message"], parsed["timestamp"], parsed["key"]) msg.backtrace = parsed["backtrace"] msg.env = parsed["env"] msg.count = parsed["count"] msg.protected = parsed["protected"] msg. = parsed["first_timestamp"] msg end |
.hostname ⇒ Object
76 77 78 |
# File 'lib/logster/message.rb', line 76 def self.hostname @hostname ||= `hostname`.strip! rescue "<unknown>" end |
.populate_env_helper(env) ⇒ Object
158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/logster/message.rb', line 158 def self.populate_env_helper(env) env[LOGSTER_ENV] ||= begin unless env.include? "rack.input" # Not a web request return env end scrubbed = default_env request = Rack::Request.new(env) params = {} request.params.each do |k, v| if k.include? "password" params[k] = "[redacted]" elsif Array === v params[k] = v[0..20] else params[k] = v && v[0..100] end end scrubbed["params"] = params if params.length > 0 ALLOWED_ENV.map { |k| scrubbed[k] = env[k] if env[k] } scrubbed end end |
.populate_from_env(env) ⇒ Object
148 149 150 151 152 153 154 155 156 |
# File 'lib/logster/message.rb', line 148 def self.populate_from_env(env) if Array === env env.map do |single_env| self.populate_env_helper(single_env) end else self.populate_env_helper(env) end end |
Instance Method Details
#<=>(other) ⇒ Object
184 185 186 187 188 189 |
# File 'lib/logster/message.rb', line 184 def <=>(other) time = self. <=> other. return time if time && time != 0 self.key <=> other.key end |
#=~(pattern) ⇒ Object
191 192 193 194 195 196 197 198 199 200 201 202 203 204 |
# File 'lib/logster/message.rb', line 191 def =~(pattern) case pattern when Hash IgnorePattern.new(nil, pattern).matches? self when String IgnorePattern.new(pattern, nil).matches? self when Regexp IgnorePattern.new(pattern, nil).matches? self when IgnorePattern pattern.matches? self else nil end end |
#grouping_hash ⇒ Object
in its own method so it can be overridden
102 103 104 |
# File 'lib/logster/message.rb', line 102 def grouping_hash return { message: self., severity: self.severity, backtrace: self.backtrace } end |
#grouping_key ⇒ Object
todo - memoize?
107 108 109 |
# File 'lib/logster/message.rb', line 107 def grouping_key Digest::SHA1.hexdigest JSON.fast_generate grouping_hash end |
#is_similar?(other) ⇒ Boolean
128 129 130 |
# File 'lib/logster/message.rb', line 128 def is_similar?(other) self.grouping_key == other.grouping_key end |
#merge_similar_message(other) ⇒ Object
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 |
# File 'lib/logster/message.rb', line 132 def (other) self. ||= self. self. = [self., other.].max self.count += other.count || 1 return false if self.count > Logster::MAX_GROUPING_LENGTH other_env = JSON.load JSON.fast_generate other.env if Array === self.env Array === other_env ? self.env.concat(other_env) : self.env << other_env else Array === other_env ? self.env = [self.env, *other_env] : self.env = [self.env, other_env] end true end |
#populate_from_env(env) ⇒ Object
80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/logster/message.rb', line 80 def populate_from_env(env) env ||= {} if Array === env env = env.map do |single_env| self.class.default_env.merge(single_env) end else env = self.class.default_env.merge(env) end @env = Message.populate_from_env(env) end |
#solved_keys ⇒ Object
todo - memoize?
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/logster/message.rb', line 112 def solved_keys if Array === env versions = env.map { |single_env| single_env["application_version"] } else versions = env["application_version"] end if versions && backtrace && backtrace.length > 0 versions = [versions] if String === versions versions.map do |version| Digest::SHA1.hexdigest "#{version} #{backtrace}" end end end |
#to_h(exclude_env: false) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/logster/message.rb', line 38 def to_h(exclude_env: false) h = { message: @message, progname: @progname, severity: @severity, timestamp: @timestamp, key: @key, backtrace: @backtrace, count: @count, protected: @protected } h[:first_timestamp] = @first_timestamp if @first_timestamp h[:env] = @env unless exclude_env h end |
#to_json(opts = nil) ⇒ Object
56 57 58 59 |
# File 'lib/logster/message.rb', line 56 def to_json(opts = nil) exclude_env = Hash === opts && opts.delete(:exclude_env) JSON.fast_generate(to_h(exclude_env: exclude_env), opts) end |