Module: Traceable
- Defined in:
- lib/traceable.rb,
lib/traceable/args.rb,
lib/traceable/config.rb,
lib/traceable/tracer.rb,
lib/traceable/version.rb,
lib/traceable/class_methods.rb
Defined Under Namespace
Modules: ClassMethods Classes: Args, Config, Tracer
Constant Summary collapse
- VERSION =
'1.5.0'
Class Method Summary collapse
- .config ⇒ Object
- .configure {|config| ... } ⇒ Object
- .included(base) ⇒ Object
- .update_args_config ⇒ Object
Instance Method Summary collapse
-
#init_tracer(parent: nil, tags: nil) ⇒ Object
Create the tracer instance used for generating log messages.
- #local_tracer ⇒ Object
-
#trace(msg = nil, **tags) ⇒ Object
Generate a log message When called without a block, generates a single log message:.
Class Method Details
.config ⇒ Object
31 32 33 |
# File 'lib/traceable/config.rb', line 31 def self.config @config ||= Config.new end |
.configure {|config| ... } ⇒ Object
25 26 27 28 29 |
# File 'lib/traceable/config.rb', line 25 def self.configure(&_) yield config update_args_config config end |
.included(base) ⇒ Object
4 5 6 |
# File 'lib/traceable/class_methods.rb', line 4 def self.included(base) base.extend(ClassMethods) end |
.update_args_config ⇒ Object
35 36 37 38 39 40 41 |
# File 'lib/traceable/config.rb', line 35 def self.update_args_config Args.set_config_limits( max_string_length: config.max_string_length, max_array_values: config.max_array_values, max_hash_keys: config.max_hash_keys ) end |
Instance Method Details
#init_tracer(parent: nil, tags: nil) ⇒ Object
Create the tracer instance used for generating log messages. If a parent is givent, tags and other settings will be inherited from it. If a parent is not given, it will automatically inherit from the next highest tracer in the call stack, if any.
The default set of tags includes a unique ID string, so all log messages generated from that tracer will have the same ID string. In combination with auto-inheriting from a parent tracer, this means that all tracing messages starting from some common root will have the same ID string to be able to group together related messages in the log.
51 52 53 54 |
# File 'lib/traceable.rb', line 51 def init_tracer(parent: nil, tags: nil) parent ||= Tracer.default_parent @tracer = Tracer.new(parent, tags: ) end |
#local_tracer ⇒ Object
36 37 38 |
# File 'lib/traceable.rb', line 36 def local_tracer @tracer ||= init_tracer end |
#trace(msg = nil, **tags) ⇒ Object
Generate a log message When called without a block, generates a single log message:
trace "this is a single message"
trace.error "something bad happened"
When called with a block, the given message is used to compose a log output at the entry and exit of the block.
trace "doing something nifty" do
do_something
end
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/traceable.rb', line 24 def trace(msg = nil, **) tracer = local_tracer if block_given? tracer.do_block(msg, **) { || yield } elsif msg tracer.info msg, ** else tracer end end |