Class: GreenLog::Logger

Inherits:
Object
  • Object
show all
Includes:
SeverityThresholdSupport
Defined in:
lib/green_log/logger.rb,
lib/green_log/classic_logger.rb

Overview

:rubocop:disable: Style/Documentation

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from SeverityThresholdSupport

#severity_threshold

Constructor Details

#initialize(downstream) ⇒ Logger

Returns a new instance of Logger.



16
17
18
# File 'lib/green_log/logger.rb', line 16

def initialize(downstream)
  @downstream = downstream
end

Instance Attribute Details

#downstreamObject (readonly)

Returns the value of attribute downstream.



20
21
22
# File 'lib/green_log/logger.rb', line 20

def downstream
  @downstream
end

Class Method Details

.build(dest: $stdout, format: SimpleWriter, severity_threshold: nil) ⇒ Object

Build a Logger.



74
75
76
77
78
79
# 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

.nullObject

Return a null-object Logger.



89
90
91
# File 'lib/green_log/logger.rb', line 89

def null
  new(NullWriter.new)
end

.resolve_format(format) ⇒ Object



81
82
83
84
85
86
# 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

Instance Method Details

#log(severity, *rest, &block) ⇒ Object

Generate a log entry. Arguments may include:

- a message string
- arbitrary data
- an exception


29
30
31
32
33
34
35
36
# 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_loggerObject



74
75
76
# 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) ⇒ Object

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



54
55
56
57
58
59
60
61
# 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_middlewareObject

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



48
49
50
# File 'lib/green_log/logger.rb', line 48

def with_middleware
  self.class.new(yield(downstream))
end

#with_severity_threshold(threshold) ⇒ Object

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



65
66
67
68
69
# File 'lib/green_log/logger.rb', line 65

def with_severity_threshold(threshold)
  with_middleware do |downstream|
    SeverityFilter.new(downstream, threshold: threshold)
  end
end