Class: FileLogger

Inherits:
LoggerBase show all
Defined in:
lib/debugtrace/loggers.rb

Overview

A logger class that outputs the file.

Instance Method Summary collapse

Constructor Details

#initialize(config, log_path) ⇒ FileLogger

Returns a new instance of FileLogger.



104
105
106
107
108
109
110
111
112
113
114
# File 'lib/debugtrace/loggers.rb', line 104

def initialize(config, log_path)
  @config = Common::check_type("config", config, Config)
  Common::check_type("log_path", log_path, String)
  dir_path = File.dirname(log_path)
  if Dir.exist?(dir_path)
    @log_path = log_path
  else
    $stderr.puts "DebugTrace-rb: FileLogger: The directory '#{dir_path}' cannot be found."
    @log_path = ''
  end
end

Instance Method Details



116
117
118
119
120
121
122
123
124
# File 'lib/debugtrace/loggers.rb', line 116

def print(message)
  Common::check_type("message", message, String)
  if @log_path != ''
    File.open(@log_path, 'a') {|file|
      datetime_str = Time.now().strftime(@config.logging_datetime_format)
      file.puts "#{datetime_str} #{message}"
    }
  end
end

#to_sObject

Returns a string representation of this object.

Returns:

  • String: A string representation of this object



128
129
130
# File 'lib/debugtrace/loggers.rb', line 128

def to_s
  "File logger: '#{@log_path}"
end