Module: Pgbus::LogFormatter

Defined in:
lib/pgbus/log_formatter.rb

Overview

Log formatters for Pgbus, inspired by Sidekiq::Logger::Formatters.

Usage:

Pgbus.configure do |c|
  c.logger.formatter = Pgbus::LogFormatter::JSON.new
end

Or via the convenience config option:

Pgbus.configure do |c|
  c.log_format = :json
end

Defined Under Namespace

Classes: JSON, Text

Class Method Summary collapse

Class Method Details

.current_contextObject



37
38
39
# File 'lib/pgbus/log_formatter.rb', line 37

def current_context
  Thread.current[:pgbus_log_context] ||= {}
end

.tidObject



22
23
24
# File 'lib/pgbus/log_formatter.rb', line 22

def tid
  Thread.current[:pgbus_tid] ||= (Thread.current.object_id ^ ::Process.pid).to_s(36)
end

.with_context(hash) ⇒ Object

Thread-local context for structured logging. Works like Sidekiq::Context — any key/value pairs set via with_context appear in the JSON output under the “ctx” key.



29
30
31
32
33
34
35
# File 'lib/pgbus/log_formatter.rb', line 29

def with_context(hash)
  orig = current_context.dup
  current_context.merge!(hash)
  yield
ensure
  Thread.current[:pgbus_log_context] = orig
end