Class: Radar::Reporter::FileReporter
- Inherits:
-
Object
- Object
- Radar::Reporter::FileReporter
- 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
-
#output_directory(event = nil) ⇒ Object
Returns the currently configured output directory.
-
#prune_time ⇒ Object
Returns the value of attribute prune_time.
Instance Method Summary collapse
-
#initialize(opts = nil) ⇒ FileReporter
constructor
A new instance of FileReporter.
-
#log(message) ⇒ Object
Convenience method for logging.
-
#prune(directory) ⇒ Object
Prunes the files in the given directory according to the age limit set by the #prune_time variable.
- #report(event) ⇒ Object
Constructor Details
#initialize(opts = nil) ⇒ FileReporter
Returns a new instance of FileReporter.
38 39 40 41 42 43 44 |
# File 'lib/radar/reporter/file_reporter.rb', line 38 def initialize(opts=nil) (opts || {}).each do |k,v| send("#{k}=", v) end @output_directory ||= lambda { |event| "~/.radar/errors/#{event.application.name}" } 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.
92 93 94 |
# File 'lib/radar/reporter/file_reporter.rb', line 92 def output_directory @output_directory end |
#prune_time ⇒ Object
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
#log(message) ⇒ Object
Convenience method for logging.
84 85 86 |
# File 'lib/radar/reporter/file_reporter.rb', line 84 def log() @event.application.logger.info("#{self.class}: #{}") if @event end |
#prune(directory) ⇒ Object
Prunes the files in the given directory according to the age limit set by the #prune_time variable.
72 73 74 75 76 77 78 79 80 81 |
# File 'lib/radar/reporter/file_reporter.rb', line 72 def prune(directory) count = Dir[File.join(directory, "*.txt")].inject(0) do |acc, file| next acc unless File.file?(file) next acc unless (Time.now.to_i - File.ctime(file).to_i) >= prune_time.to_i File.delete(file) acc + 1 end log("Pruned #{count} file(s) in #{directory}.") if count > 0 end |
#report(event) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/radar/reporter/file_reporter.rb', line 46 def report(event) @event = event output_file = File.join(File.(output_directory(event)), "#{event.occurred_at.to_i}-#{event.uniqueness_hash}.txt") directory = File.dirname(output_file) begin # Attempt to make the directory if it doesn't exist FileUtils.mkdir_p(directory) if !File.directory?(directory) # Prune files if enabled prune(directory) if prune_time # Write out the JSON to the output file log("#{self.class}: Reported to #{output_file}") File.open(output_file, 'w') { |f| f.write(event.to_json) } rescue Errno::EACCES # Rebrand the exception so its easier to tell what exactly # is going on. raise ReporterError.new("#{self.class}: Failed to create directory or log to: #{output_file}") end end |