Class: TinyCI::MultiLogger

Inherits:
Object
  • Object
show all
Defined in:
lib/tinyci/multi_logger.rb

Overview

This class allows logging to both STDOUT and to a file with a single call.

Constant Summary collapse

FORMAT =
Proc.new do |severity, datetime, progname, msg|
  "[#{datetime.strftime "%T"}] #{msg}\n"
end

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(quiet: false, path: nil) ⇒ MultiLogger

Constructor

Parameters:

  • quiet (Boolean) (defaults to: false)

    Disables logging to STDOUT

  • path (String) (defaults to: nil)

    Location to write logfile to



17
18
19
20
21
22
23
24
25
# File 'lib/tinyci/multi_logger.rb', line 17

def initialize(quiet: false, path: nil)
  @file_logger = nil
  self.output_path = path
  @quiet = quiet
  
  @stdout_logger = Logger.new($stdout)
  @stdout_logger.formatter = FORMAT
  @stdout_logger.level = Logger::INFO
end

Instance Attribute Details

#quietBoolean

Disables logging to STDOUT

Returns:

  • (Boolean)

    the current value of quiet



6
7
8
# File 'lib/tinyci/multi_logger.rb', line 6

def quiet
  @quiet
end

Instance Method Details

#output_path=(path) ⇒ Object



35
36
37
38
39
40
41
# File 'lib/tinyci/multi_logger.rb', line 35

def output_path=(path)
  if path
    @file_logger = Logger.new(path)
    @file_logger.formatter = FORMAT
    @file_logger.level = Logger::INFO
  end
end

#targetsObject



27
28
29
30
31
32
33
# File 'lib/tinyci/multi_logger.rb', line 27

def targets
  logs = []
  logs << @file_logger if @file_logger
  logs << @stdout_logger unless @quiet
  
  logs
end