o
    d6iK                  	   @   s   d Z ddlZddlZddlmZ G dd dZG dd dejZejddfd	e	d
e
dee ddfddZdedejfddZdS )a  
Centralized logging configuration with colors and timestamps.

This module configures logging for the entire application with:
- Colored output for different log levels
- Timestamps in ISO format
- Structured log format with module names
- Different colors for different log levels
    N)Optionalc                   @   s8   e Zd ZdZdZdZdZdZdZdZ	dZ
d	Zd
ZdZdS )	LogColorsz%ANSI color codes for terminal output.z[0mz[1mz[36mz[32mz[33mz[31mz[35mz[90mz[94mN)__name__
__module____qualname____doc__RESETBOLDDEBUGINFOWARNINGERRORCRITICAL	TIMESTAMPMODULEMESSAGE r   r   6/var/www/html/karishye-ai-python/app/logging_config.pyr      s    r   c                
       sp   e Zd ZdZdZejejejejej	ej	ej
ej
ejejiZddef fddZdejdef fd	d
Z  ZS )ColoredFormatterz2Custom formatter that adds colors to log messages.z%(color_timestamp)s%(asctime)s%(reset)s | %(color_level)s%(levelname)-8s%(reset)s | %(color_module)s%(name)s%(reset)s | %(color_message)s%(message)s%(reset)sT
use_colorsc                    s   t  j| jdd || _dS )z
        Initialize the formatter.
        
        Args:
            use_colors: Whether to use colored output (default: True)
        z%Y-%m-%d %H:%M:%S)fmtdatefmtN)super__init__FORMAT_TEMPLATEr   )selfr   	__class__r   r   r   7   s
   
zColoredFormatter.__init__recordreturnc                    sp   | j r#tj r#tj|_| j|j	tj
|_tj|_tj|_tj
|_nd|_d|_d|_d|_d|_t |S )z"Format the log record with colors. )r   sysstderrisattyr   r   color_timestampLEVEL_COLORSgetlevelnor   color_levelr   color_moduler   color_messageresetr   format)r   r   r   r   r   r,   D   s   
zColoredFormatter.format)T)r   r   r   r   r   loggingr
   r   r   r   r   r   r%   boolr   	LogRecordstrr,   __classcell__r   r   r   r   r   #   s    	 r   Tlevelr   log_filer   c                 C   s   t  }||  |j  t tj}||  |t	|d |
| |rBt j|ddd}||  |t	dd |
| t dt j t dt j t dt j t d	t j t t}|d
 |r||d|  dS dS )a  
    Setup logging configuration for the entire application.
    
    Args:
        level: Logging level (default: logging.INFO)
        use_colors: Whether to use colored output for console (default: True)
        log_file: Optional path to log file (default: None, logs to console only)
    
    Example:
        >>> from app.logging_config import setup_logging
        >>> setup_logging(level=logging.DEBUG, log_file="app.log")
    )r   azutf-8)modeencodingFhttpxhttpcoreurllib3
sqlalchemyu;   🚀 Logging initialized with colored output and timestampsu   📝 Logging to file: N)r-   	getLoggersetLevelhandlersclearStreamHandlerr!   r"   setFormatterr   
addHandlerFileHandlerr   r   info)r2   r   r3   root_loggerconsole_handlerfile_handlerloggerr   r   r   setup_loggingX   s*   







rH   namec                 C   s
   t | S )aS  
    Get a logger instance with the module name.
    
    Args:
        name: Logger name (typically __name__)
    
    Returns:
        Configured logger instance
    
    Example:
        >>> from app.logging_config import get_logger
        >>> logger = get_logger(__name__)
        >>> logger.info("This is a colored log message")
    )r-   r;   )rI   r   r   r   
get_logger   s   
rJ   )r   r-   r!   typingr   r   	Formatterr   r   intr.   r0   rH   LoggerrJ   r   r   r   r   <module>   s&    
6
3