Skip to content

Commit 5891390

Browse files
committed
adding help for missing log file issue for high volume
1 parent 97c6f23 commit 5891390

File tree

1 file changed

+35
-5
lines changed

1 file changed

+35
-5
lines changed

dsg_lib/common_functions/logging_config.py

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@
3535
Date: 2024/05/16
3636
License: MIT
3737
"""
38-
38+
import time
3939
import logging
4040
from pathlib import Path
4141
from uuid import uuid4
@@ -55,6 +55,9 @@ def config_log(
5555
log_diagnose: bool = False,
5656
app_name: str = None,
5757
append_app_name: bool = False,
58+
enqueue: bool = True,
59+
intercept_standard_logging: bool = True,
60+
file_sink: bool = True,
5861
):
5962
"""
6063
Configures and sets up a logger using the loguru package.
@@ -140,7 +143,7 @@ def config_log(
140143
log_path,
141144
level=logging_level.upper(),
142145
format=log_format,
143-
enqueue=True,
146+
enqueue=enqueue,
144147
backtrace=log_backtrace,
145148
rotation=log_rotation,
146149
retention=log_retention,
@@ -197,9 +200,36 @@ def emit(self, record):
197200
# Configure standard logging to use interceptor handler
198201
logging.basicConfig(handlers=[InterceptHandler()], level=logging_level.upper())
199202

200-
# Add interceptor handler to all existing loggers
201-
for name in logging.root.manager.loggerDict:
202-
logging.getLogger(name).addHandler(InterceptHandler())
203+
if intercept_standard_logging:
204+
# Add interceptor handler to all existing loggers
205+
for name in logging.root.manager.loggerDict:
206+
logging.getLogger(name).addHandler(InterceptHandler())
203207

204208
# Set the root logger's level to the lowest level possible
205209
logging.getLogger().setLevel(logging.NOTSET)
210+
211+
212+
class ResilientFileSink:
213+
def __init__(self, path, max_retries=5, retry_delay=0.1):
214+
self.path = path
215+
self.max_retries = max_retries
216+
self.retry_delay = retry_delay
217+
218+
def write(self, message):
219+
for attempt in range(self.max_retries):
220+
try:
221+
with open(self.path, 'a') as file:
222+
file.write(str(message))
223+
break # Successfully written, break the loop
224+
except FileNotFoundError:
225+
if attempt < self.max_retries - 1:
226+
time.sleep(self.retry_delay) # Wait before retrying
227+
else:
228+
raise # Reraise if max retries exceeded
229+
230+
if file_sink:
231+
# Create an instance of ResilientFileSink
232+
resilient_sink = ResilientFileSink(str(log_path))
233+
234+
# Configure the logger to use the ResilientFileSink
235+
logger.add(resilient_sink, format=log_format)

0 commit comments

Comments
 (0)