Really-amin commited on
Commit
ad6efe4
·
verified ·
1 Parent(s): 6b8f62b

Upload 2 files

Browse files
Files changed (1) hide show
  1. app/utils.py +51 -0
app/utils.py CHANGED
@@ -584,3 +584,54 @@ if __name__ == "__main__":
584
 
585
  print("\n" + "=" * 50)
586
  print("All tests completed!")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
584
 
585
  print("\n" + "=" * 50)
586
  print("All tests completed!")
587
+
588
+
589
+ # ==== Added by fix ====
590
+ def setup_logging() -> logging.Logger:
591
+ """Initialize application-wide logging and return a logger.
592
+
593
+ This implementation is safe to call multiple times and will not duplicate handlers.
594
+ It uses config.LOG_LEVEL and config.LOG_FORMAT if available, otherwise falls back
595
+ to sensible defaults.
596
+ """
597
+ # Choose logger name
598
+ logger_name = getattr(config, "LOGGER_NAME", "crypto_aggregator")
599
+ logger = logging.getLogger(logger_name)
600
+
601
+ # If handlers already exist, just return the existing logger
602
+ if logger.handlers:
603
+ return logger
604
+
605
+ # Determine log level
606
+ level_name = getattr(config, "LOG_LEVEL", "INFO").upper()
607
+ level = getattr(logging, level_name, logging.INFO)
608
+ logger.setLevel(level)
609
+
610
+ # Determine log format
611
+ log_format = getattr(
612
+ config,
613
+ "LOG_FORMAT",
614
+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s",
615
+ )
616
+ formatter = logging.Formatter(log_format)
617
+
618
+ # Console handler
619
+ console_handler = logging.StreamHandler()
620
+ console_handler.setFormatter(formatter)
621
+ logger.addHandler(console_handler)
622
+
623
+ # Optional file handler if LOG_FILE is defined
624
+ log_file = getattr(config, "LOG_FILE", None)
625
+ if log_file:
626
+ try:
627
+ file_handler = RotatingFileHandler(log_file, maxBytes=1_000_000, backupCount=3)
628
+ file_handler.setFormatter(formatter)
629
+ logger.addHandler(file_handler)
630
+ except Exception as e:
631
+ # If file handler fails, log the warning but continue with console logging
632
+ logging.getLogger(__name__).warning(
633
+ "Failed to initialize file logging (%s): %s", log_file, e
634
+ )
635
+
636
+ logger.info("Logging initialized (level=%s)", level_name)
637
+ return logger