U
    
W[8                  
   @   s   d Z ddlZddlmZ ddlmZmZmZ ddl	m
Z
 ddlmZ ddlm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
 Ze ZeeG dd deZG dd deZdS )z3
Integration with Python standard library logging.
    N)implementer)_PY3currentframeunicode   )LogLevel)formatEvent)ILogObserverc                  C   s0   i } t  D ]\}}|| |< || t|< q| S )z
    Reverse the above mapping, adding both the numerical keys used above and
    the corresponding string keys also used by python logging.
    @return: the reversed mapping
    )toStdlibLogLevelMappingitemsstdlibLoggingZgetLevelName)mappingZlogLevelZ
pyLogLevel r   8/usr/lib/python3/dist-packages/twisted/logger/_stdlib.py_reverseLogLevelMapping   s
    r   c                   @   s4   e Zd ZdZdZdefddZdddZd	d
 ZdS )STDLibLogObserverae  
    Log observer that writes to the python standard library's C{logging}
    module.

    @note: Warning: specific logging configurations (example: network) can lead
        to this observer blocking.  Nothing is done here to prevent that, so be
        sure to not to configure the standard library logging module to block
        when used in conjunction with this module: code within Twisted, such as
        twisted.web, assumes that logging does not block.

    @cvar defaultStackDepth: This is the default number of frames that it takes
        to get from L{STDLibLogObserver} through the logging module, plus one;
        in other words, the number of frames if you were to call a
        L{STDLibLogObserver} directly.  This is useful to use as an offset for
        the C{stackDepth} parameter to C{__init__}, to add frames for other
        publishers.
       Ztwistedc                 C   s    t || _| j| j_|| _dS )z
        @param loggerName: logger identifier.
        @type loggerName: C{str}

        @param stackDepth: The depth of the stack to investigate for caller
            metadata.
        @type stackDepth: L{int}
        N)r   Z	getLoggerlogger_findCallerZ
findCaller
stackDepth)selfnamer   r   r   r   __init__C   s    	
zSTDLibLogObserver.__init__Fc                 C   s2   t | j}|j}trd}nd}|j|j|jf| S )a  
        Based on the stack depth passed to this L{STDLibLogObserver}, identify
        the calling function.

        @param stackInfo: Whether or not to construct stack information.
            (Currently ignored.)
        @type stackInfo: L{bool}

        @return: Depending on Python version, either a 3-tuple of (filename,
            lineno, name) or a 4-tuple of that plus stack information.
        @rtype: L{tuple}
        Nr   )r   r   f_coder   co_filenamef_linenoco_name)r   Z	stackInfofcoZextrar   r   r   r   Q   s    
zSTDLibLogObserver._findCallerc                 C   s`   | dtj}| d}|dkr&d}n|j|j| f}t |tj}| j	j
|t||d dS )zB
        Format an event and bridge it to Python logging.
        Z	log_levelZlog_failureN)exc_info)getr   infotypevalueZgetTracebackObjectr
   r   INFOr   logStringifiableFromEvent)r   eventlevelZfailureZexcInfoZstdlibLevelr   r   r   __call__g   s    
    zSTDLibLogObserver.__call__N)F)__name__
__module____qualname____doc__ZdefaultStackDepthr   r   r*   r   r   r   r   r   -   s
   
r   c                   @   s6   e Zd ZdZdd Zdd Zdd Zer.eZneZdS )	r'   z
    An object that implements C{__str__()} in order to defer the work of
    formatting until it's converted into a C{str}.
    c                 C   s
   || _ dS )zF
        @param event: An event.
        @type event: L{dict}
        N)r(   )r   r(   r   r   r   r   }   s    zStringifiableFromEvent.__init__c                 C   s
   t | jS r   )r   r(   r   r   r   r   __unicode__   s    z"StringifiableFromEvent.__unicode__c                 C   s   t | dS )Nzutf-8)r   encoder/   r   r   r   	__bytes__   s    z StringifiableFromEvent.__bytes__N)	r+   r,   r-   r.   r   r0   r2   r   __str__r   r   r   r   r'   x   s   r'   )r.   Zloggingr   Zzope.interfacer   Ztwisted.python.compatr   r   r   Z_levelsr   Z_formatr   Z	_observerr	   debugDEBUGr"   r%   warnZWARNINGerrorZERRORZcriticalZCRITICALr
   r   ZfromStdlibLogLevelMappingobjectr   r'   r   r   r   r   <module>   s,        J