Class: Appear::Output

Inherits:
Object
  • Object
show all
Defined in:
lib/appear/output.rb

Overview

The Output service encapsulates writing logging information to log files and STDERR, and writing output to STDOUT.

Instance Method Summary collapse

Constructor Details

#initialize(log_file_name, silent) ⇒ Output

Create a new Output service.

Parameters:

  • log_file_name (String, nil)

    if a string, log to the file at this path

  • silent (Boolean)

    if true, output to STDERR



12
13
14
15
16
17
18
# File 'lib/appear/output.rb', line 12

def initialize(log_file_name, silent)
  @file_logger = nil
  @stderr_logger = nil

  @file_logger = Logger.new(log_file_name.to_s) if log_file_name
  @stderr_logger = Logger.new(STDERR) unless silent
end

Instance Method Details

#log(*any) ⇒ Object

Log a message.

Parameters:

  • any (Array<Any>)


23
24
25
26
# File 'lib/appear/output.rb', line 23

def log(*any)
  @stderr_logger.debug(*any) if @stderr_logger
  @file_logger.debug(*any) if @file_logger
end

#log_error(err) ⇒ Object

Log an error

Parameters:



31
32
33
34
35
36
# File 'lib/appear/output.rb', line 31

def log_error(err)
  log("Error #{err.inspect}: #{err.to_s.inspect}")
  if err.backtrace
    err.backtrace.each { |line| log("  " + line) }
  end
end

#output(*any) ⇒ Object

Output a message to STDOUT, and also to the log file.

Parameters:

  • any (Array<Any>)


41
42
43
44
# File 'lib/appear/output.rb', line 41

def output(*any)
  STDOUT.puts(*any)
  @file_logger.debug(*any) if @file_logger
end