Class: Loggerstash

Inherits:
Object
  • Object
show all
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 Method Summary collapse

Constructor Details

#initialize(logstash_server:, metrics_registry: nil, formatter: nil, logstash_writer: nil, logger: nil) ⇒ Loggerstash

A new Loggerstash!

Parameters:

  • logstash_server (String)

    an address:port, hostname:port, or srvname to which a json_lines logstash connection can be made.

  • metrics_registry (Prometheus::Client::Registry) (defaults to: nil)

    where the metrics which are used by the underlying LogstashWriter should be registered, for later presentation by the Prometheus client.

  • formatter (Proc) (defaults to: nil)

    a formatting proc which takes the same arguments as the standard Logger formatter, but rather than emitting a string, it should pass back a Hash containing all the fields you wish to send to logstash.

  • logstash_writer (LogstashWriter) (defaults to: nil)

    in the event that you've already got a LogstashWriter instance configured, you can pass it in here. Note that any values you've set for logstash_server and metrics_registry will be ignored.

  • logger (Logger) (defaults to: nil)

    passed to the LogstashWriter we create. May or may not, itself, be attached to the Loggerstash for forwarding to logstash (Logception!).



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 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).

Parameters:

  • obj (Object)

    the instance or class to attach this Loggerstash to. We won't check that you're attaching to an object or class that will benefit from the attachment; that's up to you to ensure.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/loggerstash.rb', line 63

def attach(obj)
  run_writer

  @op_mutex.synchronize do
    obj.instance_variable_set(:@logstash_writer, @logstash_writer)
    obj.instance_variable_set(:@loggerstash_formatter, @formatter)

    if obj.is_a?(Module)
      obj.prepend(Mixin)
    else
      obj.singleton_class.prepend(Mixin)
    end
  end
end