<s> """<STR_LIT>""" <EOL> import os <EOL> import sys <EOL> import datetime <EOL> from io import IOBase <EOL> from circuits . six import string_types , text_type <EOL> from circuits . core import handler , BaseComponent <EOL> def formattime ( ) : <EOL> return datetime . datetime . now ( ) . strftime ( '<STR_LIT>' ) <EOL> class Logger ( BaseComponent ) : <EOL> channel = "<STR_LIT>" <EOL> format = '<STR_LIT>' <EOL> def __init__ ( self , file = None , logger = None , ** kwargs ) : <EOL> super ( Logger , self ) . __init__ ( ** kwargs ) <EOL> if isinstance ( file , string_types ) : <EOL> self . file = open ( os . path . abspath ( os . path . expanduser ( file ) ) , "<STR_LIT:a>" ) <EOL> elif isinstance ( file , IOBase ) or hasattr ( file , "<STR_LIT>" ) : <EOL> self . file = file <EOL> else : <EOL> self . file = sys . stdout <EOL> self . logger = logger <EOL> @ handler ( "<STR_LIT>" ) <EOL> def log_response ( self , response_event , value ) : <EOL> response = response_event . args [ <NUM_LIT:0> ] <EOL> self . log ( response ) <EOL> def log ( self , response ) : <EOL> request = response . request <EOL> remote = request . remote <EOL> outheaders = response . headers <EOL> inheaders = request . headers or { } <EOL> protocol = "<STR_LIT>" % request . protocol <EOL> host = inheaders . get ( "<STR_LIT>" , ( remote . name or remote . ip ) ) <EOL> atoms = { "<STR_LIT:h>" : host , <EOL> "<STR_LIT:l>" : "<STR_LIT:->" , <EOL> "<STR_LIT:u>" : getattr ( request , "<STR_LIT>" , None ) or "<STR_LIT:->" , <EOL> "<STR_LIT:t>" : formattime ( ) , <EOL> "<STR_LIT:r>" : "<STR_LIT>" % ( request . method , request . path , protocol ) , <EOL> "<STR_LIT:s>" : int ( response . status ) , <EOL> "<STR_LIT:b>" : outheaders . get ( "<STR_LIT>" , "<STR_LIT>" ) or "<STR_LIT:->" , <EOL> "<STR_LIT:f>" : inheaders . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> "<STR_LIT:a>" : inheaders . get ( "<STR_LIT>" , "<STR_LIT>" ) , <EOL> } <EOL> for k , v in list ( atoms . items ( ) ) : <EOL> if isinstance ( v , text_type ) : <EOL> v = v . encode ( "<STR_LIT:utf8>" ) <EOL> elif not isinstance ( v , str ) : <EOL> v = str ( v ) <EOL> v = repr ( v ) [ <NUM_LIT:1> : - <NUM_LIT:1> ] <EOL> atoms [ k ] = v . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> if self . logger is not None : <EOL> self . logger . info ( self . format % atoms ) <EOL> else : <EOL> self . file . write ( self . format % atoms ) <EOL> self . file . write ( "<STR_LIT:\n>" ) <EOL> self . file . flush ( ) </s>
