Class: GreenLog::Logger
- Inherits:
-
Object
- Object
- GreenLog::Logger
- Includes:
- SeverityThresholdSupport
- Defined in:
- lib/green_log/logger.rb,
lib/green_log/classic_logger.rb
Overview
:rubocop:disable: Style/Documentation
Instance Attribute Summary collapse
-
#downstream ⇒ Object
readonly
Returns the value of attribute downstream.
Class Method Summary collapse
-
.build(dest: $stdout, format: SimpleWriter, severity_threshold: nil) ⇒ Object
Build a Logger.
- .resolve_format(format) ⇒ Object
Instance Method Summary collapse
-
#initialize(downstream) ⇒ Logger
constructor
A new instance of Logger.
-
#log(severity, *rest, &block) ⇒ Object
Generate a log entry.
- #to_classic_logger ⇒ Object
-
#with_context(static_context = nil, &context_generator) ⇒ Object
Add a middleware that adds context.
-
#with_middleware ⇒ Object
Add a middleware in front of the downstream handler.
-
#with_severity_threshold(threshold) ⇒ Object
Add a middleware that filters by severity.
Methods included from SeverityThresholdSupport
Constructor Details
#initialize(downstream) ⇒ Logger
Returns a new instance of Logger.
15 16 17 |
# File 'lib/green_log/logger.rb', line 15 def initialize(downstream) @downstream = downstream end |
Instance Attribute Details
#downstream ⇒ Object (readonly)
Returns the value of attribute downstream.
19 20 21 |
# File 'lib/green_log/logger.rb', line 19 def downstream @downstream end |
Class Method Details
.build(dest: $stdout, format: SimpleWriter, severity_threshold: nil) ⇒ Object
Build a Logger.
73 74 75 76 77 78 |
# File 'lib/green_log/logger.rb', line 73 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 |
.resolve_format(format) ⇒ Object
80 81 82 83 84 85 |
# File 'lib/green_log/logger.rb', line 80 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
28 29 30 31 32 33 34 35 |
# File 'lib/green_log/logger.rb', line 28 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 ⇒ Object
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.
53 54 55 56 57 58 59 60 |
# File 'lib/green_log/logger.rb', line 53 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 ⇒ Object
Add a middleware in front of the downstream handler. Return a new Logger with the expanded handler-stack.
47 48 49 |
# File 'lib/green_log/logger.rb', line 47 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.
64 65 66 67 68 |
# File 'lib/green_log/logger.rb', line 64 def with_severity_threshold(threshold) with_middleware do |downstream| SeverityFilter.new(downstream, threshold: threshold) end end |