Module: Slk::Support::ErrorLogger

Defined in:
lib/slk/support/error_logger.rb

Overview

Logs errors to a file for debugging

Class Method Summary collapse

Class Method Details

.log(error, paths: XdgPaths.new) ⇒ String?

Log an error to the error log file

Parameters:

  • error (Exception)

    The error to log

  • paths (XdgPaths) (defaults to: XdgPaths.new)

    Path helper (for testing)

Returns:

  • (String, nil)

    Path to the log file, or nil if logging failed



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/slk/support/error_logger.rb', line 11

def self.log(error, paths: XdgPaths.new)
  paths.ensure_cache_dir

  log_file = paths.cache_file('error.log')
  File.open(log_file, 'a') do |f|
    f.puts "#{Time.now.iso8601} - #{error.class}: #{error.message}"
    f.puts error.backtrace.first(10).map { |line| "  #{line}" }.join("\n") if error.backtrace
    f.puts
  end

  log_file
rescue SystemCallError, IOError
  # If we can't write to the log, fail silently rather than crashing
  # The user will still see the error message in the console
  nil
end