U
    [h!                     @  s  U d Z ddlmZ ddlZddlZddlmZ ddlmZ ddlmZ ddlm	Z	 ddlm
Z
 dd	lmZ dd
lmZ ddlmZ ddlmZ ddlmZ erdZerdndZndZdZedddZedeed f ZedZejejkreej dddddZe Zde d< ddd d!d"Z!d#d#d d$d%Z"eej#d&f Z$G d'd dZ%G d(d& d&Z&d/dd)dd*d+d,Z'G d-d. d.Z(dS )0a  Logging control and utilities.

Control of logging for SA can be performed from the regular python logging
module.  The regular dotted module namespace is used, starting at
'sqlalchemy'.  For class-level logging, the class name is appended.

The "echo" keyword parameter, available on SQLA :class:`_engine.Engine`
and :class:`_pool.Pool` objects, corresponds to a logger specific to that
instance only.

    )annotationsN)Any)Optional)overload)Set)Type)TypeVar)Union   )py311)py38)LiteralT   F_IT
Identified)ZbounddebugZ
sqlalchemyzlogging.LoggerNone)loggerreturnc                 C  s*   t tj}|t d | | d S )Nz.%(asctime)s %(levelname)s %(name)s %(message)s)loggingZStreamHandlersysstdoutZsetFormatterZ	FormatterZ
addHandler)r   Zhandler r   4./venv/lib/python3.8/site-packages/sqlalchemy/log.py_add_default_handler:   s
    r   zSet[Type[Identified]]_logged_classesType[Identified]str)clsr   c                 C  s   t | dd p| jd | j S )NZ_sqla_logger_namespace.)getattr
__module____name__r   r   r   r   _qual_logger_name_for_clsE   s    r$   z	Type[_IT]c                   s>   t t|   fdd| _ fdd| _ | _t|  | S )Nc                   s     tjS N)isEnabledForr   DEBUGselfr   r   r   <lambda>N   s   zclass_logger.<locals>.<lambda>c                   s     tjS r%   )r&   r   INFOr(   r*   r   r   r+   Q   s   )r   	getLoggerr$   _should_log_debug_should_log_infor   r   addr#   r   r*   r   class_loggerL   s    
r1   InstanceLoggerc                   @  sJ   e Zd ZU dZdZded< ded< ded< d	d
ddZd	d
ddZdS )r   r   NzOptional[str]logging_name_IdentifiedLoggerTyper   _EchoFlagType_echoboolr   c                 C  s   | j tjS r%   )r   r&   r   r'   r(   r   r   r   r.   e   s    zIdentified._should_log_debugc                 C  s   | j tjS r%   )r   r&   r   r,   r(   r   r   r   r/   h   s    zIdentified._should_log_info)r"   r!   __qualname__	__slots__r3   __annotations__r.   r/   r   r   r   r   r   \   s   
c                   @  s   e Zd ZU dZejejejejdZde	d< dZ
ddddd	Zdd
d
ddddZdd
d
ddddZdd
d
ddddZeZdd
d
ddddZdd
d
ddddZdd
d
ddddZddd
d
ddddZddddd Zdd!d"d#Zd$S )%r2   a  A logger adapter (wrapper) for :class:`.Identified` subclasses.

    This allows multiple instances (e.g. Engine or Pool instances)
    to share a logger, but have its verbosity controlled on a
    per-instance basis.

    The basic functionality is to return a logging level
    which is based on an instance's echo setting.

    Default implementation is:

    'debug' -> logging.DEBUG
    True    -> logging.INFO
    False   -> Effective level of underlying logger (
    logging.WARNING by default)
    None    -> same as False
    )NFTr   r5   r6   )echor   r   )r<   namec                 C  s8   || _ t|| _| j| tjkr4| jjs4t| j d S r%   )r<   r   r-   r   	_echo_mapr,   Zhandlersr   )r)   r<   r=   r   r   r   __init__   s    zInstanceLogger.__init__r   r   )msgargskwargsr   c                 O  s   | j tj|f|| dS )z/Delegate a debug call to the underlying logger.N)logr   r'   r)   r@   rA   rB   r   r   r   r      s    zInstanceLogger.debugc                 O  s   | j tj|f|| dS )z/Delegate an info call to the underlying logger.N)rC   r   r,   rD   r   r   r   info   s    zInstanceLogger.infoc                 O  s   | j tj|f|| dS )z1Delegate a warning call to the underlying logger.N)rC   r   ZWARNINGrD   r   r   r   warning   s    zInstanceLogger.warningc                 O  s   | j tj|f|| dS )zB
        Delegate an error call to the underlying logger.
        NrC   r   ZERRORrD   r   r   r   error   s    zInstanceLogger.errorc                 O  s"   d|d< | j tj|f|| dS )z4Delegate an exception call to the underlying logger.r
   exc_infoNrG   rD   r   r   r   	exception   s    zInstanceLogger.exceptionc                 O  s   | j tj|f|| dS )z2Delegate a critical call to the underlying logger.N)rC   r   ZCRITICALrD   r   r   r   critical   s    zInstanceLogger.criticalint)levelr@   rA   rB   r   c                 O  sj   | j jj|krdS | j| j }|tjkr2| j  }||krftrR|	ddt
 |d< | j j|||f| dS )zDelegate a log call to the underlying logger.

        The level here is determined by the echo
        flag as well as that of the underlying logger, and
        logger._log() is called directly.

        N
