o
    Uhk                     @   sZ   d dl Z d dlZd dlmZ d dlmZ edZejddd dZG dd	 d	Z	e	 Z
dS )
    N)TimedRotatingFileHandler)PathlogsT)parentsexist_okz%(asctime)s :: %(message)sc                       s   e Zd ZdZdZ fddZeed fdedej	fdd	Z
d
eddfddZdded
eddfddZdd
eddfddZd
eddfddZdddZ  ZS )LogUtilsz/Utility class for logging messages (Singleton).Nc                    s6   | j du rt | | _ | j   t| j j | j S )aa  
        Create a new instance of the class if it doesn't already exist.

        This method is responsible for creating a singleton instance of the class.
        If the instance doesn't exist, it creates a new instance, sets up the logger,
        and registers a shutdown function. If the instance already exists, it returns
        the existing instance.

        Args:
            cls: The class object.
            *args: Variable length argument list.
            **kwargs: Arbitrary keyword arguments.

        Returns
        -------
            object: The singleton instance of the class.
        N)	_instancesuper__new___setup_loggeratexitregister_shutdown_logger)clsargskwargs	__class__ ./var/www/html/RetailAPI/app/utils/log_utils.pyr
      s
   

zLogUtils.__new__zdefault.loglog_filereturnc                 C   sT   t || _| jjs'| jt j t|dddd}|t t	 | j
| | jS )z#Set up logging for the application.midnight      )filenamewhenintervalbackupCount)logging	getLoggerloggerhandlerssetLevelINFOr   setFormatter	FormatterLOGGING_FORMATTER
addHandler)selfr   file_handlerr   r   r   r   (   s   zLogUtils._setup_loggermessagec                 C   "   |  ttd  | j| dS )zLog an access message.z
access.logN)r   strLOGS_DIRr!   infor)   r+   r   r   r   
log_access8      zLogUtils.log_access namec                 C   s8   |  ttd  |r| d| n|}| j| dS )zLog an error message.z	error.logz: Nr   r-   r.   r!   error)r)   r4   r+   formatted_messager   r   r   	error_log=   s   zLogUtils.error_logc                 C   s,   |  ttd  t| | j| dS )zLog a debug message.z	debug.logN)r   r-   r.   r   debugr!   r6   r0   r   r   r   	debug_logC   s   
zLogUtils.debug_logc                 C   r,   )zLog a stack trace message.zstack_trace.logNr5   r0   r   r   r   stack_traceI   r2   zLogUtils.stack_tracec                 C   s0   | j jD ]}td |  | j | qdS )zShutdown logger.zShutting down logger...N)r!   r"   r   r/   closeremoveHandler)r)   handlerr   r   r   r   N   s
   
zLogUtils._shutdown_logger)r3   r3   )r3   )r   N)__name__
__module____qualname____doc__r   r
   r-   r.   r   Loggerr   r1   r8   r:   r;   r   __classcell__r   r   r   r   r      s     r   )r   r   logging.handlersr   pathlibr   r.   mkdirr'   r   CUSTOM_LOGGERr   r   r   r   <module>   s    
K