Class: Progstr::LogMessage

Inherits:
Object
  • Object
show all
Defined in:
lib/progstr/log_message.rb

Constant Summary collapse

LEVELS =
{ :info => 0, :warning => 1, :error => 2, :fatal => 3 }

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ LogMessage

Returns a new instance of LogMessage.



8
9
10
11
12
13
14
# File 'lib/progstr/log_message.rb', line 8

def initialize(params)
  @text = params[:text]
  @source = params[:source]
  @host = params[:host]
  @level = params[:level]
  @time = params[:time]
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



6
7
8
# File 'lib/progstr/log_message.rb', line 6

def host
  @host
end

#levelObject (readonly)

Returns the value of attribute level.



6
7
8
# File 'lib/progstr/log_message.rb', line 6

def level
  @level
end

#sourceObject (readonly)

Returns the value of attribute source.



6
7
8
# File 'lib/progstr/log_message.rb', line 6

def source
  @source
end

#textObject (readonly)

Returns the value of attribute text.



6
7
8
# File 'lib/progstr/log_message.rb', line 6

def text
  @text
end

#timeObject (readonly)

Returns the value of attribute time.



6
7
8
# File 'lib/progstr/log_message.rb', line 6

def time
  @time
end

Instance Method Details

#encode_time(time) ⇒ Object



20
21
22
23
# File 'lib/progstr/log_message.rb', line 20

def encode_time(time)
  time ||= Time.now
  (time.to_f * 1000).to_i
end

#get_host(host) ⇒ Object



25
26
27
# File 'lib/progstr/log_message.rb', line 25

def get_host(host)
  host || Socket.gethostname || ENV["HOST"] || "Unknown"
end

#to_hashObject



16
17
18
# File 'lib/progstr/log_message.rb', line 16

def to_hash
  {:text => @text, :source => @source, :host => get_host(@host), :level => LEVELS[@level], :time => encode_time(@time)}
end