o
    )Zh1                     @   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   TZwrapped_stderrF)hasattrsysstderrr
   cursesZ	setuptermZtigetnumcoloramagetattrZ
initialiseobject	Exception r   r   B/var/www/html/lang_env/lib/python3.10/site-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 | _|rct rctdurNtdp"tdp"d}| D ]\}}t	t
||d| j|< q'td}	|	durIt	|	d| _dS d| _dS | D ]\}}d	| | j|< qRd
| _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$   NZsetafZsetf    asciiZsgr0 z[2;3%dmz[0m)logging	Formatter__init___fmt_colorsr   r   Ztigetstritemsr   Ztparm_normal)
selfr#   r$   r%   r&   r   Zfg_colorlevelnocodenormalr   r   r   r,   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): %rr)   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$   asctimer2   r.   r&   r0   Z	end_colorr-   exc_infoexc_textformatExceptionrstripextendsplitjoinreplace)r1   r5   r<   e	formattedlinesr   r   r   format   s,   


zLogFormatter.format)__name__
__module____qualname____doc__DEFAULT_FORMATZDEFAULT_DATE_FORMATr*   DEBUGINFOWARNINGERRORCRITICALZDEFAULT_COLORSr?   boolr   intr,   r   rL   r   r   r   r   r   Q   s:    


4r   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
st }|t  || dS dS dS )zTurns 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)r]   whenintervalr_   r`   z.The value of log_rotate_mode option should be z"size" or "time", not "%s".F)r&   )tornado.optionsrY   r*   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)rY   rZ   tornadoZrotate_modeZchannelerror_messager   r   r   enable_pretty_logging   sJ   	
ry   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   r*   infozSSet the Python log level. If 'none', tornado won't touch the logging configuration.zdebug|info|warning|error|none)defaulthelpmetavarru   zSend 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|   ri   PATHzPath 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|   rm   i z%max size of log files before rolloverrn   
   znumber of log files to keeprp   Zmidnightzcspecify the type of TimedRotatingFileHandler interval other options:('S', 'M', 'H', 'D', 'W0'-'W6')rq   r    z$The interval value of timed rotatingrj   r\   z(The mode of rotating files(time or size)c                      s   t  S r   )ry   r   rY   r   r   <lambda>W  s    z(define_logging_options.<locals>.<lambda>)rd   rY   definerW   r?   rX   Zadd_parse_callback)rY   rw   r   r   r   define_logging_options	  sd   		
	r   )NNr   )rP   r*   logging.handlersr   Ztornado.escaper   Ztornado.utilr   r   r   ImportErrorr   typingr   r   r   r   rf   Z
access_logZapp_logZgen_logrW   r   r?   r   r+   r   Loggerry   r   r   r   r   r   <module>   sB   


 
2