Method: Sapience::Appender::Stream#initialize

Defined in:
lib/sapience/appender/stream.rb

#initialize(options = {}, &block) ⇒ Stream

Create a Stream Logger appender instance.

Parameters

:file_name [String|IO]
  Name of file to write to.
  Or, an IO stream to which to write the log message to.

:level [:trace | :debug | :info | :warn | :error | :fatal]
  Override the log level for this appender.
  Default: Sapience.config.default_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 'sapience'

# Enable trace level logging
Sapience.config.default_level = :info

# Log to screen
Sapience.add_appender(:stream, io: STDOUT, formatter: :color)

# And log to a file at the same time
Sapience::Logger.add_appender(:stream, file_name: 'application.log', formatter: :color)

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

Example 2. To log all levels to file and only :info and above to screen:

require 'sapience'

# Enable trace level logging
Sapience.config.default_level = :trace

# Log to screen but only display :info and above
Sapience.add_appender(:stream, io: STDOUT, level: :info)

# And log to a file at the same time, including all :trace level data
Sapience.add_appender(:stream, file_name: 'application.log')

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

rubocop:disable AbcSize, CyclomaticComplexity, PerceivedComplexity



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/sapience/appender/stream.rb', line 62

def initialize(options = {}, &block)
  unless options[:io] || options[:file_name]
    fail ArgumentError, "missing mandatory argument :file_name or :io"
  end

  opts = options.dup
  if (io = opts.delete(:io))
    @log = Sapience.constantize(io)
  else
    @file_name = opts.delete(:file_name)
    reopen
  end

  # Set the log level and formatter if supplied
  super(opts, &block)
end