- Improve logging. If the logger level == Logger::DEBUG, then we'll log the

caller file, line number, and method. Example:

  With 'logger.level == Logger::DEBUG'
  I, [2010-10-28T03:38:23.984737 #25011]  INFO -- logtest.rb:9#foo: Hello
     ^ includes source file, line, and method. Source file gets trimmed
     if it's in $: (RUBYLIB path)
                                                  
  With 'logger.level != Logger::DEBUG'
  I, [2010-10-28T03:37:42.235899 #24998]  INFO -- logtest.rb: Hello
                                                     ^ progname, aka $0
This commit is contained in:
Jordan Sissel 2010-10-28 10:39:13 +00:00
parent b711b9d870
commit fb72212e36
4 changed files with 47 additions and 21 deletions

View file

@ -39,8 +39,7 @@ class LogStash::Filters::Date
# TODO(sissel): check event.message, too.
if (event.fields.include?(field) rescue false)
fieldvalue = event.fields[field]
#fieldvalue = [fieldvalue] if fieldvalue.is_a?(String)
@logger.info fieldvalue
fieldvalue = [fieldvalue] if fieldvalue.is_a?(String)
fieldvalue.each do |value|
#value = event["fields"][field]
begin

View file

@ -14,7 +14,7 @@ class LogStash::Filters::Grok
def register
# TODO(sissel): Make patterns files come from the config
@config.each do |tag, tagconfig|
@logger.debug("Grok tag #{tag}")
@logger.debug("Registering tag with grok: #{tag}")
pile = Grok::Pile.new
pile.add_patterns_from_file("patterns/grok-patterns")
pile.add_patterns_from_file("patterns/linux-syslog")
@ -32,7 +32,6 @@ class LogStash::Filters::Grok
if !event.tags.empty?
event.tags.each do |tag|
@logger.info @grokpiles.keys
if @grokpiles.include?(tag)
pile = @grokpiles[tag]
grok, match = pile.match(message)

View file

@ -7,7 +7,6 @@ require "socket" # for Socket.gethostname
class LogStash::Inputs::File
def initialize(url, config={}, &block)
@logger = Logger.new(STDERR)
@logger.level = $DEBUG ? Logger::DEBUG : Logger::WARN
@url = url
@url = URI.parse(url) if url.is_a? String

View file

@ -3,26 +3,55 @@ require "logger"
require "ap"
class LogStash::Logger < Logger
@@formatter = LogStash::Logger::Formatter.new
def initialize(*args)
super(*args)
@formatter = @@formatter
end
end
@formatter = LogStash::Logger::Formatter.new
# Set default loglevel to WARN unless $DEBUG is set (run with 'ruby -d')
self.send(:level=, $DEBUG ? Logger::DEBUG: Logger::WARN)
@formatter.progname = self.send(:progname=, File.basename($0))
info("Using formatter: #{@formatter}")
end # def initialize
def level=(level)
super(level)
@formatter.level = level
end # def level=
end # class LogStash::Logger
# Implement a custom Logger::Formatter that uses awesome_inspect on non-strings.
class LogStash::Logger::Formatter < Logger::Formatter
def call(level, timestamp, progname, object)
if object.is_a?(String)
super(level, timestamp, progname, object)
else
super(level, timestamp, progname, object.awesome_inspect)
attr_accessor :level
attr_accessor :progname
def call(severity, timestamp, who, object)
# override progname to be the caller if the log level threshold is DEBUG
# We only do this if the logger level is DEBUG because inspecting the
# stack and doing extra string manipulation can have performance impacts
# under high logging rates.
if @level == Logger::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]
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
end
if object.is_a?(String)
super(severity, timestamp, who, object)
else
super(severity, timestamp, who, object.awesome_inspect)
end
end # def call
end # class LogStash::Logger::Formatter
#a =Logger.new(STDOUT)
#a.formatter = LogStash::Logger::Formatter.new
#a.level = Logger::INFO
#a.info({"hello" => 12345})