Class: Lapine::AnnotatedLogger

Inherits:
Logger
  • Object
show all
Defined in:
lib/lapine/annotated_logger.rb

Constant Summary collapse

NUMBER_TO_COLOR_MAP =
{"debug" => '0;37', "info" => '32', "warn" => '33', "error" => '31', "fatal" => '31', "unknown" => '37'}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ AnnotatedLogger

Returns a new instance of AnnotatedLogger.



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/lapine/annotated_logger.rb', line 10

def initialize *args
  super *args
  [:info, :debug, :warn, :error, :fatal].each { |m|
    AnnotatedLogger.class_eval %Q!
  def #{m} arg=nil, &block
    level = "#{m}"
    pid = "%.5d:" % $$
    if block_given?
      arg = yield
    end
    out = arg
    out = out.gsub(/\n/, ' ') unless (level == "fatal" || out =~ /\\w+\\.rb:\\d+:in/m)
    t = Time.now
    l = log_message(t, pid, level, out)
    super(l) if l
  end
  !
  }
end

Instance Attribute Details

#colorize_loggingObject

if set turns on colors (hint: turn off in production)



6
7
8
# File 'lib/lapine/annotated_logger.rb', line 6

def colorize_logging
  @colorize_logging
end

#log_method_callerObject

if set will log ruby method name substring from where logging is called



4
5
6
# File 'lib/lapine/annotated_logger.rb', line 4

def log_method_caller
  @log_method_caller
end

#log_timestampsObject

if set will log timestamps up to millisecond



5
6
7
# File 'lib/lapine/annotated_logger.rb', line 5

def log_timestamps
  @log_timestamps
end

Instance Method Details

#caller_methodObject



60
61
62
# File 'lib/lapine/annotated_logger.rb', line 60

def caller_method
  parse_caller(caller(3).first)
end

#log_message(t, pid, level, out) ⇒ 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
58
# File 'lib/lapine/annotated_logger.rb', line 30

def log_message(t, pid, level, out)
  color_on = color_off = sql_color_on = ""
  if self.colorize_logging
    color = NUMBER_TO_COLOR_MAP[level.to_s]
    color_on = "\033[#{color}m"
    sql_color_on = "\033[34m"
    color_off = "\033[0m"
  end
  format_string = ""
  format_values = []
  if self.log_timestamps
    format_string << "%s.%03d "
    format_values << [t.strftime("%Y-%m-%d %H:%M:%S"), t.usec / 1000]
  end
  format_string << "%s #{color_on}%6.6s#{color_off} "
  format_values << [pid, level]

  if self.log_method_caller
    file, line, method = caller_method
    format_string << "|%-40.40s "
    format_values << "#{File.basename(file)}:#{line}:#{method}"
  end

  format_string << "%s"
  format_values << [out]
  format_values.flatten!

  format_string % format_values
end

#parse_caller(at) ⇒ Object



64
65
66
67
68
69
70
71
# File 'lib/lapine/annotated_logger.rb', line 64

def parse_caller(at)
  if /^(.+?):(\d+)(?::in `(.*)')?/ =~ at
    file = Regexp.last_match[1]
    line = Regexp.last_match[2].to_i
    method = Regexp.last_match[3]
    [file, line, method]
  end
end