Class: Log::Entry
- Inherits:
-
Object
- Object
- Log::Entry
- Defined in:
- lib/log/entry.rb
Constant Summary collapse
- DefaultFormat =
the value used by #to_s if no format is given
"%{time:%FT%T} [%{severity}]: %{text} in %{origin}"
- Severity =
Hash.new{|h,k|k}.merge({ :debug => 1, :info => 2, :warn => 4, :error => 8, :fail => 16, })
- InvSeverity =
Severity.invert
Class Attribute Summary collapse
-
.time_format ⇒ Object
Returns the value of attribute time_format.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#flags ⇒ Object
readonly
Returns the value of attribute flags.
-
#origin ⇒ Object
readonly
Returns the value of attribute origin.
-
#severity ⇒ Object
readonly
Returns the value of attribute severity.
-
#text ⇒ Object
readonly
Returns the value of attribute text.
-
#time ⇒ Object
readonly
Returns the value of attribute time.
Class Method Summary collapse
- .deserialize(line) ⇒ Object
- .format(entity, value, *args) ⇒ Object
- .format_flags(entry, flags) ⇒ Object
- .format_origin(entry) ⇒ Object
- .format_severity(entry) ⇒ Object
- .format_text(entry) ⇒ Object
- .format_time(entry, time, format = nil) ⇒ Object
- .formatter_for(entity, &formatter) ⇒ Object
Instance Method Summary collapse
- #[](key) ⇒ Object
- #debug? ⇒ Boolean
- #error? ⇒ Boolean
- #fail? ⇒ Boolean
- #info? ⇒ Boolean
-
#initialize(text, severity = :info, origin = nil, data = nil, *flags) ⇒ Entry
constructor
A new instance of Entry.
- #inspect ⇒ Object
- #serialize ⇒ Object
- #to_s(format = nil) ⇒ Object
- #warn? ⇒ Boolean
Constructor Details
#initialize(text, severity = :info, origin = nil, data = nil, *flags) ⇒ Entry
Returns a new instance of Entry.
102 103 104 105 106 107 108 109 110 111 |
# File 'lib/log/entry.rb', line 102 def initialize(text, severity=:info, origin=nil, data=nil, *flags) @time = flags.first.kind_of?(Time) ? flags.shift : Time.now @severity = severity @origin = origin.to_s @text = text @data = data @flags = flags.last.kind_of?(Hash) ? flags.pop : {} @flags.each_key { |k,v| @flags[k.to_s] = @flags.delete(k) } flags.each { |flag| @flags[flag.to_s] = true } end |
Class Attribute Details
.time_format ⇒ Object
Returns the value of attribute time_format.
31 32 33 |
# File 'lib/log/entry.rb', line 31 def time_format @time_format end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the value of attribute data.
101 102 103 |
# File 'lib/log/entry.rb', line 101 def data @data end |
#flags ⇒ Object (readonly)
Returns the value of attribute flags.
100 101 102 |
# File 'lib/log/entry.rb', line 100 def flags @flags end |
#origin ⇒ Object (readonly)
Returns the value of attribute origin.
98 99 100 |
# File 'lib/log/entry.rb', line 98 def origin @origin end |
#severity ⇒ Object (readonly)
Returns the value of attribute severity.
97 98 99 |
# File 'lib/log/entry.rb', line 97 def severity @severity end |
#text ⇒ Object (readonly)
Returns the value of attribute text.
99 100 101 |
# File 'lib/log/entry.rb', line 99 def text @text end |
#time ⇒ Object (readonly)
Returns the value of attribute time.
96 97 98 |
# File 'lib/log/entry.rb', line 96 def time @time end |
Class Method Details
.deserialize(line) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/log/entry.rb', line 33 def deserialize(line) time, severity, origin, text, flagstr, data = line.chomp(RecordTerminator).split(RecordSeparator) flags = {} flagstr.split(UnitSeparator).each_cons(2) { |key, value| flagstr[key] = value } severity = Integer(severity) rescue severity new( text, InvSeverity[severity], Log.unescape(origin), Marshal.load(Log.unescape(data)), Time.at(time.to_i), flags ) end |
.format(entity, value, *args) ⇒ Object
54 55 56 |
# File 'lib/log/entry.rb', line 54 def format(entity, value, *args) @formatter[entity].call(value, *args) end |
.format_flags(entry, flags) ⇒ Object
70 71 72 |
# File 'lib/log/entry.rb', line 70 def format_flags(entry, flags) entry.flags.map{ |k,v| "#{k}: #{v}"}.join(", ") end |
.format_origin(entry) ⇒ Object
62 63 64 |
# File 'lib/log/entry.rb', line 62 def format_origin(entry) entry.origin.to_s end |
.format_severity(entry) ⇒ Object
66 67 68 |
# File 'lib/log/entry.rb', line 66 def format_severity(entry) entry.severity.to_s end |
.format_text(entry) ⇒ Object
74 75 76 |
# File 'lib/log/entry.rb', line 74 def format_text(entry) entry.text.chomp.gsub(/[\r\n]+/, '; ').gsub(/[\x00-\x1f\x7f]/, '.') end |
.format_time(entry, time, format = nil) ⇒ Object
58 59 60 |
# File 'lib/log/entry.rb', line 58 def format_time(entry, time, format=nil) entry.time.strftime(format || @time_format) end |
.formatter_for(entity, &formatter) ⇒ Object
50 51 52 |
# File 'lib/log/entry.rb', line 50 def formatter_for(entity, &formatter) @formatter[entity] = formatter end |
Instance Method Details
#[](key) ⇒ Object
113 114 115 |
# File 'lib/log/entry.rb', line 113 def [](key) @flags[key] end |
#debug? ⇒ Boolean
117 118 119 |
# File 'lib/log/entry.rb', line 117 def debug? @severity == Severity[:debug] end |
#error? ⇒ Boolean
129 130 131 |
# File 'lib/log/entry.rb', line 129 def error? @severity == Severity[:error] end |
#fail? ⇒ Boolean
133 134 135 |
# File 'lib/log/entry.rb', line 133 def fail? @severity == Severity[:fail] end |
#info? ⇒ Boolean
121 122 123 |
# File 'lib/log/entry.rb', line 121 def info? @severity == Severity[:info] end |
#inspect ⇒ Object
160 161 162 163 164 165 166 167 168 169 170 |
# File 'lib/log/entry.rb', line 160 def inspect "#<%s %s %s %s %s flags=%s data=%s>" % [ self.class, @time.strftime("%FT%T"), @severity, @origin, @text.inspect, @flags.inspect, @data.inspect ] end |
#serialize ⇒ Object
137 138 139 140 141 142 143 144 145 146 |
# File 'lib/log/entry.rb', line 137 def serialize Serialized % [ @time, Severity[@severity], Log.escape(@origin), Log.escape(@text), @flags.map.join(UnitSeparator), Log.escape(Marshal.dump(@data)) ] end |
#to_s(format = nil) ⇒ Object
148 149 150 151 152 153 154 155 156 157 158 |
# File 'lib/log/entry.rb', line 148 def to_s(format=nil) format ||= DefaultFormat format.gsub(/%(%|\{[^}]+\})/) { |match| if match == "%%" then "%" else entity, *args = match[2..-2].split(/:/) Entry.format(entity, self, *args) end } end |
#warn? ⇒ Boolean
125 126 127 |
# File 'lib/log/entry.rb', line 125 def warn? @severity == Severity[:warn] end |