σ
¨[c           @` s7  d  Z  d d l m Z m Z m 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 y d d l Z Wn e k
 r d Z n Xy d d l Z Wn e k
 rΏ d Z n Xe j d  Z e j d  Z e j d  Z d	   Z d
   Z d e j f d     YZ d d d  Z d d  Z d S(   sH  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.
i    (   t   absolute_importt   divisiont   print_functionN(   t   _unicode(   t   unicode_typet   basestring_types   tornado.accesss   tornado.applications   tornado.generalc           C` s   y t  t j d  r t j j   r t rP t j   t j d  d k r~ t Sq t r t j t	 t j
 d t    k r~ t Sq n  Wn t k
 r n Xt S(   Nt   isattyt   colorsi    t   wrapped_stderr(   t   hasattrt   syst   stderrR   t   cursest	   setuptermt   tigetnumt   Truet   coloramat   getattrt
   initialiset   objectt	   Exceptiont   False(    (    (    s*   lib/python2.7/site-packages/tornado/log.pyt   _stderr_supports_color7   s    !
c         C` s-   y t  |   SWn t k
 r( t |   SXd  S(   N(   R   t   UnicodeDecodeErrort   repr(   t   s(    (    s*   lib/python2.7/site-packages/tornado/log.pyt   _safe_unicodeI   s    t   LogFormatterc           B` si   e  Z d  Z d Z d Z i d e j 6d e j 6d e j 6d e j	 6Z
 e e d e e
 d  Z d	   Z RS(
   sp  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`.
    sV   %(color)s[%(levelname)1.1s %(asctime)s %(module)s:%(lineno)d]%(end_color)s %(message)ss   %y%m%d %H:%M:%Si   i   i   i   t   %c   	      C` s<  t  j j |  d | | |  _ i  |  _ | r/t   r/t d k	 rυ t j d  pd t j d  pd d } d t	 j
 k  o d k  n r t | d  } n  x< | j   D]. \ } } t t j | |  d  |  j | <q₯ Wt t j d	  d  |  _ q8x+ | j   D] \ } } d
 | |  j | <qWd |  _ n	 d |  _ d S(   sX  
        :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.
        t   datefmtt   setaft   setft    i   i    i   t   asciit   sgr0s   [2;3%dms   [0mN(   i   i    (   i   i   i   (   t   loggingt	   Formattert   __init__t   _fmtt   _colorsR   R   t   Nonet   tigetstrR
   t   version_infoR   t   itemst   tparmt   _normal(	   t   selft   fmtR   t   stylet   colorR   t   fg_colort   levelnot   code(    (    s*   lib/python2.7/site-packages/tornado/log.pyR%   r   s     		&c         C` sV  y4 | j    } t | t  s$ t  t |  | _ Wn) t k
 r_ } d | | j f | _ n X|  j | |  j	  | _
 | j |  j k r¬ |  j | j | _ |  j | _ n d | _ | _ |  j | j } | j rω | j sω |  j | j  | _ qω n  | j rF| j   g } | j d   | j j d  D  d j |  } n  | j d d  S(   Ns   Bad message (%r): %rR    c         s` s   |  ] } t  |  Vq d  S(   N(   R   (   t   .0t   ln(    (    s*   lib/python2.7/site-packages/tornado/log.pys	   <genexpr>Λ   s    s   
s   
    (   t
   getMessaget
   isinstanceR   t   AssertionErrorR   t   messageR   t   __dict__t
   formatTimeR   t   asctimeR3   R'   R1   R-   t	   end_colorR&   t   exc_infot   exc_textt   formatExceptiont   rstript   extendt   splitt   joint   replace(   R.   t   recordR:   t   et	   formattedt   lines(    (    s*   lib/python2.7/site-packages/tornado/log.pyt   format‘   s(    			#(   t   __name__t
   __module__t   __doc__t   DEFAULT_FORMATt   DEFAULT_DATE_FORMATR#   t   DEBUGt   INFOt   WARNINGt   ERRORt   DEFAULT_COLORSR   R%   RK   (    (    (    s*   lib/python2.7/site-packages/tornado/log.pyR   P   s   


.c      	   C` s¨  |  d k r' d d l } | j j }  n  |  j d k sK |  j j   d k rO d S| d k rj t j   } n  | j t t |  j j     |  j	 rV|  j
 } | d k rΤ t j j d |  j	 d |  j d |  j  } n\ | d k rt j j d |  j	 d	 |  j d
 |  j d |  j  } n d d | } t |   | j t d t   | j |  n  |  j sx|  j d k r€| j r€t j   } | j t    | j |  n  d S(   s¬   Turns on formatted logging output as configured.

    This is called automatically by `tornado.options.parse_command_line`
    and `tornado.options.parse_config_file`.
    i    Nt   nonet   sizet   filenamet   maxBytest   backupCountt   timet   whent   intervals.   The value of log_rotate_mode option should be s   "size" or "time", not "%s".R1   (   R(   t   tornado.optionst   optionsR#   t   lowert	   getLoggert   setLevelR   t   uppert   log_file_prefixt   log_rotate_modet   handlerst   RotatingFileHandlert   log_file_max_sizet   log_file_num_backupst   TimedRotatingFileHandlert   log_rotate_whent   log_rotate_intervalt
   ValueErrort   setFormatterR   R   t
   addHandlert   log_to_stderrt   StreamHandler(   R_   t   loggert   tornadot   rotate_modet   channelt   error_message(    (    s*   lib/python2.7/site-packages/tornado/log.pyt   enable_pretty_loggingΠ   s>    $								c      
   ` s?    d k r' d d l } | j j   n    j d d d d d d d	   j d
 d t d d d d   j d d t d d d d d d   j d d t d d" d d   j d d t d d d d   j d d t d d d d   j d d t d d d d   j d d t d d d d   j   f d     d S(#   sB  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.
    i    NR#   t   defaultt   infot   helpsS   Set the Python log level. If 'none', tornado won't touch the logging configuration.t   metavars   debug|info|warning|error|noneRp   t   types   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.Rd   t   PATHs₯   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)Rh   id   iθ  s%   max size of log files before rolloverRi   i
   s   number of log files to keepRk   t   midnightsc   specify the type of TimedRotatingFileHandler interval other options:('S', 'M', 'H', 'D', 'W0'-'W6')Rl   i   s$   The interval value of timed rotatingRe   RW   s(   The mode of rotating files(time or size)c           ` s
   t     S(   N(   Rw   (    (   R_   (    s*   lib/python2.7/site-packages/tornado/log.pyt   <lambda>"  s    i  i αυ(   R(   R^   R_   t   definet   boolt   strt   intt   add_parse_callback(   R_   Rs   (    (   R_   s*   lib/python2.7/site-packages/tornado/log.pyt   define_logging_optionsϊ   s*    	(   RN   t
   __future__R    R   R   R#   t   logging.handlersR
   t   tornado.escapeR   t   tornado.utilR   R   R   t   ImportErrorR(   R   Ra   t
   access_logt   app_logt   gen_logR   R   R$   R   Rw   R   (    (    (    s*   lib/python2.7/site-packages/tornado/log.pyt   <module>   s,   

		*