Class: FileLogger

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

Overview

A logger class that outputs the file.

Constant Summary collapse

@@log_path_default =
'debugtrace.log'

Instance Method Summary collapse

Constructor Details

#initialize(config) ⇒ FileLogger

Returns a new instance of FileLogger.



101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/debugtrace/loggers.rb', line 101

def initialize(config)
  @log_path = @@log_path_default
  @config = Common::check_type("config", config, Config)
  Common::check_type("log_path", config.log_path, String)
  @log_path = config.log_path
  @append = false

  if @log_path.start_with?('+')
    @log_path = @log_path[1..-1]
    @append = true
  end

  dir_path = File.dirname(@log_path)

  if !Dir.exist?(dir_path)
    @log_path = @@log_path_default
    @append = true
    print("DebugTrace-rb: FileLogger: The directory '#{dir_path}' cannot be found.\n")
  end

  if !@append
    File.open(@log_path, 'w') { |file|
    }
  end
end

Instance Method Details



127
128
129
130
131
132
133
134
135
# File 'lib/debugtrace/loggers.rb', line 127

def print(message)
# Common::check_type("message", message, String)
  if File.exist?(@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_sString

Returns a string representation of this object.

Returns:

  • (String)

    A string representation of this object



139
140
141
# File 'lib/debugtrace/loggers.rb', line 139

def to_s
  "#{self.class.name} path: #{@log_path}, append: #{@append}"
end