<s> """<STR_LIT>""" <EOL> from __future__ import absolute_import , division , print_function , with_statement <EOL> import logging <EOL> import logging . handlers <EOL> import sys <EOL> import time <EOL> from tornado . escape import _unicode <EOL> from tornado . util import unicode_type , basestring_type <EOL> try : <EOL> import curses <EOL> except ImportError : <EOL> curses = None <EOL> access_log = logging . getLogger ( "<STR_LIT>" ) <EOL> app_log = logging . getLogger ( "<STR_LIT>" ) <EOL> gen_log = logging . getLogger ( "<STR_LIT>" ) <EOL> def _stderr_supports_color ( ) : <EOL> color = False <EOL> if curses and sys . stderr . isatty ( ) : <EOL> try : <EOL> curses . setupterm ( ) <EOL> if curses . tigetnum ( "<STR_LIT>" ) > <NUM_LIT:0> : <EOL> color = True <EOL> except Exception : <EOL> pass <EOL> return color <EOL> class LogFormatter ( logging . Formatter ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , color = True , * args , ** kwargs ) : <EOL> logging . Formatter . __init__ ( self , * args , ** kwargs ) <EOL> self . _color = color and _stderr_supports_color ( ) <EOL> if self . _color : <EOL> fg_color = ( curses . tigetstr ( "<STR_LIT>" ) or <EOL> curses . tigetstr ( "<STR_LIT>" ) or "<STR_LIT>" ) <EOL> if ( <NUM_LIT:3> , <NUM_LIT:0> ) < sys . version_info < ( <NUM_LIT:3> , <NUM_LIT:2> , <NUM_LIT:3> ) : <EOL> fg_color = unicode_type ( fg_color , "<STR_LIT:ascii>" ) <EOL> self . _colors = { <EOL> logging . DEBUG : unicode_type ( curses . tparm ( fg_color , <NUM_LIT:4> ) , <EOL> "<STR_LIT:ascii>" ) , <EOL> logging . INFO : unicode_type ( curses . tparm ( fg_color , <NUM_LIT:2> ) , <EOL> "<STR_LIT:ascii>" ) , <EOL> logging . WARNING : unicode_type ( curses . tparm ( fg_color , <NUM_LIT:3> ) , <EOL> "<STR_LIT:ascii>" ) , <EOL> logging . ERROR : unicode_type ( curses . tparm ( fg_color , <NUM_LIT:1> ) , <EOL> "<STR_LIT:ascii>" ) , <EOL> } <EOL> self . _normal = unicode_type ( curses . tigetstr ( "<STR_LIT>" ) , "<STR_LIT:ascii>" ) <EOL> def format ( self , record ) : <EOL> try : <EOL> record . message = record . getMessage ( ) <EOL> except Exception as e : <EOL> record . message = "<STR_LIT>" % ( e , record . __dict__ ) <EOL> assert isinstance ( record . message , basestring_type ) <EOL> record . asctime = time . strftime ( <EOL> "<STR_LIT>" , self . converter ( record . created ) ) <EOL> prefix = '<STR_LIT>' % record . __dict__ <EOL> if self . _color : <EOL> prefix = ( self . _colors . get ( record . levelno , self . _normal ) + <EOL> prefix + self . _normal ) <EOL> def safe_unicode ( s ) : <EOL> try : <EOL> return _unicode ( s ) <EOL> except UnicodeDecodeError : <EOL> return repr ( s ) <EOL> formatted = prefix + "<STR_LIT:U+0020>" + safe_unicode ( record . message ) <EOL> if record . exc_info : <EOL> if not record . exc_text : <EOL> record . exc_text = self . formatException ( record . exc_info ) <EOL> if record . exc_text : <EOL> lines = [ formatted . rstrip ( ) ] <EOL> lines . extend ( safe_unicode ( ln ) for ln in record . exc_text . split ( '<STR_LIT:\n>' ) ) <EOL> formatted = '<STR_LIT:\n>' . join ( lines ) <EOL> return formatted . replace ( "<STR_LIT:\n>" , "<STR_LIT>" ) <EOL> def enable_pretty_logging ( options = None , logger = None ) : <EOL> """<STR_LIT>""" <EOL> if options is None : <EOL> from tornado . options import options <EOL> if options . logging == '<STR_LIT:none>' : <EOL> return <EOL> if logger is None : <EOL> logger = logging . getLogger ( ) <EOL> logger . setLevel ( getattr ( logging , options . logging . upper ( ) ) ) <EOL> if options . log_file_prefix : <EOL> channel = logging . handlers . RotatingFileHandler ( <EOL> filename = options . log_file_prefix , <EOL> maxBytes = options . log_file_max_size , <EOL> backupCount = options . log_file_num_backups ) <EOL> channel . setFormatter ( LogFormatter ( color = False ) ) <EOL> logger . addHandler ( channel ) <EOL> if ( options . log_to_stderr or <EOL> ( options . log_to_stderr is None and not logger . handlers ) ) : <EOL> channel = logging . StreamHandler ( ) <EOL> channel . setFormatter ( LogFormatter ( ) ) <EOL> logger . addHandler ( channel ) <EOL> def define_logging_options ( options = None ) : <EOL> if options is None : <EOL> from tornado . options import options <EOL> options . define ( "<STR_LIT>" , default = "<STR_LIT:info>" , <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" ) , <EOL> metavar = "<STR_LIT>" ) <EOL> options . define ( "<STR_LIT>" , type = bool , default = None , <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> options . define ( "<STR_LIT>" , type = str , default = None , metavar = "<STR_LIT>" , <EOL> help = ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) ) <EOL> options . define ( "<STR_LIT>" , type = int , default = <NUM_LIT:100> * <NUM_LIT:1000> * <NUM_LIT:1000> , <EOL> help = "<STR_LIT>" ) <EOL> options . define ( "<STR_LIT>" , type = int , default = <NUM_LIT:10> , <EOL> help = "<STR_LIT>" ) <EOL> options . add_parse_callback ( enable_pretty_logging ) </s>
