Class: FetchUtil::RequestLog

Inherits:
Object
  • Object
show all
Defined in:
lib/fetch_util/request_log.rb

Constant Summary collapse

DEFAULT_PATH =
File.expand_path("~/.local/state/fetch_util/requests.log")

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path: ENV.fetch("FETCH_UTIL_REQUEST_LOG", DEFAULT_PATH)) ⇒ RequestLog

Returns a new instance of RequestLog.



10
11
12
# File 'lib/fetch_util/request_log.rb', line 10

def initialize(path: ENV.fetch("FETCH_UTIL_REQUEST_LOG", DEFAULT_PATH))
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



14
15
16
# File 'lib/fetch_util/request_log.rb', line 14

def path
  @path
end

Instance Method Details

#append(entry, duration: nil) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/fetch_util/request_log.rb', line 16

def append(entry, duration: nil)
  FileUtils.mkdir_p(File.dirname(path))
  line = "#{Time.now.utc.iso8601}\t#{entry}"
  line = "#{line}\t#{format("%.2f", duration)}s" if duration
  File.open(path, "a") { |file| file.puts(line) }
  path
end