lib/cryptum/log.rb in cryptum-0.0.403 vs lib/cryptum/log.rb in cryptum-0.0.404

- old
+ new

@@ -10,24 +10,34 @@ level = opts[:level].to_s.downcase.to_sym msg = opts[:msg] which_self = opts[:which_self].to_s event_history = opts[:event_history] + # 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 = File.open('/tmp/cryptum.log', 'a') + log_file = File.open("/tmp/cryptum-ai-#{session}-#{symbol}.json", 'a') if level == :learning # 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 - # Only attempt to exit gracefully if level == :error - exit_gracefully = false - case level when :debug logger.level = Logger::DEBUG when :error exit_gracefully = true @@ -36,40 +46,52 @@ # 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 + when :info, :learning + logger.level = Logger::INFO when :unknown logger.level = Logger::UNKNOWN when :warn logger.level = Logger::WARN - when :info - logger.level = Logger::INFO 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 - logger.datetime_format = '%Y-%m-%d %H:%M:%S.%N' - log_event = '' - if event_history.respond_to?('order_book') - log_event = "session: #{event_history.order_book[:path].split('/').first}, " - log_event += "symbol: #{event_history.order_book[:symbol]}" - end - - if msg.instance_of?(Interrupt) - log_event += ' => CTRL+C Detected...Exiting Session.' - logger.level = Logger::WARN - exit_gracefully = true + 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 += " => #{msg}" - log_event += " => \n\t#{msg.backtrace.join("\n\t")}\n\n\n" if msg.respond_to?('backtrace') - end + log_event = '' + if event_history.respond_to?('order_book') + log_event = "session: #{session}, " + log_event += "symbol: #{symbol}" + end - logger.add(logger.level, log_event, which_self) + if msg.instance_of?(Interrupt) + log_event += ' => CTRL+C Detected...Exiting Session.' + logger.level = Logger::WARN + exit_gracefully = true + else + log_event += " => #{msg}" + log_event += " => \n\t#{msg.backtrace.join("\n\t")}\n\n\n" if msg.respond_to?('backtrace') + end - Cryptum::UI::Exit.gracefully(event_history: event_history) if exit_gracefully + 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