Class: Loggerstash
- Inherits:
-
Object
- Object
- Loggerstash
- Defined in:
- lib/loggerstash.rb
Overview
A sidecar class to augment a Logger with super-cow-logstash-forwarding powers.
Defined Under Namespace
Modules: Mixin Classes: AlreadyRunningError, Error
Instance Attribute Summary collapse
-
#formatter ⇒ Object
writeonly
Set the formatter proc to a new proc.
Instance Method Summary collapse
-
#attach(obj) ⇒ Object
Associate this Loggerstash with a Logger (or class of Loggers).
-
#initialize(logstash_server:, metrics_registry: nil, formatter: nil, logstash_writer: nil, logger: nil) ⇒ Loggerstash
constructor
A new Loggerstash!.
-
#log_message(s, t, p, m) ⇒ Object
Send a logger message to logstash.
Constructor Details
#initialize(logstash_server:, metrics_registry: nil, formatter: nil, logstash_writer: nil, logger: nil) ⇒ Loggerstash
A new Loggerstash!
42 43 44 45 46 47 48 49 50 |
# File 'lib/loggerstash.rb', line 42 def initialize(logstash_server:, metrics_registry: nil, formatter: nil, logstash_writer: nil, logger: nil) @logstash_server = logstash_server @metrics_registry = metrics_registry @formatter = formatter @logstash_writer = logstash_writer @logger = logger @op_mutex = Mutex.new end |
Instance Attribute Details
#formatter=(value) ⇒ Object (writeonly)
Set the formatter proc to a new proc.
The passed in proc must take four arguments: severity, timestamp,
progname and message. timestamp is a Time, all over arguments
are Strings, and progname can possibly be nil. It must return a
Hash containing the parameters you wish to send to logstash.
24 25 26 |
# File 'lib/loggerstash.rb', line 24 def formatter=(value) @formatter = value end |
Instance Method Details
#attach(obj) ⇒ Object
Associate this Loggerstash with a Logger (or class of Loggers).
A single Loggerstash instance can be associated with one or more Logger objects, or all instances of Logger, by attaching the Loggerstash to the other object (or class). Attaching a Loggerstash means it can no longer be configured (by the setter methods).
63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/loggerstash.rb', line 63 def attach(obj) @op_mutex.synchronize do obj.instance_variable_set(:@loggerstash, self) if obj.is_a?(Module) obj.prepend(Mixin) else obj.singleton_class.prepend(Mixin) end run_writer end end |
#log_message(s, t, p, m) ⇒ Object
Send a logger message to logstash.
93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/loggerstash.rb', line 93 def (s, t, p, m) @op_mutex.synchronize do if @logstash_writer.nil? #:nocov: run_writer #:nocov: end @logstash_writer.send_event((@formatter || default_formatter).call(s, t, p, m)) end end |