# frozen_string_literal: true module Cryptum # This plugin is used to instantiate a Cryptum logger with a custom message format module Log # Supported Method Parameters:: # Cryptum::Log.create( # ) public_class_method def self.append(opts = {}) level = opts[:level].to_s.downcase.to_sym msg = opts[:msg] which_self = opts[:which_self].to_s event_history = opts[:event_history] driver_name = File.basename($PROGRAM_NAME) # Only attempt to exit gracefully if level == :error exit_gracefully = false if event_history.respond_to?('order_book') session = event_history.order_book[:path].split('/').first symbol = event_history.order_book[:symbol] end # Define Date / Time Format datetime_str = '%Y-%m-%d %H:%M:%S.%N%z' # Always append to log file log_file_path = '/tmp/cryptum.log' log_file_path = "/tmp/cryptum-ai-#{session}-#{symbol}.json" if level == :learning log_file = File.open(log_file_path, 'a') # Leave 10 "old" log files where # each file is ~ 1,024,000 bytes logger = Logger.new( log_file, 10, 1_024_000 ) logger.datetime_format = datetime_str case level when :debug logger.level = Logger::DEBUG when :error logger.level = Logger::ERROR exit_gracefully = true unless driver_name == 'cryptum-repl' puts "\nERROR: See #{log_file_path} for more details." if driver_name == 'cryptum-repl' when :fatal # This is reserved for the Cryptum::UI::Exit module # if the Interrupt or StandardError exceptions are # triggered. This prevents infintely attempting to # exit if something in the module fails. logger.level = Logger::FATAL if driver_name == 'cryptum-repl' puts "\nFATAL ERROR: Event history cannot be saved." puts "See #{log_file_path} for more details." end when :info, :learning logger.level = Logger::INFO when :unknown logger.level = Logger::UNKNOWN when :warn logger.level = Logger::WARN else level_error = "ERROR: Invalid log level. Valid options are:\n" level_error += ":info\n:warn\n:unknown\n:fatal\n:error\n:debug" raise level_error end if level == :learning logger.formatter = proc do |_severity, datetime, progname, json_msg| date = datetime.strftime(datetime_str) JSON.pretty_generate( date: date, session: session, symbol: symbol, progname: progname, msg: json_msg ) end else log_event = "driver: #{driver_name}" if event_history.respond_to?('order_book') log_event = ", session: #{session}, " log_event += "symbol: #{symbol}" end if msg.instance_of?(Interrupt) logger.level = Logger::WARN log_event += ' => CTRL+C Detected...Exiting Session.' exit_gracefully = true unless driver_name == 'cryptum-repl' else log_event += " => #{msg}" if msg.respond_to?('backtrace') && !msg.instance_of?(Errno::ECONNRESET) log_event += " => \n\t#{msg.backtrace.join("\n\t")}" log_event += "\n\n\n" end end logger.add(logger.level, log_event, which_self) Cryptum::UI::Exit.gracefully(event_history: event_history) if exit_gracefully end rescue Interrupt, StandardError => e raise e end # Display Usage for this Module public_class_method def self.help puts "USAGE: logger = #{self}.create() " end end end