<s> """<STR_LIT>""" <EOL> from zope . interface import implementer <EOL> from twisted . python . compat import ioType , unicode <EOL> from . _observer import ILogObserver <EOL> from . _format import formatTime <EOL> from . _format import timeFormatRFC3339 <EOL> from . _format import formatEventAsClassicLogText <EOL> @ implementer ( ILogObserver ) <EOL> class FileLogObserver ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , outFile , formatEvent ) : <EOL> """<STR_LIT>""" <EOL> if ioType ( outFile ) is not unicode : <EOL> self . _encoding = "<STR_LIT:utf-8>" <EOL> else : <EOL> self . _encoding = None <EOL> self . _outFile = outFile <EOL> self . formatEvent = formatEvent <EOL> def __call__ ( self , event ) : <EOL> """<STR_LIT>""" <EOL> text = self . formatEvent ( event ) <EOL> if text is None : <EOL> text = u"<STR_LIT>" <EOL> if "<STR_LIT>" in event : <EOL> try : <EOL> traceback = event [ "<STR_LIT>" ] . getTraceback ( ) <EOL> except Exception : <EOL> traceback = u"<STR_LIT>" <EOL> text = u"<STR_LIT:\n>" . join ( ( text , traceback ) ) <EOL> if self . _encoding is not None : <EOL> text = text . encode ( self . _encoding ) <EOL> if text : <EOL> self . _outFile . write ( text ) <EOL> self . _outFile . flush ( ) <EOL> def textFileLogObserver ( outFile , timeFormat = timeFormatRFC3339 ) : <EOL> """<STR_LIT>""" <EOL> def formatEvent ( event ) : <EOL> return formatEventAsClassicLogText ( <EOL> event , formatTime = lambda e : formatTime ( e , timeFormat ) <EOL> ) <EOL> return FileLogObserver ( outFile , formatEvent ) </s>
