Skip to content

Commit

Permalink
Better: Rescue ENOENT error writing into /tmp (#16)
Browse files Browse the repository at this point in the history
* Better: Rescue ENOENT error writing into /tmp

* .log

* Add missing require

* Add explanation to README
  • Loading branch information
ylecuyer authored and elhu committed Jun 3, 2019
1 parent 3da2ade commit fc930dc
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,8 @@ If the argument is a path to a non-writeable file, Peastash will attempt to dele
Peastash can easily be extended to output to any target.
Simply configure Peastash's output with an object that responds to the ``#dump`` method. This method will be called at the end of the ``#log`` block, with 1 argument: a ``LogStash::Event`` object that you will probably need to serialize to json.

In case the directory doesn't exist, peastash won't crash, a tempfile will be created in /tmp (or $TMPDIR) and the data will be stored in this file. The full path of this tempfile will be printed to STDERR so it can easily be retrieved.

### What if I want to use it in my rack app?

There's a middleware for that! Simply add the following somewhere:
Expand Down
5 changes: 5 additions & 0 deletions lib/peastash/log_device.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@ def open_logfile(filename)
STDERR.puts "[#{Time.now}][#{Process.pid}] Could not open #{filename} for writing, recreating it. Info: #{stat_data.inspect}"
FileUtils.rm(filename)
create_logfile(filename)
rescue Errno::ENOENT => e
require 'tempfile'
temp_file = Tempfile.new([filename, '.log'])
STDERR.puts "[#{Time.now}][#{Process.pid}] Could not open #{filename} for writing: #{e.message}. Data will be writen in: #{temp_file.path}"
open_logfile(temp_file.path)
end
end
end

0 comments on commit fc930dc

Please sign in to comment.