Module: Logsy

Defined in:
lib/logsy.rb,
lib/logsy/store.rb,
lib/logsy/railtie.rb,
lib/logsy/version.rb,
lib/logsy/configuration.rb,
lib/logsy/json_formatter.rb,
lib/logsy/rack_middleware.rb,
lib/logsy/controller_hooks.rb,
lib/logsy/sidekiq_middleware.rb,
lib/logsy/sidekiq_middleware/client.rb,
lib/logsy/sidekiq_middleware/server.rb

Defined Under Namespace

Modules: ControllerHooks, SidekiqMiddleware Classes: Configuration, Error, JsonFormatter, RackMiddleware, Railtie, Store

Constant Summary collapse

VERSION =
'0.2.0'

Class Method Summary collapse

Class Method Details

.[](key) ⇒ Object

Read a tag from the per-request store.

Logsy[:user_id]   # => 'u-1' or nil


18
19
20
# File 'lib/logsy.rb', line 18

def [](key)
  Store.tags[key.to_sym]
end

.[]=(key, value) ⇒ Object

Write a tag into the per-request store. From this point on, every log line emitted during the current request (or job) carries it. Non-nil values are coerced to String so log fields have a consistent type when ingested by structured log stores (which often infer field type from the first value they see).

Logsy[:user_id] = user.id   # 42        -> "42"
Logsy[:order_id] = nil      # nil stays nil


30
31
32
# File 'lib/logsy.rb', line 30

def []=(key, value)
  Store.tags[key.to_sym] = value&.to_s
end

.configurationObject



57
58
59
# File 'lib/logsy.rb', line 57

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Configure the gem. Yields the Configuration instance.

Logsy.configure do |c|
  c.job_propagated_keys = %i[request_id user_id]
end

Yields:



52
53
54
55
# File 'lib/logsy.rb', line 52

def configure
  yield(configuration)
  configuration
end

.resetObject

Clear all tags. The Rails executor calls this between requests, and the Sidekiq middleware calls it between jobs. You usually don’t need to call it yourself.



43
44
45
# File 'lib/logsy.rb', line 43

def reset
  Store.reset
end

.reset_configuration!Object

Test helper.



62
63
64
# File 'lib/logsy.rb', line 62

def reset_configuration!
  @configuration = nil
end

.tagsObject

All tags currently set, as a hash. Used by the formatter on every log line; consumers can call it for debugging.



36
37
38
# File 'lib/logsy.rb', line 36

def tags
  Store.tags
end