module Loggerator::Log
Don't expose internals into included modules so name-collisions are reduced
Public Instance Methods
contexts(data)
click to toggle source
# File lib/loggerator/log.rb, line 30 def contexts(data) Loggerator.config.default_context.merge(request_context.merge(local_context.merge(data))) end
local_context()
click to toggle source
# File lib/loggerator/log.rb, line 6 def local_context RequestStore.store[:local_context] ||= {} end
local_context=(h)
click to toggle source
# File lib/loggerator/log.rb, line 10 def local_context=(h) RequestStore.store[:local_context] = h end
stderr()
click to toggle source
# File lib/loggerator/log.rb, line 26 def stderr Loggerator.config.stderr end
stderr=(stream)
click to toggle source
# File lib/loggerator/log.rb, line 22 def stderr=(stream) Loggerator.config.stderr = stream end
stdout()
click to toggle source
# File lib/loggerator/log.rb, line 18 def stdout Loggerator.config.stdout end
stdout=(stream)
click to toggle source
# File lib/loggerator/log.rb, line 14 def stdout=(stream) Loggerator.config.stdout = stream end
to_stream(stream, data) { || ... }
click to toggle source
# File lib/loggerator/log.rb, line 34 def to_stream(stream, data, &block) unless block str = unparse(data) stream.print(str + "\n") else data = data.dup start = Time.now to_stream(stream, data.merge(at: 'start')) begin res = yield to_stream(stream, data.merge( at: 'finish', elapsed: (Time.now - start).to_f)) res rescue to_stream(stream, data.merge( at: 'exception', elapsed: (Time.now - start).to_f)) raise $! end end end
Private Instance Methods
quote_string(k, v)
click to toggle source
# File lib/loggerator/log.rb, line 83 def quote_string(k, v) # try to find a quote style that fits if !v.include?('"') %{#{k}="#{v}"} elsif !v.include?("'") %{#{k}='#{v}'} else %{#{k}="#{v.gsub(/"/, '\\"')}"} end end
request_context()
click to toggle source
# File lib/loggerator/log.rb, line 57 def request_context RequestStore.store[:request_context] || {} end
unparse(attrs)
click to toggle source
# File lib/loggerator/log.rb, line 61 def unparse(attrs) attrs.map { |k, v| unparse_pair(k, v) }.compact.join(" ") end
unparse_pair(k, v)
click to toggle source
# File lib/loggerator/log.rb, line 65 def unparse_pair(k, v) v = v.call if v.is_a?(Proc) # only quote strings if they include whitespace if v == nil nil elsif v == true k elsif v.is_a?(Float) "#{k}=#{format("%.3f", v)}" elsif v.is_a?(String) && v =~ /\s/ quote_string(k, v) elsif v.is_a?(Time) "#{k}=#{v.iso8601}" else "#{k}=#{v}" end end