
    	,hk                         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Z G d d	      Z	 e	       Z
y)
    N)TimedRotatingFileHandler)PathlogsT)parentsexist_okz%(asctime)s :: %(message)sc                        e Zd ZdZdZ fdZ eedz        fdedej                  fdZ
deddfd	Zdd
ededdfdZddeddfdZdeddfdZddZ xZS )LogUtilsz/Utility class for logging messages (Singleton).Nc                     | j                   Wt        | 	  |       | _         | j                   j                          t	        j
                  | 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.
        )	_instancesuper__new___setup_loggeratexitregister_shutdown_logger)clsargskwargs	__class__s      ./var/www/html/RetailAPI/app/utils/log_utils.pyr   zLogUtils.__new__   sO    $ == !GOC0CMMM'')OOCMM::;}}    zdefault.loglog_filereturnc                 p   t        j                  |      | _        | j                  j                  s{| j                  j	                  t         j
                         t        |ddd      }|j                  t        j                  t                     | j                  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_handlers      r   r   zLogUtils._setup_logger(   s    ''1{{##KK  .3!	L %%g&7&78I&JKKK""<0{{r   messagec                 |    | j                  t        t        dz               | j                  j	                  |       y)zLog an access message.z
access.logN)r   strLOGS_DIRr$   infor,   r.   s     r   
log_accesszLogUtils.log_access8   s,    3x,678!r   namec                     | j                  t        t        dz               |r| d| n|}| j                  j	                  |       y)zLog an error message.z	error.logz: Nr   r0   r1   r$   error)r,   r5   r.   formatted_messages       r   	error_logzLogUtils.error_log=   sB    3x+56748tfBwi0g+,r   c                     | j                  t        t        dz               t        j                  |       | j
                  j                  |       y)zLog a debug message.z	debug.logN)r   r0   r1   r"   debugr$   r8   r3   s     r   	debug_logzLogUtils.debug_logC   s8    3x+567g'"r   c                 |    | j                  t        t        dz               | j                  j	                  |       y)zLog a stack trace message.zstack_trace.logNr7   r3   s     r   stack_tracezLogUtils.stack_traceI   s-    3x*;;<='"r   c                     | j                   j                  D ]B  }t        j                  d       |j	                          | j                   j                  |       D y)zShutdown logger.zShutting down logger...N)r$   r%   r"   r2   closeremoveHandler)r,   handlers     r   r   zLogUtils._shutdown_loggerN   s>    {{++GLL23MMOKK%%g. ,r   ) rD   )rD   )r   N)__name__
__module____qualname____doc__r   r   r0   r1   r"   Loggerr   r4   r:   r=   r?   r   __classcell__)r   s   @r   r	   r	      s    9I0 -0=0H,I c gnn  "# "$ "
-c - -d -# #d ##3 #4 #
/r   r	   )r   r"   logging.handlersr   pathlibr   r1   mkdirr*   r	   CUSTOM_LOGGER r   r   <module>rP      sF      5 < td +0 H/ H/V 
r   