class Logster::Message

Constants

ALLOWED_ENV
LOGSTER_ENV

Attributes

backtrace[RW]
count[RW]
env[RW]
key[RW]
message[RW]
progname[RW]
protected[RW]
severity[RW]
timestamp[RW]

Public Class Methods

from_json(json) click to toggle source
# File lib/logster/message.rb, line 46
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
end
new(severity, progname, message, timestamp = nil, key = nil) click to toggle source
# File lib/logster/message.rb, line 17
def initialize(severity, progname, message, timestamp = nil, key = nil)
  @timestamp = timestamp || get_timestamp
  @severity = severity
  @progname = progname
  @message = message
  @key = key || SecureRandom.hex
  @backtrace = nil
  @count = 1
  @protected = false
end
populate_from_env(env) click to toggle source
# File lib/logster/message.rb, line 64
def self.populate_from_env(env)
  env[LOGSTER_ENV] ||= begin
      unless env.include? "rack.input"
        # Not a web request
        return env
      end
      scrubbed = {}
      request = Rack::Request.new(env)
      params = {}
      request.params.each do |k,v|
        if k.include? "password"
          params[k] = "[redacted]"
        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

Public Instance Methods

<=>(other) click to toggle source
# File lib/logster/message.rb, line 88
def <=>(other)
  time = self.timestamp <=> other.timestamp
  return time if time && time != 0

  self.key <=> other.key
end
=~(pattern) click to toggle source
# File lib/logster/message.rb, line 95
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
populate_from_env(env) click to toggle source
# File lib/logster/message.rb, line 59
def populate_from_env(env)
  @env = Message.populate_from_env(env)
end
to_h() click to toggle source
# File lib/logster/message.rb, line 28
def to_h
  {
    message: @message,
    progname: @progname,
    severity: @severity,
    timestamp: @timestamp,
    key: @key,
    backtrace: @backtrace,
    count: @count,
    env: @env,
    protected: @protected
  }
end
to_json(opts = nil) click to toggle source
# File lib/logster/message.rb, line 42
def to_json(opts = nil)
  JSON.fast_generate(to_h, opts)
end

Protected Instance Methods

get_timestamp() click to toggle source
# File lib/logster/message.rb, line 112
def get_timestamp
  (Time.new.to_f * 1000).to_i
end