Class: Log::Entry

Inherits:
Object
  • Object
show all
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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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_formatObject

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

#dataObject (readonly)

Returns the value of attribute data.



101
102
103
# File 'lib/log/entry.rb', line 101

def data
  @data
end

#flagsObject (readonly)

Returns the value of attribute flags.



100
101
102
# File 'lib/log/entry.rb', line 100

def flags
  @flags
end

#originObject (readonly)

Returns the value of attribute origin.



98
99
100
# File 'lib/log/entry.rb', line 98

def origin
  @origin
end

#severityObject (readonly)

Returns the value of attribute severity.



97
98
99
# File 'lib/log/entry.rb', line 97

def severity
  @severity
end

#textObject (readonly)

Returns the value of attribute text.



99
100
101
# File 'lib/log/entry.rb', line 99

def text
  @text
end

#timeObject (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

Returns:

  • (Boolean)


117
118
119
# File 'lib/log/entry.rb', line 117

def debug?
	@severity == Severity[:debug]
end

#error?Boolean

Returns:

  • (Boolean)


129
130
131
# File 'lib/log/entry.rb', line 129

def error?
	@severity == Severity[:error]
end

#fail?Boolean

Returns:

  • (Boolean)


133
134
135
# File 'lib/log/entry.rb', line 133

def fail?
	@severity == Severity[:fail]
end

#info?Boolean

Returns:

  • (Boolean)


121
122
123
# File 'lib/log/entry.rb', line 121

def info?
	@severity == Severity[:info]
end

#inspectObject



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

#serializeObject



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

Returns:

  • (Boolean)


125
126
127
# File 'lib/log/entry.rb', line 125

def warn?
	@severity == Severity[:warn]
end