Sfoglia il codice sorgente

Merge pull request #73 from oddlama/feat-optional-log-init

feat: add init_logging option to allow users to use their own logging
Kolja Beigel 5 mesi fa
parent
commit
73ed6d96c3
2 ha cambiato i file con 36 aggiunte e 30 eliminazioni
  1. 2 0
      README.md
  2. 34 30
      RealtimeSTT/audio_recorder.py

+ 2 - 0
README.md

@@ -435,6 +435,8 @@ When you initialize the `AudioToTextRecorder` class, you have various options to
 
 - **level** (int, default=logging.WARNING): Logging level.
 
+- **init_logging** (bool, default=True): Whether to initialize the logging framework. Set to False to manage this yourself.
+
 - **handle_buffer_overflow** (bool, default=True): If set, the system will log a warning when an input overflow occurs during recording and remove the data from the buffer.
 
 - **beam_size** (int, default=5): The beam size to use for beam search decoding.

+ 34 - 30
RealtimeSTT/audio_recorder.py

@@ -208,6 +208,7 @@ class AudioToTextRecorder:
                  use_microphone=True,
                  spinner=True,
                  level=logging.WARNING,
+                 init_logging=True,
 
                  # Realtime transcription parameters
                  enable_realtime_transcription=False,
@@ -314,6 +315,8 @@ class AudioToTextRecorder:
         - spinner (bool, default=True): Show spinner animation with current
             state.
         - level (int, default=logging.WARNING): Logging level.
+        - init_logging (bool, default=True): Whether to initialize
+            the logging framework. Set to False to manage this yourself.
         - enable_realtime_transcription (bool, default=False): Enables or
             disables real-time transcription of audio. When set to True, the
             audio will be transcribed continuously as it is being recorded.
@@ -568,36 +571,37 @@ class AudioToTextRecorder:
         self.early_transcription_on_silence = early_transcription_on_silence
         self.use_extended_logging = use_extended_logging
 
-        # Initialize the logging configuration with the specified level
-        log_format = 'RealTimeSTT: %(name)s - %(levelname)s - %(message)s'
-
-        # Adjust file_log_format to include milliseconds
-        file_log_format = '%(asctime)s.%(msecs)03d - ' + log_format
-
-        # Get the root logger
-        logger = logging.getLogger()
-        logger.setLevel(logging.DEBUG)  # Set the root logger's level to DEBUG
-
-        # Remove any existing handlers
-        logger.handlers = []
-
-        # Create a console handler and set its level
-        console_handler = logging.StreamHandler()
-        console_handler.setLevel(level) 
-        console_handler.setFormatter(logging.Formatter(log_format))
-
-        # Add the handlers to the logger
-        if not no_log_file:
-            # Create a file handler and set its level
-            file_handler = logging.FileHandler('realtimesst.log')
-            file_handler.setLevel(logging.DEBUG)
-            file_handler.setFormatter(logging.Formatter(
-                file_log_format,
-                datefmt='%Y-%m-%d %H:%M:%S'
-            ))
-
-            logger.addHandler(file_handler)
-        logger.addHandler(console_handler)
+        if init_logging:
+            # Initialize the logging configuration with the specified level
+            log_format = 'RealTimeSTT: %(name)s - %(levelname)s - %(message)s'
+
+            # Adjust file_log_format to include milliseconds
+            file_log_format = '%(asctime)s.%(msecs)03d - ' + log_format
+
+            # Get the root logger
+            logger = logging.getLogger()
+            logger.setLevel(logging.DEBUG)  # Set the root logger's level to DEBUG
+
+            # Remove any existing handlers
+            logger.handlers = []
+
+            # Create a console handler and set its level
+            console_handler = logging.StreamHandler()
+            console_handler.setLevel(level) 
+            console_handler.setFormatter(logging.Formatter(log_format))
+
+            # Add the handlers to the logger
+            if not no_log_file:
+                # Create a file handler and set its level
+                file_handler = logging.FileHandler('realtimesst.log')
+                file_handler.setLevel(logging.DEBUG)
+                file_handler.setFormatter(logging.Formatter(
+                    file_log_format,
+                    datefmt='%Y-%m-%d %H:%M:%S'
+                ))
+
+                logger.addHandler(file_handler)
+            logger.addHandler(console_handler)            
 
         self.is_shut_down = False
         self.shutdown_event = mp.Event()