Skip to content

Commit 7608451

Browse files
committed
updates
1 parent 2317459 commit 7608451

File tree

1 file changed

+19
-11
lines changed

1 file changed

+19
-11
lines changed

dsg_lib/common_functions/logging_config.py

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
This module provides a comprehensive logging setup using the loguru library, facilitating easy logging management for Python applications. The `config_log` function, central to this module, allows for extensive customization of logging behavior. It supports specifying the logging directory, log file name, logging level, and controls for log rotation, retention, and formatting among other features. Additionally, it offers advanced options like backtrace and diagnose for in-depth debugging, and the ability to append the application name to the log file for clearer identification.
44
55
Usage example:
6+
7+
```python
68
from dsg_lib.common_functions.logging_config import config_log
79
810
config_log(
@@ -11,15 +13,7 @@
1113
logging_level='DEBUG', # Minimum logging level
1214
log_rotation='100 MB', # Size threshold for log rotation
1315
log_retention='30 days', # Duration to retain old log files
14-
log_backtrace=True, # Enable detailed backtraces in logs
15-
log_format="<green>{time:YYYY-MM-DD HH:mm:ss.SSSSSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>", # Custom log format
16-
log_serializer=False, # Toggle log serialization
17-
log_diagnose=True, # Enable diagnostic information in logs
18-
app_name='my_app', # Application name for log identification
19-
append_app_name=True # Append application name to log file names
2016
enqueue=True, # Enqueue log messages
21-
intercept_standard_logging=True, # Intercept standard Python logging
22-
file_sink=True # Use a file sink for logging
2317
)
2418
2519
# Example log messages
@@ -28,6 +22,7 @@
2822
logger.error("This is an error message")
2923
logger.warning("This is a warning message")
3024
logger.critical("This is a critical message")
25+
```
3126
3227
Author: Mike Ryan
3328
DateCreated: 2021/07/16
@@ -89,7 +84,7 @@ def config_log(
8984
logging_directory='logs',
9085
log_name='app.log',
9186
logging_level='DEBUG',
92-
log_rotation='500 MB',
87+
log_rotation='100 MB',
9388
log_retention='10 days',
9489
log_backtrace=True,
9590
log_format="<green>{time:YYYY-MM-DD HH:mm:ss.SSSSSS}</green> | <level>{level: <8}</level> | <cyan>{name}</cyan>:<cyan>{function}</cyan>:<cyan>{line}</cyan> - <level>{message}</level>",
@@ -216,6 +211,19 @@ def emit(self, record):
216211

217212

218213
class ResilientFileSink:
214+
"""
215+
A file sink designed for resilience, capable of retrying write operations.
216+
217+
This class implements a resilient file writing mechanism that attempts to write messages to a file, retrying the operation a specified number of times if it fails. This is particularly useful in scenarios where write operations might intermittently fail due to temporary issues such as file system locks or networked file system delays.
218+
219+
Attributes:
220+
path (str): The path to the file where messages will be written.
221+
max_retries (int): The maximum number of retry attempts for a failed write operation.
222+
retry_delay (float): The delay between retry attempts, in seconds.
223+
224+
Methods:
225+
write(message): Attempts to write a message to the file, retrying on failure up to `max_retries` times.
226+
"""
219227
def __init__(self, path, max_retries=5, retry_delay=0.1):
220228
self.path = path
221229
self.max_retries = max_retries
@@ -234,15 +242,15 @@ def write(self, message):
234242
raise # Reraise if max retries exceeded
235243

236244

237-
basic_config_handlers = []
245+
basic_config_handlers:list = []
246+
238247
if file_sink:
239248
# Create an instance of ResilientFileSink
240249
resilient_sink = ResilientFileSink(str(log_path))
241250

242251
# Configure the logger to use the ResilientFileSink
243252
basic_config_handlers.append(resilient_sink)
244253

245-
basic_config_handlers = []
246254
if intercept_standard_logging:
247255
basic_config_handlers.append(InterceptHandler())
248256

0 commit comments

Comments
 (0)