/usr/lib/ruby/vendor_ruby/log4r/formatter/formatter.rb is in ruby-log4r 1.1.10-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 | # :include: ../rdoc/formatter
#
# Version:: $Id$
require "singleton"
require "log4r/base"
module Log4r
# Formatter is an abstract class and a null object
class Formatter
def initialize(hash={})
end
# Define this method in a subclass to format data.
def format(logevent)
end
end
# SimpleFormatter produces output like this:
#
# WARN loggername> Danger, Will Robinson, danger!
#
# Does not write traces and does not inspect objects.
class SimpleFormatter < Formatter
def format(event)
sprintf("%*s %s> %s\n", MaxLevelLength, LNAMES[event.level],
event.name, event.data)
end
end
# BasicFormatter produces output like this:
#
# WARN loggername: I dropped my Wookie!
#
# Or like this if trace is on:
#
# WARN loggername(file.rb at 12): Hot potato!
#
# Also, it will pretty-print any Exception it gets and
# +inspect+ everything else.
#
# Hash arguments include:
#
# +depth+:: How many lines of the stacktrace to display.
class BasicFormatter < SimpleFormatter
@@basicformat = "%*s %s"
def initialize(hash={})
@depth = (hash[:depth] or hash['depth'] or 7).to_i
end
def format(event)
buff = sprintf(@@basicformat, MaxLevelLength, LNAMES[event.level],
event.name)
buff << (event.tracer.nil? ? "" : "(#{event.tracer[0]})") + ": "
buff << format_object(event.data) + "\n"
buff
end
# Formats data according to its class:
#
# String:: Prints it out as normal.
# Exception:: Produces output similar to command-line exceptions.
# Object:: Prints the type of object, then the output of
# +inspect+. An example -- Array: [1, 2, 3]
def format_object(obj)
if obj.kind_of? Exception
return "Caught #{obj.class}: #{obj.message}\n\t" +\
(obj.backtrace.nil? ? [] : obj.backtrace[0...@depth]).join("\n\t")
elsif obj.kind_of? String
return obj
else # inspect the object
return "#{obj.class}: #{obj.inspect}"
end
end
end
# Formats objects the same way irb does:
#
# loggername:foo.rb in 12>
# [1, 3, 4]
# loggername:foo.rb in 13>
# {1=>"1"}
#
# Strings don't get inspected. just printed. The trace is optional.
class ObjectFormatter < Formatter
def format(event)
buff = event.logger.name
buff << (event.tracer.nil? ? "" : ":#{event.tracer[0]}") + ">\n"
buff << (event.data.kind_of?(String) ? event.data : event.data.inspect)
buff << "\n"
end
end
# Outputters that don't define a Formatter will get this, which
# is currently BasicFormatter
class DefaultFormatter < BasicFormatter
end
end
|