Module: Logsy

Defined in:
lib/logsy.rb,
lib/logsy/store.rb,
lib/logsy/version.rb,
lib/logsy/configuration.rb,
lib/logsy/json_formatter.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, Store

Constant Summary collapse

VERSION =
'0.1.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


16
17
18
# File 'lib/logsy.rb', line 16

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


28
29
30
# File 'lib/logsy.rb', line 28

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

.configurationObject



55
56
57
# File 'lib/logsy.rb', line 55

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:



50
51
52
53
# File 'lib/logsy.rb', line 50

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.



41
42
43
# File 'lib/logsy.rb', line 41

def reset
  Store.reset
end

.reset_configuration!Object

Test helper.



60
61
62
# File 'lib/logsy.rb', line 60

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.



34
35
36
# File 'lib/logsy.rb', line 34

def tags
  Store.tags
end