module Eco module API module Common module Session class Logger < Eco::Language::BasicLogger TIMESTAMP_PATTERN = '%Y-%m-%dT%H:%M:%S' attr_reader :cache def initialize(file_level: ::Logger::DEBUG, log_file: nil, enviro: nil, **kargs) super(**kargs) raise "Required Environment object (enviro:). Given: #{enviro}" if enviro && !enviro.is_a?(Eco::API::Common::Session::Environment) @enviro = enviro @cache = Logger::Cache.new if log_file = fetch_log_file(log_file) loggers[:file] = ::Logger.new(log_file).tap do |logger| logger.formatter = format_proc(console: false) do |severity, datetime, msg, formatted_msg| cache.add(severity, datetime, msg, formatted_msg) end logger.level = file_level end end if_config(:timestamp_console) { |value| self.timestamp = value } if_config(:console_level) { |value| self.level = value } if_config(:file_level) { |value| self.file_level = value } end def file_level=(value) loggers[:file]&.level = value end private def config(attr) return nil unless cnf = @enviro&.config&.logger cnf.send(attr) if cnf.respond_to?(attr) end def if_config(attr) unless (value = config(attr)).nil? yield(value) if block_given? value end end def fetch_log_file(log_file) if log_file ||= config(:file) log_file = @enviro.file_manager.dir.file(log_file) if @enviro FileManager.create_directory(log_file, includes_file: true) end log_file end end end end end end require_relative "logger/cache" require_relative "logger/log"