Class: Radar::Reporter::FileReporter

Inherits:
Object
  • Object
show all
Defined in:
lib/radar/reporter/file_reporter.rb

Overview

Reports exceptions by dumping the JSON data out to a file on the local filesystem. The reporter is configurable:

Configurable Values

output_directory

Specifies the directory where the outputted files are stored. This value can be either a string or a lambda which takes an ExceptionEvent as its only parameter. The reporter will automatically attempt to make the configured directory if it doesn't already exist. Examples of both methods of specifying the directory are shown below:

reporter.output_directory = "~/hard/coded/path"

Or:

reporter.output_directory = lambda { |event| "~/.radar/errors/#{event.application.name}" }

prune_time

Specifies the maximum age (in seconds) that a previously outputted file is allowed to reach before being pruned. When an exception is raised, the FileReporter will automatically prune existing files which are older than the specified amount. By default this is nil (no pruning occurs).

# One week:
reporter.prune_time = 60 * 60 * 24 * 7

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeFileReporter

Returns a new instance of FileReporter.



38
39
40
41
# File 'lib/radar/reporter/file_reporter.rb', line 38

def initialize
  @output_directory = lambda { |event| "~/.radar/errors/#{event.application.name}" }
  @prune_time = nil
end

Instance Attribute Details

#output_directory(event = nil) ⇒ Object

Returns the currently configured output directory. If event is given as a parameter and the currently set directory is a lambda, then the lambda will be evaluated then returned. If no event is given, the lambda is returned as-is.



73
74
75
# File 'lib/radar/reporter/file_reporter.rb', line 73

def output_directory
  @output_directory
end

#prune_timeObject

Returns the value of attribute prune_time.



36
37
38
# File 'lib/radar/reporter/file_reporter.rb', line 36

def prune_time
  @prune_time
end

Instance Method Details

#prune(directory) ⇒ Object

Prunes the files in the given directory according to the age limit set by the #prune_time variable.

Parameters:

  • directory (String)

    Directory to prune



61
62
63
64
65
66
67
# File 'lib/radar/reporter/file_reporter.rb', line 61

def prune(directory)
  Dir[File.join(directory, "*.txt")].each do |file|
    next unless File.file?(file)
    next unless (Time.now.to_i - File.ctime(file).to_i) >= prune_time.to_i
    File.delete(file)
  end
end

#report(event) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/radar/reporter/file_reporter.rb', line 43

def report(event)
  output_file = File.join(File.expand_path(output_directory(event)), "#{event.occurred_at.to_i}-#{event.uniqueness_hash}.txt")
  directory = File.dirname(output_file)

  # Attempt to make the directory if it doesn't exist
  FileUtils.mkdir_p directory

  # Prune files if enabled
  prune(directory) if prune_time

  # Write out the JSON to the output file
  File.open(output_file, 'w') { |f| f.write(event.to_json) }
end