Module: Logsy

Defined in:
lib/logsy.rb,
lib/logsy/store.rb,
lib/logsy/railtie.rb,
lib/logsy/version.rb,
lib/logsy/job_hooks.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, JobHooks, SidekiqMiddleware Classes: Configuration, Error, JsonFormatter, RackMiddleware, Railtie, Store

Constant Summary collapse

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


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

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


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

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

.configurationObject



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

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:



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

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.



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

def reset
  Store.reset
end

.reset_configuration!Object

Test helper.



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

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.



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

def tags
  Store.tags
end