o
    Qœ_~0  ã                   @   s   d Z ddlZddlZddlZddlmZ ddlmZmZ zddl	Z	W n e
y-   dZ	Y nw zddlZW n e
y?   dZY nw ddlmZmZmZmZ e d¡Ze d¡Ze d¡Zd	efd
d„Zded	efdd„ZG dd„ dejƒZ	ddedeej d	dfdd„Zdded	dfdd„ZdS )aH  Logging support for Tornado.

Tornado uses three logger streams:

* ``tornado.access``: Per-request logging for Tornado's HTTP servers (and
  potentially other servers in the future)
* ``tornado.application``: Logging of errors from application code (i.e.
  uncaught exceptions from callbacks)
* ``tornado.general``: General-purpose logging, including any errors
  or warnings from Tornado itself.

These streams may be configured independently using the standard library's
`logging` module.  For example, you may wish to send ``tornado.access`` logs
to a separate file for analysis.
é    N)Ú_unicode)Úunicode_typeÚbasestring_type)ÚDictÚAnyÚcastÚOptionalztornado.accessztornado.applicationztornado.generalÚreturnc                   C   sŠ   z:t tjdƒr/tj ¡ r2trt ¡  t d¡dkrW dS W dS tr5tjttj	dt
ƒ ƒu r8W dS W dS W dS W dS W dS  tyD   Y dS w )NÚisattyÚcolorsr   TÚwrapped_stderrF)ÚhasattrÚsysÚstderrr
   ÚcursesÚ	setuptermÚtigetnumÚcoloramaÚgetattrÚ
initialiseÚobjectÚ	Exception© r   r   ú-/usr/lib/python3/dist-packages/tornado/log.pyÚ_stderr_supports_color7   s2   ÿ÷
ÿ÷ò÷	øüür   Úsc                 C   s&   zt | ƒW S  ty   t| ƒ Y S w ©N)r   ÚUnicodeDecodeErrorÚrepr)r   r   r   r   Ú_safe_unicodeJ   s
   
ÿr   c                   @   s‚   e Zd ZdZdZdZejdejdej	dej
dejdiZeed	d
efdededededeeef ddfdd„Zdedefdd„ZdS )ÚLogFormatterap  Log formatter used in Tornado.

    Key features of this formatter are:

    * Color support when logging to a terminal that supports it.
    * Timestamps on every log line.
    * Robust against str/bytes encoding problems.

    This formatter is enabled automatically by
    `tornado.options.parse_command_line` or `tornado.options.parse_config_file`
    (unless ``--logging=none`` is used).

    Color support on Windows versions that do not support ANSI color codes is
    enabled by use of the colorama__ library. Applications that wish to use
    this must first initialize colorama with a call to ``colorama.init``.
    See the colorama documentation for details.

    __ https://pypi.python.org/pypi/colorama

    .. versionchanged:: 4.5
       Added support for ``colorama``. Changed the constructor
       signature to be compatible with `logging.config.dictConfig`.
    zV%(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)sz%y%m%d %H:%M:%Sé   é   é   é   é   ú%TÚfmtÚdatefmtÚstyleÚcolorr   r	   Nc           	      C   sº   t jj| |d� || _i | _|rXtƒ rXtdurCt d¡p"t d¡p"d}| ¡ D ]\}}t	t 
||¡dƒ| j|< q't	t d¡dƒ| _dS | ¡ D ]\}}d| | j|< qGd	| _dS d
| _dS )aX  
        :arg bool color: Enables color support.
        :arg str fmt: Log message format.
          It will be applied to the attributes dict of log records. The
          text between ``%(color)s`` and ``%(end_color)s`` will be colored
          depending on the level if color support is on.
        :arg dict colors: color mappings from logging level to terminal color
          code
        :arg str datefmt: Datetime format.
          Used for formatting ``(asctime)`` placeholder in ``prefix_fmt``.

        .. versionchanged:: 3.2

           Added ``fmt`` and ``datefmt`` arguments.
        )r(   NÚsetafÚsetfó    ÚasciiÚsgr0z[2;3%dmz[0mÚ )ÚloggingÚ	FormatterÚ__init__Ú_fmtÚ_colorsr   r   ÚtigetstrÚitemsr   ÚtparmÚ_normal)	Úselfr'   r(   r)   r*   r   Úfg_colorÚlevelnoÚcoder   r   r   r3   t   s   
ÿ

zLogFormatter.__init__Úrecordc              
   C   s  z|  ¡ }t|tƒsJ ‚t|ƒ|_W n ty, } zd||jf |_W Y d }~nd }~ww |  |tt	| j
