Class: Logbert::Message

Inherits:
Object
  • Object
show all
Defined in:
lib/logbert/message.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(logger, level, time, pid, exception, options, content = nil, &content_proc) ⇒ Message

Returns a new instance of Message.



6
7
8
9
10
11
12
13
14
15
16
# File 'lib/logbert/message.rb', line 6

def initialize(logger, level, time, pid, exception, options, content = nil, &content_proc)
  @logger       = logger
  @level        = level
  @time         = time
  @pid          = pid
  @exception    = exception
  @options      = options

  @content      = content
  @content_proc = content_proc
end

Instance Attribute Details

#content_procObject (readonly)

Returns the value of attribute content_proc.



4
5
6
# File 'lib/logbert/message.rb', line 4

def content_proc
  @content_proc
end

#exceptionObject (readonly)

Returns the value of attribute exception.



4
5
6
# File 'lib/logbert/message.rb', line 4

def exception
  @exception
end

#levelObject (readonly)

Returns the value of attribute level.



4
5
6
# File 'lib/logbert/message.rb', line 4

def level
  @level
end

#loggerObject (readonly)

Returns the value of attribute logger.



4
5
6
# File 'lib/logbert/message.rb', line 4

def logger
  @logger
end

#optionsObject (readonly)

Returns the value of attribute options.



4
5
6
# File 'lib/logbert/message.rb', line 4

def options
  @options
end

#pidObject (readonly)

Returns the value of attribute pid.



4
5
6
# File 'lib/logbert/message.rb', line 4

def pid
  @pid
end

#timeObject (readonly)

Returns the value of attribute time.



4
5
6
# File 'lib/logbert/message.rb', line 4

def time
  @time
end

Class Method Details

.convert_exception(exc) ⇒ Object



41
42
43
44
45
46
47
48
49
50
51
# File 'lib/logbert/message.rb', line 41

def self.convert_exception(exc)
  if exc.is_a? Exception
    return {
      exc_class:     exc.exception.class,
      exc_message:   exc.exception.message,
      exc_backtrace: exc.exception.backtrace
    }
  else
    return exc
  end
end

.create(logger, level, exception, options, content = nil, &content_proc) ⇒ Object



18
19
20
# File 'lib/logbert/message.rb', line 18

def self.create(logger, level, exception, options, content = nil, &content_proc)
  Message.new logger, level, Time.now, Process.pid, Message.convert_exception(exception), options, content, &content_proc
end

.from_json(json_msg) ⇒ Object



22
23
24
25
26
# File 'lib/logbert/message.rb', line 22

def self.from_json(json_msg)
  l = Level.new(json_msg[:level_name], json_msg[:level_value])
  # note: the exception key contains a hash-level representation of an exception
  Message.create(json_msg[:logger], l, json_msg[:exception], json_msg[:options], json_msg[:content], json_msg[:content_proc])
end

Instance Method Details

#contentObject

Returns the content. If the content has not been created yet, then call @content_proc and save the value.



55
56
57
58
59
60
61
62
63
# File 'lib/logbert/message.rb', line 55

def content
  @content ||= begin
    if @content_proc
      @content_proc.call.to_s
    else
      ""
    end
  end
end

#to_jsonObject



28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/logbert/message.rb', line 28

def to_json
  return {
    logger:       @logger.to_s,
    level:        {level_value: @level.value, level_name: @level.name},
    time:         @time.to_s,
    pid:          @pid,
    exception:    @exception,
    options:      @options,
    content:      self.content,
    content_proc: nil
  }
end