o
    ui                     @   s2   d dl Z d dlZd dlZG dd dZdd ZdS )    Nc                   @   s0   e Zd ZdZdZedddZedddZdS )Loggerz7
    Logger class for managing logging operations.
    Nc                 C   s   | j du r| || _ | j S )aM  
        Get the logger instance with the specified name. If it doesn't exist, create and cache it.

        Args:
            cls (type): The class type.
            name (str, optional): The name of the logger. Defaults to None, which uses the class name.

        Returns:
            logging.Logger: The logger instance.
        N)_loggerinit_logger)clsname r   7/home/ubuntu/sommelier/podcast-pipeline/utils/logger.py
get_logger   s   
zLogger.get_loggerc           
   	   C   s  |du r"d}dt jv r|d t jd  }dt jv r"|d t jd  }td|  t|}|tj tdt	 }td	t	 }t j
d
| dd td}td
| d| d| d}|| || G dd dtj}|d}t }	|	| ||	 |S )a  
        Initialize the logger, including file and console logging.

        Args:
            cls (type): The class type.
            name (str, optional): The name of the logger. Defaults to None.

        Returns:
            logging.Logger: The initialized logger instance.
        NmainSELF_ID_IDCUDA_VISIBLE_DEVICES_GPUzInitialize logger for z%Y-%m-%dz%H-%M-%Szlogs/T)exist_okz4%(asctime)s - %(name)s - %(levelname)s - %(message)s/-z.logc                       s    e Zd ZdZ fddZ  ZS )z*Logger.init_logger.<locals>.ColorFormatterzS
            Custom log formatter to add color to specific log levels.
            c                    s   |j tjkrdt|j d |_n2|j tjkr"dt|j d |_n!|j tjkr3dt|j d |_n|j tjkrCdt|j d |_t 	|S )a  
                Format the log record with color based on log level.

                Args:
                    record (logging.LogRecord): The log record to format.

                Returns:
                    str: The formatted log message.
                z[1;31mz[0mz[1;33mz[1;34mz[1;32m)
levelnologgingERRORstrmsgWARNINGINFODEBUGsuperformat)selfrecord	__class__r   r   r   H   s   
z1Logger.init_logger.<locals>.ColorFormatter.format)__name__
__module____qualname____doc__r   __classcell__r   r   r   r   ColorFormatterC   s    r%   )osenvironprintr   	getLoggersetLevelr   timestrftime	localtimemakedirs	FormatterFileHandlersetFormatter
addHandlerStreamHandler)
r   r   loggerlog_datelog_time	formatterfhr%   color_formatterchr   r   r   r       s4   






zLogger.init_logger)N)r    r!   r"   r#   r   classmethodr	   r   r   r   r   r   r   	   s    r   c                    s    fdd}|S )z
    Decorator to log the execution time of a function.

    Args:
        func (callable): The function whose execution time is to be logged.

    Returns:
        callable: The wrapper function that logs the execution time of the original function.
    c                     sD   t   } | i |}t   }t d j d||  d |S )Nz	Function z took z seconds to execute)r+   r   r	   debugr    )argskwargs
start_timeresultend_timefuncr   r   wrapperq   s   ztime_logger.<locals>.wrapperr   )rC   rD   r   rB   r   time_loggerf   s   	rE   )r   r+   r&   r   rE   r   r   r   r   <module>   s
   ]