Class: Awestruct::AwestructLogFormatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/awestruct/logger.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#levelObject

Returns the value of attribute level.



28
29
30
# File 'lib/awestruct/logger.rb', line 28

def level
  @level
end

#prognameObject

Returns the value of attribute progname.



29
30
31
# File 'lib/awestruct/logger.rb', line 29

def progname
  @progname
end

Instance Method Details

#call(severity, timestamp, who, object) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/awestruct/logger.rb', line 31

def call(severity, timestamp, who, object)
  is_debug = $LOG.level == Logger::DEBUG
  if is_debug
    # callstack inspection, include our caller
    # turn this: "/usr/lib/ruby/1.8/irb/workspace.rb:52:in `irb_binding'"
    # into this: ["/usr/lib/ruby/1.8/irb/workspace.rb", "52", "irb_binding"]
    #
    # caller[3] is actually who invoked the Logger#<type>
    # This only works if you use the severity methods
    path, line, method = caller[3].split(/(?::in `|:|')/)
    # Trim RUBYLIB path from 'file' if we can
    #whence = $:.select { |p| path.start_with?(p) }[0]
    whence = $:.detect { |p| path.start_with?(p) }
    if !whence
      # We get here if the path is not in $:
      file = path
    else
      file = path[whence.length + 1..-1]
    end
    who = "#{file}:#{line}##{method}"
  end

  if severity[0] == 'D'
    "[%s] %5s -- %s [%s]\n" % [timestamp.strftime('%Y-%m-%d %H:%M:%S'), severity, object, who]
  else
    if severity[0] == 'W'
      ("%s" % [object]).colorize(:yellow) + "\n"
    elsif severity[0] == 'E'
      ("%s" % [object]).colorize(:red) + "\n"
    else
      "%s\n" % [object]
    end
  end
end