File size: 2,153 Bytes
43cd37c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# logger_config.py
#
# Imports
import logging
from logging.handlers import RotatingFileHandler
from pythonjsonlogger import jsonlogger
import os
#
############################################################################################################
#
# Functions:

def setup_logger(log_file_path="tldw_app_logs.json"):
    """

    Sets up the logger with both StreamHandler and FileHandler, formatted in JSON.



    Parameters:

        log_file_path (str): Path to the JSON log file.



    Returns:

        logging.Logger: Configured logger instance.

    """
    logger = logging.getLogger("tldw_app_logs")
    logger.setLevel(logging.DEBUG)  # Set to DEBUG for detailed logs

    # Prevent adding multiple handlers if the logger is already configured
    if not logger.handlers:
        # StreamHandler for console output
        stream_handler = logging.StreamHandler()
        stream_formatter = jsonlogger.JsonFormatter(
            '%(asctime)s %(levelname)s %(name)s event %(event)s type %(type)s value %(value)s labels %(labels)s timestamp %(timestamp)s'
        )
        stream_handler.setFormatter(stream_formatter)
        logger.addHandler(stream_handler)

        # Ensure the directory for log_file_path exists
        log_dir = os.path.dirname(log_file_path)
        if log_dir and not os.path.exists(log_dir):
            os.makedirs(log_dir, exist_ok=True)

        # RotatingFileHandler for writing logs to a JSON file with rotation
        file_handler = RotatingFileHandler(
            log_file_path, maxBytes=10*1024*1024, backupCount=5  # 10 MB per file, keep 5 backups
        )
        file_formatter = jsonlogger.JsonFormatter(
            '%(asctime)s %(levelname)s %(name)s event %(event)s type %(type)s value %(value)s labels %(labels)s timestamp %(timestamp)s'
        )
        file_handler.setFormatter(file_formatter)
        logger.addHandler(file_handler)

    return logger

# Initialize the logger
logger = setup_logger()

#
# End of Functions
############################################################################################################