
    g"                         d dl mZ d dlmZmZmZmZ d dlmZm	Z	m
Z
mZ d dlZd dlZd dlmZ  G d de      Z G d d	      Zy)
    )Enum)OptionalDictAnyUnion)ForeBackStyleinitN)datetimec                        e Zd ZdZdZdZdZdZy)LogLevel               N)__name__
__module____qualname__DEBUGINFOSUCCESSWARNINGERROR     J/var/www/openai/venv/lib/python3.12/site-packages/crawl4ai/async_logger.pyr   r      s    EDGGEr   r   c                      e Zd ZdZdddddddd	d
d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ej                  ddddfdee   dededeeeef      deeeef      defdZdedefdZdedefdZdefdZ	 	 	 d-dedededeeeef      deeeef      dee   fd Zd.dedefd!Zd/dedefd"Zd0dedefd#Zd1dedefd$Z d2dedefd%Z!	 	 d3d&ed'ed(e"ded)ef
d*Z#	 	 d4d&ed+eded)efd,Z$y)5AsyncLoggerz
    Asynchronous logger with support for colored console output and file logging.
    Supports templated messages with colored components.
    u   →u   ✓u   ↓u   ◆u   ■u   ●   ×u   ⋯u   ℹu   ⚠)
INITREADYFETCHSCRAPEEXTRACTCOMPLETEr   r   r   r   N
   Tlog_file	log_level	tag_widthiconscolorsverbosec                 L   t                || _        || _        || _        |xs | j                  | _        |xs | j                  | _        || _        |rRt        j                  t        j                  j                  t        j                  j                  |            d       yy)ax  
        Initialize the logger.
        
        Args:
            log_file: Optional file path for logging
            log_level: Minimum log level to display
            tag_width: Width for tag formatting
            icons: Custom icons for different tags
            colors: Custom colors for different log levels
            verbose: Whether to output to console
        T)exist_okN)r   r)   r*   r+   DEFAULT_ICONSr,   DEFAULT_COLORSr-   r.   osmakedirspathdirnameabspath)selfr)   r*   r+   r,   r-   r.   s          r   __init__zAsyncLogger.__init__*   s{    ( 	 ""0d00
3 3 3 KK(ABTR r   tagreturnc                 B    d| dj                  | j                  d      S )z#Format a tag with consistent width.[].)ljustr+   r8   r:   s     r   _format_tagzAsyncLogger._format_tagJ   s!    3%qz44r   c                 T    | j                   j                  || j                   d         S )z=Get the icon for a tag, defaulting to info icon if not found.r   )r,   getrA   s     r   	_get_iconzAsyncLogger._get_iconN   s     zz~~c4::f#566r   messagec                    | j                   rt        j                         j                  d      dd }t	        | j                   dd      5 }|j                  t        j                  d      j                  t        j                  d      }t        t              j                         D ]%  }t        |t              s|j                  |d      }' |j                  d| d	| d
       ddd       yy# 1 sw Y   yxY w)z.Write a message to the log file if configured.z%Y-%m-%d %H:%M:%S.%fNazutf-8)encoding r=   z] 
)r)   r   nowstrftimeopenreplacer   RESETr
   	RESET_ALLvarsvalues
isinstancestrwrite)r8   rF   	timestampfclean_messagecolors         r   _write_to_filezAsyncLogger._write_to_fileR   s    == //0FGLIdmmS7;q '

B ? G GY[ \!$Z..0E!%-(5(=(=eR(H 1 !I;br:; <; ;;s   A/C1;,C11C:levelparams
base_colorc                    |j                   | j                  j                   k  ry|rg	  |j                  di |}|rQ|j                         D ]>  \  }	}
|	|v st	        ||	         }|j                  ||
 | t        j                         }@ n|}|xs | j                  |   }
