lib/eco/api/common/session/file_manager.rb in eco-helpers-3.0.4 vs lib/eco/api/common/session/file_manager.rb in eco-helpers-3.0.5
- old
+ new
@@ -2,10 +2,11 @@
module API
module Common
module Session
class FileManager
include Eco::Data::Files
+ include Eco::Language::AuxiliarLogger
attr_reader :dir, :dir_path
attr_accessor :timestamp_pattern
def initialize(init = {}, enviro: nil)
@@ -18,15 +19,17 @@
def dir_path=(value)
@dir = Eco::Data::Files::Directory.new(value)
@dir_path = @dir.create
rescue StandardError => err
- logger.error("could not create or make any sense of directory '#{value}': #{err}")
+ log(:error) {
+ "could not create or make any sense of directory '#{value}': #{err}"
+ }
end
def logger
- @enviro&.logger || ::Logger.new(IO::NULL)
+ @enviro&.logger || super
end
##### FILE #####
def file(filename, should_exist: false)
dir.file(filename, should_exist: should_exist)
@@ -38,23 +41,27 @@
def file_content(filename, mode: nil)
file = dir.file(filename, should_exist: true)
unless file
- logger.error("Can't read from file '#{filename}' because it does not exist.")
+ log(:error) {
+ "Can't read from file '#{filename}' because it does not exist."
+ }
return nil
end
- logger.debug("Reading from file '#{file}'")
+ log(:debug) { "Reading from file '#{file}'" }
mode ? File.read(file, mode: mode) : File.read(file)
end
def load_json(filename)
file = dir.file(filename, should_exist: true)
unless file
- logger.error("Can't read from file '#{filename}' because it does not exist.")
+ log(:error) {
+ "Can't read from file '#{filename}' because it does not exist."
+ }
return nil
end
fd = File.open(file)
JSON.load fd # rubocop:disable Security/JSONLoad
@@ -75,11 +82,11 @@
file = filename_for(filename, modifier)
FileManager.create_directory(
FileManager.file_fullpath(file)
)
- logger.debug("Writting to file '#{file}'")
+ log(:debug) { "Writting to file '#{file}'" }
mode = mode == :binary ? 'wb' : 'w'
File.open(file, mode) do |fd|
first = true
@@ -101,21 +108,21 @@
file = filename_for(filename, modifier)
FileManager.create_directory(
FileManager.file_fullpath(file)
)
- logger.debug("Writting to file '#{file}'")
+ log(:debug) { "Writting to file '#{file}'" }
mode = mode == :binary ? 'wb' : 'w'
File.open(file, mode) { |fd| fd << content }
file
end
# if the file does not exist, it creates it
def append(content, filename, mode: :string)
file = dir.file(filename)
- logger.debug("Appending to file '#{file}'")
+ log(:debug) { "Appending to file '#{file}'" }
mode = mode == :binary ? 'ab' : 'a'
File.open(file, mode) { |fd| fd << "#{content}\n" }
file
end