class GreenLog::Logger

:rubocop: disable: Style/Documentation

Log entry generator.

Attributes

downstream[R]

Public Class Methods

build(dest: $stdout, format: SimpleWriter, severity_threshold: nil) click to toggle source

Build a Logger.

# File lib/green_log/logger.rb, line 74
def build(dest: $stdout, format: SimpleWriter, severity_threshold: nil)
  format = resolve_format(format)
  downstream = format.new(dest)
  downstream = SeverityFilter.new(downstream, threshold: severity_threshold) if severity_threshold
  new(downstream)
end
new(downstream) click to toggle source
# File lib/green_log/logger.rb, line 16
def initialize(downstream)
  @downstream = downstream
end
null() click to toggle source

Return a null-object Logger.

# File lib/green_log/logger.rb, line 89
def null
  new(NullWriter.new)
end
resolve_format(format) click to toggle source
# File lib/green_log/logger.rb, line 81
def resolve_format(format)
  return format if format.is_a?(Class)

  format = format.to_s if format.is_a?(Symbol)
  GreenLog.const_get("#{format.capitalize}Writer")
end

Public Instance Methods

log(severity, *rest, &block) click to toggle source

Generate a log entry. Arguments may include:

- a message string
- arbitrary data
- an exception
# File lib/green_log/logger.rb, line 29
def log(severity, *rest, &block)
  severity = Severity.resolve(severity)
  return false if severity < severity_threshold

  entry = Entry.build(severity, *rest, &block)
  downstream << entry
  true
end
to_classic_logger() click to toggle source
# File lib/green_log/classic_logger.rb, line 74
def to_classic_logger
  GreenLog::ClassicLogger.new(downstream)
end
with_context(static_context = nil, &context_generator) click to toggle source

Add a middleware that adds context. Return a new Logger with the expanded handler-stack.

# File lib/green_log/logger.rb, line 54
def with_context(static_context = nil, &context_generator)
  with_middleware do |current_downstream|
    downstream = current_downstream
    downstream = Contextualizer.new(downstream) { static_context } unless static_context.nil?
    downstream = Contextualizer.new(downstream, &context_generator) unless context_generator.nil?
    downstream
  end
end
with_middleware() { |downstream| ... } click to toggle source

Add a middleware in front of the downstream handler. Return a new Logger with the expanded handler-stack.

# File lib/green_log/logger.rb, line 48
def with_middleware
  self.class.new(yield(downstream))
end
with_severity_threshold(threshold) click to toggle source

Add a middleware that filters by severity. Return a new Logger with the expanded handler-stack.

# File lib/green_log/logger.rb, line 65
def with_severity_threshold(threshold)
  with_middleware do |downstream|
    SeverityFilter.new(downstream, threshold: threshold)
  end
end