Class: Semlogger::Formatter

Inherits:
Logger::Formatter
  • Object
show all
Defined in:
lib/semlogger/formatter.rb

Instance Method Summary collapse

Constructor Details

#initialize(tags, data) ⇒ Formatter

Returns a new instance of Formatter.



2
3
4
# File 'lib/semlogger/formatter.rb', line 2

def initialize tags, data
	@tags, @data = tags, data
end

Instance Method Details

#call(severity, time, progname, msg) ⇒ Object



35
36
37
# File 'lib/semlogger/formatter.rb', line 35

def call severity, time, progname, msg
	[severity, time.xmlschema(9), progname, msg, tags, data].to_json + "\n"
end

#data2ser(data) ⇒ Object



18
19
20
21
# File 'lib/semlogger/formatter.rb', line 18

def data2ser data
	data = data ? data.dup : {}
	data.each {|k,v| data[k] = obj2ser v }
end

#entry(severity, time, progname, msg, tags, data) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
# File 'lib/semlogger/formatter.rb', line 23

def entry severity, time, progname, msg, tags, data
	e = { 
		lvl: severity,
		ts: time,
	}
	e[:prog] = progname  if progname
	e[:tags] = tags  if tags and not tags.empty?
	e[:data] = data  if data and not data.empty?
	e[:msg]  = msg
	e
end

#obj2ser(obj) ⇒ Object



6
7
8
9
10
11
# File 'lib/semlogger/formatter.rb', line 6

def obj2ser obj
	case obj
	when Proc then obj.call
	else obj
	end
end

#tags2list(tags) ⇒ Object



13
14
15
16
# File 'lib/semlogger/formatter.rb', line 13

def tags2list tags
	tags = tags ? tags.dup : []
	tags.map &method( :obj2ser)
end