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.



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

def level
  @level
end

#prognameObject

Returns the value of attribute progname.



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

def progname
  @progname
end

Instance Method Details

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



30
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
# File 'lib/awestruct/logger.rb', line 30

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 =~ /DEBUG/
    "[%s] %5s -- %s [%s]\n" % [timestamp.strftime('%Y-%m-%d %H:%M:%S'), severity, object, who]
  else
    "%s\n" % [object]
  end
end