Class: Chainsaw::Detector

Inherits:
Object
  • Object
show all
Defined in:
lib/chainsaw/detector.rb

Constant Summary collapse

PATTERNS =
{
  :clf =>  {
    :pattern     => /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} (?:-|[^ ]+) (?:-|[^ ]+) \[(\d{2}\/[a-z]{3}\/\d{4}:\d{2}:\d{2}:\d{2} -\d{4})\]/i,
    :time_format => '%d/%b/%Y:%H:%M:%S %z'
  },
  :apache_error => {
    :pattern     => /^\[([a-z]{3} [a-z]{3} \d{2} \d{2}:\d{2}:\d{2} \d{4})\]/i,
    :time_format => '%a %b %d %H:%M:%S %Y'
  },
  :nginx_error => {
    :pattern     => /^(\d{4}\/\d{2}\/\d{2} \d{2}:\d{2}:\d{2})/i,
    :time_format => '%Y/%m/%d %H:%M:%S'
  },
  :ruby_logger => {
    :pattern     => /^[a-z]{1}, \[(\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2})\.\d+ #\d+\]/i,
    :time_format => '%Y-%m-%dT%H:%M:%S'
  },
  :rails => {
    :pattern     => /^started [a-z]+ "[^"]+" for \d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} at (\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2} -\d{4})/i,
    :time_format => '%Y-%m-%d %H:%M:%S %z'
  },
  :syslog => {
    :pattern     => /^([a-z]{3}  ?\d{1,2} \d{2}:\d{2}:\d{2})/i,
    :time_format => '%b %e %H:%M:%S'
  },
  :redis => {
    :pattern     => /^\[\d+\]  ?(\d{1,2} [a-z]{3} \d{2}:\d{2}:\d{2})/i,
    :time_format => '%e %b %H:%M:%S'
  },
  :puppet => {
    :pattern     => /^\[(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})\]/i,
    :time_format => '%Y-%m-%d %H:%M:%S'
  },
  :mongodb => {
    :pattern     => /^(\w{3} \w{3} \d{2} \d{2}:\d{2}:\d{2})/i,
    :time_format => '%a %b %d %H:%M:%S'
  },
  :rack => {
    :pattern     => /^\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} (?:-|[^ ]+) (?:-|[^ ]+) \[(\d{2}\/[a-z]{3}\/\d{4} \d{2}:\d{2}:\d{2})\]/i,
    :time_format => '%d/%b/%Y %H:%M:%S'
  },
  :python => {
    :pattern     => /^(\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2})/i,
    :time_format => '%Y-%m-%d %H:%M:%S'
  },
  :django => {
    :pattern     => /^\[(\d{2}\/[a-z]{3}\/\d{4} \d{2}:\d{2}:\d{2})\]/i,
    :time_format => '%d/%b/%Y %H:%M:%S'
  }
}

Class Method Summary collapse

Class Method Details

.detect(log) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/chainsaw/detector.rb', line 54

def self.detect(log)
  type = nil

  log.each_line do |line|
    type = get_type(line)
    break unless type.nil?
  end

  if type.nil?
    puts "\033[31mUnable to determine log format :(\033[0m"
    exit
  else
    Format.new(type, PATTERNS[type])
  end
end

.get_type(line) ⇒ Object



70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/chainsaw/detector.rb', line 70

def self.get_type(line)
  type = nil
  
  PATTERNS.each do |key, value|
    if line.match(value[:pattern])
      type = key
      break
    end
  end

  type
end