Class: SemanticLogger::Appender::IO

Inherits:
Subscriber
  • Object
show all
Defined in:
lib/semantic_logger/appender/io.rb

Constant Summary

Constants inherited from Subscriber

Subscriber::MESSAGE_ERRORS

Instance Attribute Summary

Attributes inherited from Subscriber

#application, #environment, #formatter, #host, #logger, #metrics

Instance Method Summary collapse

Methods inherited from Subscriber

#batch_all_or_nothing?, #batch_by_default?, #batch_refusal, #close, #console_output?, #default_formatter, #fails_any_batch?, #inspect, #inspect_details, #message_error?, #not_sent?, #rejected_entry, #should_log?, #wrote_nothing?

Constructor Details

#initialize(io, **args) ⇒ IO

Create a Stream Logger appender instance.

Parameters io [IO] An IO stream to which to write the log messages to.

:level [:trace | :debug | :info | :warn | :error | :fatal] Override the log level for this appender. Default: none (:trace), so it writes every entry that a logger emits; each logger filters by its own level.

:formatter: [Object|Proc] An instance of a class that implements #call, or a Proc to be used to format the output from this appender Default: Use the built-in formatter (See: #call)

:filter [Regexp|Proc] RegExp: Only include log messages where the class name matches the supplied regular expression. All other messages will be ignored. Proc: Only include log messages where the supplied Proc returns true The Proc must return true or false.

Example require "semantic_logger"

# Log :info and above
SemanticLogger.default_level = :info

# Log to screen
SemanticLogger.add_appender(io: $stdout, formatter: :color)

logger = SemanticLogger['test']
logger.info 'Hello World'


45
46
47
48
49
50
51
52
# File 'lib/semantic_logger/appender/io.rb', line 45

def initialize(io, **args, &)
  @io = io
  unless @io.respond_to?(:write)
    raise(ArgumentError, "SemanticLogging::Appender::IO io is not a valid IO instance: #{io.inspect}")
  end

  super(**args, &)
end

Instance Method Details

#console_stream ⇒ Object



68
69
70
71
72
73
74
# File 'lib/semantic_logger/appender/io.rb', line 68

def console_stream
  if @io.equal?($stdout)
    :stdout
  elsif @io.equal?($stderr)
    :stderr
  end
end

#flush ⇒ Object

Flush all pending logs to disk. Waits for all sent documents to be written to disk



64
65
66
# File 'lib/semantic_logger/appender/io.rb', line 64

def flush
  @io.flush if @io.respond_to?(:flush)
end

#log(log) ⇒ Object



54
55
56
57
58
59
60
# File 'lib/semantic_logger/appender/io.rb', line 54

def log(log)
  # Since only one appender thread writes to the IO at a time, it is not necessary to protect
  # access to it. The line is made as part of the formatting, so that a formatter's output that
  # cannot be one, such as nil, is taken for the entry's error.
  TextOutput.write(@io, format_log(log) { |text| TextOutput.line(text) })
  true
end