|
 | j                  |       d| j                  |       d| t        j                   }| j                  s|j                  dd      rt!        |       | j#                  |       y# t        $ r }d| d}t        j                  }Y d}~d}~ww xY w)a  
        Core logging method that handles message formatting and output.
        
        Args:
            level: Log level for this message
            message: Message template string
            tag: Tag for the message
            params: Parameters to format into the message
            colors: Color overrides for specific parameters
            base_color: Base color for the entire message
        Nz!LOGGING ERROR: Missing parameter z in message template force_verboseFr   )valuer*   formatitemsrV   rP   r
   rR   KeyErrorr   r   r-   rB   rE   r.   rD   printr\   )r8   r]   rF   r:   r^   r-   r_   kwargsformatted_messagekeyr[   	value_strelog_lines                 r   _logzAsyncLogger._log^   sF   * ;;--- '$2GNN$<V$<! &,lln
U&=(+F3K(8I0A0I0I )#(')U__4E F1-	 '5 !( 0dkk%0WT--c231T^^C5H4IK\J]^c^m^m]no <<6::ou=(O 	H%  '&GsJ^$_! 's   .D 6D 	E!D<<Ec                 J     | j                   t        j                  ||fi | y)zLog a debug message.N)rn   r   r   r8   rF   r:   rh   s       r   debugzAsyncLogger.debug       		(..'39&9r   c                 J     | j                   t        j                  ||fi | y)zLog an info message.N)rn   r   r   rp   s       r   infozAsyncLogger.info   s    		(--#88r   c                 J     | j                   t        j                  ||fi | y)zLog a success message.N)rn   r   r   rp   s       r   successzAsyncLogger.success       		(""GS;F;r   c                 J     | j                   t        j                  ||fi | y)zLog a warning message.N)rn   r   r   rp   s       r   warningzAsyncLogger.warning   rw   r   c                 J     | j                   t        j                  ||fi | y)zLog an error message.Nrn   r   r   rp   s       r   errorzAsyncLogger.error   rr   r   urlrv   timing
url_lengthc           
          | j                  |rt        j                  nt        j                  d|||||d|rt        j
                  nt        j                  t        j                  d       y)aH  
        Convenience method for logging URL fetch status.
        
        Args:
            url: The URL being processed
            success: Whether the operation was successful
            timing: Time taken for the operation
            tag: Tag for the message
            url_length: Maximum length for URL in log
        z?{url:.{url_length}}... | Status: {status} | Time: {timing:.2f}s)r}   r   statusr~   )r   r~   )r]   rF   r:   r^   r-   N)rn   r   r   r   r   GREENREDYELLOW)r8   r}   rv   r~   r:   r   s         r   
url_statuszAsyncLogger.url_status   sZ    $ 			&-(""8>>U(! 	 )0$**TXX++ 	 	
r   r|   c           	      R    | j                  t        j                  d||||d       y)z
        Convenience method for logging error status.
        
        Args:
            url: The URL being processed
            error: Error message
            tag: Tag for the message
            url_length: Maximum length for URL in log
        z'{url:.{url_length}}... | Error: {error})r}   r   r|   )r]   rF   r:   r^   Nr{   )r8   r}   r|   r:   r   s        r   error_statuszAsyncLogger.error_status   s0      			..=(	 	 		
r   )NNN)r   )r   )r   )r   )r   )r$   2   )r   r   )%r   r   r   __doc__r1   r   r   r   LIGHTBLACK_EXr   CYANr   r   r   r   r   r   r2   r   rV   intr   boolr9   rB   rE   r\   r   rn   rq   rt   rv   ry   r|   floatr   r   r   r   r   r    r       sg    M 	**tyy$**$++N #'&nn*.04S3-S S 	S
 S#X'S hm,-S S@5s 5s 57S 7S 7
<c 
<" ,0+/$(8&8& 8& 	8&
 c3h(8& c3h(8& SM8&t:S :s :9C 9c 9<s < <<s < <:S :s :  
 
  
 	 

  
  
L 

 
 	

 
r   r    )enumr   typingr   r   r   r   coloramar   r	   r
   r   timer3   r   r   r    r   r   r   <module>r      s5     - - , ,  	 t X
 X
r   