stacklevelr
   )r   managerdisabler>   r<   r   NOTSETgetEffectiveLevel
STACKLEVELgetSTACKLEVEL_OFFSETZ_log)r)   rM   r@   rA   rB   Zselected_levelr   r   r   rC      s    

zInstanceLogger.logr7   )rM   r   c                 C  s   | j jj|krdS ||  kS )z)Is this logger enabled for level 'level'?F)r   rO   rP   rR   r)   rM   r   r   r   r&      s    zInstanceLogger.isEnabledForr8   c                 C  s$   | j | j }|tjkr | j }|S )z+What's the effective level for this logger?)r>   r<   r   rQ   r   rR   rV   r   r   r   rR      s    

z InstanceLogger.getEffectiveLevelN)r"   r!   r9   __doc__r   rQ   r,   r'   r>   r;   r:   r?   r   rE   rF   warnrH   rJ   rK   rC   r&   rR   r   r   r   r   r2   l   s&   
r5   )instanceechoflagr   c                 C  sT   | j rdt| j| j f }n
t| j}|| _|dkr@t|}n
t||}|| _dS )zEcreate a logger for an instance that implements :class:`.Identified`.z%s.%s)FNN)r3   r$   	__class__r6   r   r-   r2   r   )rY   rZ   r=   r   r   r   r   instance_logger   s    

r\   c                   @  s`   e Zd ZdZeddd dddZeddddd	dZd
dddddZddddddZdS )echo_propertya}      When ``True``, enable log output for this element.

    This has the effect of setting the Python logging level for the namespace
    of this element's class and object reference.  A value of boolean ``True``
    indicates that the loglevel ``logging.INFO`` will be set for the logger,
    whereas the string value ``debug`` will set the loglevel to
    ``logging.DEBUG``.
    zLiteral[None]r   )rY   ownerr   c                 C  s   d S r%   r   r)   rY   r^   r   r   r   __get__  s    zecho_property.__get__r   r5   c                 C  s   d S r%   r   r_   r   r   r   r`     s    zOptional[Identified]z#Union[echo_property, _EchoFlagType]c                 C  s   |d kr| S |j S d S r%   )r6   r_   r   r   r   r`     s    r   )rY   valuer   c                 C  s   t ||d d S )N)rZ   )r\   )r)   rY   ra   r   r   r   __set__  s    zecho_property.__set__N)r"   r!   r9   rW   r   r`   rb   r   r   r   r   r]     s   
r]   )N))rW   Z
__future__r   r   r   typingr   r   r   r   r   r   r	   utilr   r   Zutil.typingr   rS   rU   r   r7   r5   r-   Z
rootloggerrM   rQ   ZsetLevelZWARNr   setr   r;   r$   r1   ZLoggerr4   r   r2   r\   r]   r   r   r   r   <module>	   sB   
y 