ƒ¡|_|j| jv rJ| j|j |_| j|_nd |_|_| j|j }|jrc|jsc|  |j¡|_|jr~| ¡ g}| dd„ |j d¡D ƒ¡ d |¡}| dd¡S )NzBad message (%r): %rr0   c                 s   s   � | ]}t |ƒV  qd S r   )r   )Ú.0Úlnr   r   r   Ú	<genexpr>Î   s   € z&LogFormatter.format.<locals>.<genexpr>Ú
z
    )Ú
getMessageÚ
isinstancer   r   Úmessager   Ú__dict__Ú
formatTimer   Ústrr(   Úasctimer<   r5   r*   r9   Ú	end_colorr4   Úexc_infoÚexc_textÚformatExceptionÚrstripÚextendÚsplitÚjoinÚreplace)r:   r>   rE   ÚeÚ	formattedÚlinesr   r   r   Úformat¤   s,   €ÿ


zLogFormatter.format)Ú__name__Ú
__module__Ú__qualname__Ú__doc__ÚDEFAULT_FORMATÚDEFAULT_DATE_FORMATr1   ÚDEBUGÚINFOÚWARNINGÚERRORÚCRITICALÚDEFAULT_COLORSrH   Úboolr   Úintr3   r   rV   r   r   r   r   r    Q   s:    û
úþýüû
ú
ù0r    ÚoptionsÚloggerc                 C   s   | du rddl }|jj} | jdu s| j ¡ dkrdS |du r"t ¡ }| tt| j ¡ ƒ¡ | jrp| j	}|dkrEtj
j| j| j| jdd�}n|dkrYtj
j| j| j| j| jdd�}n
d	d
|  }t|ƒ‚| tdd�¡ | |¡ | js{| jdu rŒ|j
sŽt ¡ }| tƒ ¡ | |¡ dS dS dS )z¬Turns on formatted logging output as configured.

    This is called automatically by `tornado.options.parse_command_line`
    and `tornado.options.parse_config_file`.
    Nr   ÚnoneÚsizezutf-8)ÚfilenameÚmaxBytesÚbackupCountÚencodingÚtime)ri   ÚwhenÚintervalrk   rl   z.The value of log_rotate_mode option should be z"size" or "time", not "%s".F)r*   )Útornado.optionsre   r1   ÚlowerÚ	getLoggerÚsetLevelr   ÚupperÚlog_file_prefixÚlog_rotate_modeÚhandlersÚRotatingFileHandlerÚlog_file_max_sizeÚlog_file_num_backupsÚTimedRotatingFileHandlerÚlog_rotate_whenÚlog_rotate_intervalÚ
ValueErrorÚsetFormatterr    Ú
addHandlerÚlog_to_stderrÚStreamHandler)re   rf   ÚtornadoÚrotate_modeÚchannelÚerror_messager   r   r   Úenable_pretty_loggingÓ   sJ   üû	ÿÿ
ür‡   c                    sÀ   ˆ du rddl }|jj‰ ˆ jddddd� ˆ jdtdd	d
� ˆ jdtdddd� ˆ jdtddd
� ˆ jdtddd
� ˆ jdtddd
� ˆ jdtddd
� ˆ jdtddd
� ˆ  ‡ fdd„¡ dS ) aB  Add logging-related flags to ``options``.

    These options are present automatically on the default options instance;
    this method is only necessary if you have created your own `.OptionParser`.

    .. versionadded:: 4.2
        This function existed in prior versions but was broken and undocumented until 4.2.
    Nr   r1   ÚinfozSSet the Python log level. If 'none', tornado won't touch the logging configuration.zdebug|info|warning|error|none)ÚdefaultÚhelpÚmetavarr�   zŒSend log output to stderr (colorized if possible). By default use stderr if --log_file_prefix is not set and no other logging is configured.)Útyper‰   rŠ   ru   ÚPATHz¥Path prefix for log files. Note that if you are running multiple tornado processes, log_file_prefix must be different for each of them (e.g. include the port number))rŒ   r‰   r‹   rŠ   ry   i áõz%max size of log files before rolloverrz   é
   znumber of log files to keepr|   Úmidnightzcspecify the type of TimedRotatingFileHandler interval other options:('S', 'M', 'H', 'D', 'W0'-'W6')r}   r$   z$The interval value of timed rotatingrv   rh   z(The mode of rotating files(time or size)c                      s   t ˆ ƒS r   )r‡   r   ©re   r   r   Ú<lambda>S  s    z(define_logging_options.<locals>.<lambda>)rp   re   Údefinerc   rH   rd   Úadd_parse_callback)re   rƒ   r   r�   r   Údefine_logging_options  sd   	ù	û
úüÿû	üür”   )NNr   )rZ   r1   Úlogging.handlersr   Útornado.escaper   Útornado.utilr   r   r   ÚImportErrorr   Útypingr   r   r   r   rr   Ú
access_logÚapp_logÚgen_logrc   r   rH   r   r2   r    ÚLoggerr‡   r”   r   r   r   r   Ú<module>   sB   ÿÿ


 ÿÿÿ
þ2