<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import <EOL> import logging <EOL> import socket <EOL> import time <EOL> import simplejson <EOL> from twisted . python import reflect <EOL> from twisted . python . failure import Failure <EOL> MAX_TRIMMED_MESSAGE_SIZE = <NUM_LIT:200> <EOL> DEFAULT_HOST = "<STR_LIT:127.0.0.1>" <EOL> DEFAULT_PORT = <NUM_LIT> <EOL> __emptyLogRecord = logging . LogRecord ( None , None , None , None , None , None , None ) <EOL> _DEFAULT_LOGGING_ATTRIBUTES = [ '<STR_LIT:message>' , '<STR_LIT>' ] <EOL> _DEFAULT_LOGGING_ATTRIBUTES . extend ( vars ( __emptyLogRecord ) . keys ( ) ) <EOL> del __emptyLogRecord <EOL> def unserialize ( msg ) : <EOL> """<STR_LIT>""" <EOL> category , data = msg . split ( '<STR_LIT::>' , <NUM_LIT:1> ) <EOL> return category , simplejson . loads ( data ) <EOL> def augmentWithFailure ( eventDict , failure , why = None ) : <EOL> """<STR_LIT>""" <EOL> eventDict [ '<STR_LIT>' ] = failure . getTraceback ( ) <EOL> eventDict [ '<STR_LIT>' ] = reflect . qual ( failure . type ) <EOL> eventDict [ '<STR_LIT>' ] = reflect . safe_str ( failure . value ) <EOL> eventDict . setdefault ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> eventDict [ '<STR_LIT:message>' ] = ( why or <EOL> eventDict [ '<STR_LIT>' ] or <EOL> eventDict [ '<STR_LIT>' ] ) <EOL> class MemoryLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> self . logged = [ ] <EOL> def log ( self , category , eventDict ) : <EOL> self . logged . append ( ( category , eventDict ) ) <EOL> class UDPLogger ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , host = DEFAULT_HOST , port = DEFAULT_PORT , <EOL> defaultFields = None ) : <EOL> self . socket = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> self . socket . connect ( ( host , port ) ) <EOL> self . defaultFields = defaultFields or { } <EOL> def augment ( self , eventDict ) : <EOL> """<STR_LIT>""" <EOL> eventDict . setdefault ( '<STR_LIT>' , time . time ( ) ) <EOL> for key , value in self . defaultFields . iteritems ( ) : <EOL> eventDict . setdefault ( key , value ) <EOL> def serialize ( self , category , eventDict ) : <EOL> """<STR_LIT>""" <EOL> msg = simplejson . dumps ( eventDict , <EOL> default = lambda x : str ( repr ( x ) ) , <EOL> skipkeys = True ) <EOL> return "<STR_LIT>" % ( category , msg ) <EOL> def serializeFailure ( self , category , eventDict , size , failure , why ) : <EOL> """<STR_LIT>""" <EOL> newEventDict = { <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { <EOL> '<STR_LIT>' : category , <EOL> '<STR_LIT>' : eventDict [ '<STR_LIT>' ] , <EOL> } , <EOL> } <EOL> augmentWithFailure ( newEventDict , failure , why ) <EOL> if '<STR_LIT:message>' in eventDict : <EOL> text = eventDict [ '<STR_LIT:message>' ] <EOL> if len ( text ) > MAX_TRIMMED_MESSAGE_SIZE : <EOL> newEventDict [ '<STR_LIT>' ] = len ( text ) <EOL> text = text [ : MAX_TRIMMED_MESSAGE_SIZE - <NUM_LIT:4> ] + '<STR_LIT>' <EOL> newEventDict [ '<STR_LIT>' ] [ '<STR_LIT:message>' ] = text <EOL> for key in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT:filename>' , '<STR_LIT>' ) : <EOL> if key in eventDict : <EOL> newEventDict [ '<STR_LIT>' ] [ key ] = eventDict [ key ] <EOL> newEventDict [ '<STR_LIT>' ] = size <EOL> self . augment ( newEventDict ) <EOL> return self . serialize ( '<STR_LIT>' , newEventDict ) <EOL> def log ( self , category , eventDict ) : <EOL> """<STR_LIT>""" <EOL> self . augment ( eventDict ) <EOL> data = self . serialize ( category , eventDict ) <EOL> try : <EOL> self . socket . send ( data ) <EOL> except : <EOL> failure = Failure ( ) <EOL> why = "<STR_LIT>" <EOL> data = self . serializeFailure ( category , eventDict , len ( data ) , <EOL> failure , why ) <EOL> try : <EOL> self . socket . send ( data ) <EOL> except Exception : <EOL> import sys <EOL> text = why + '<STR_LIT:\n>' + failure . getBriefTraceback ( ) <EOL> print >> sys . stderr , text <EOL> class UDPLogHandler ( logging . Handler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , logger , category = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> logging . Handler . __init__ ( self ) <EOL> self . setFormatter ( logging . Formatter ( ) ) <EOL> self . logger = logger <EOL> self . category = category <EOL> def emit ( self , record ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> eventDict = { <EOL> '<STR_LIT>' : self . category , <EOL> '<STR_LIT>' : record . levelname , <EOL> '<STR_LIT>' : record . name , <EOL> '<STR_LIT:filename>' : record . pathname , <EOL> '<STR_LIT>' : record . lineno , <EOL> '<STR_LIT>' : record . funcName , <EOL> '<STR_LIT>' : record . created , <EOL> } <EOL> if isinstance ( record . args , dict ) : <EOL> eventDict . update ( record . args ) <EOL> extra = { name : value for name , value in vars ( record ) . iteritems ( ) <EOL> if name not in _DEFAULT_LOGGING_ATTRIBUTES } <EOL> eventDict . update ( extra ) <EOL> self . format ( record ) <EOL> eventDict [ '<STR_LIT:message>' ] = record . message <EOL> if record . exc_info : <EOL> excType , excValue = record . exc_info [ <NUM_LIT:0> : <NUM_LIT:2> ] <EOL> eventDict [ '<STR_LIT>' ] = reflect . safe_str ( excValue ) <EOL> if excValue is None : <EOL> eventDict [ '<STR_LIT>' ] = None <EOL> eventDict [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> else : <EOL> eventDict [ '<STR_LIT>' ] = record . exc_text <EOL> eventDict [ '<STR_LIT>' ] = reflect . qual ( excType ) <EOL> category = eventDict [ '<STR_LIT>' ] <EOL> del eventDict [ '<STR_LIT>' ] <EOL> self . logger . log ( category , eventDict ) <EOL> except ( KeyboardInterrupt , SystemExit ) : <EOL> raise <EOL> except : <EOL> self . handleError ( record ) <EOL> class ConfigurableUDPLogHandler ( UDPLogHandler ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , defaultFields = None , category = '<STR_LIT>' , <EOL> host = DEFAULT_HOST , port = DEFAULT_PORT , <EOL> includeHostname = True ) : <EOL> """<STR_LIT>""" <EOL> defaultFields = defaultFields or { } <EOL> if includeHostname : <EOL> defaultFields . setdefault ( '<STR_LIT>' , socket . gethostname ( ) ) <EOL> logger = UDPLogger ( host = host , port = port , defaultFields = defaultFields ) <EOL> UDPLogHandler . __init__ ( self , logger , category ) <EOL> def main ( ) : <EOL> sock = socket . socket ( socket . AF_INET , socket . SOCK_DGRAM ) <EOL> sock . bind ( ( DEFAULT_HOST , DEFAULT_PORT ) ) <EOL> while True : <EOL> data , addr = sock . recvfrom ( <NUM_LIT> ) <EOL> print data . rstrip ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> main ( ) </s>
