<s> import time <EOL> from sys import stdout , stderr <EOL> from zope . interface import implements <EOL> from twisted . python . log import startLoggingWithObserver , textFromEventDict , msg , err , ILogObserver <EOL> from twisted . python . syslog import SyslogObserver <EOL> from twisted . python . logfile import DailyLogFile <EOL> class CarbonLogObserver ( object ) : <EOL> implements ( ILogObserver ) <EOL> def log_to_dir ( self , logdir ) : <EOL> self . logdir = logdir <EOL> self . console_logfile = DailyLogFile ( '<STR_LIT>' , logdir ) <EOL> self . custom_logs = { } <EOL> self . observer = self . logdir_observer <EOL> def log_to_syslog ( self , prefix ) : <EOL> observer = SyslogObserver ( prefix ) . emit <EOL> def syslog_observer ( event ) : <EOL> event [ "<STR_LIT>" ] = event . get ( "<STR_LIT:type>" , "<STR_LIT>" ) <EOL> observer ( event ) <EOL> self . observer = syslog_observer <EOL> def __call__ ( self , event ) : <EOL> return self . observer ( event ) <EOL> def stdout_observer ( self , event ) : <EOL> stdout . write ( formatEvent ( event , includeType = True ) + '<STR_LIT:\n>' ) <EOL> stdout . flush ( ) <EOL> def logdir_observer ( self , event ) : <EOL> message = formatEvent ( event ) <EOL> log_type = event . get ( '<STR_LIT:type>' ) <EOL> if log_type is not None and log_type not in self . custom_logs : <EOL> self . custom_logs [ log_type ] = DailyLogFile ( log_type + '<STR_LIT>' , self . logdir ) <EOL> logfile = self . custom_logs . get ( log_type , self . console_logfile ) <EOL> logfile . write ( message + '<STR_LIT:\n>' ) <EOL> logfile . flush ( ) <EOL> observer = stdout_observer <EOL> carbonLogObserver = CarbonLogObserver ( ) <EOL> def formatEvent ( event , includeType = False ) : <EOL> event [ '<STR_LIT>' ] = '<STR_LIT>' in event <EOL> message = textFromEventDict ( event ) <EOL> if includeType : <EOL> typeTag = '<STR_LIT>' % event . get ( '<STR_LIT:type>' , '<STR_LIT>' ) <EOL> else : <EOL> typeTag = '<STR_LIT>' <EOL> timestamp = time . strftime ( "<STR_LIT>" ) <EOL> return "<STR_LIT>" % ( timestamp , typeTag , message ) <EOL> logToDir = carbonLogObserver . log_to_dir <EOL> logToSyslog = carbonLogObserver . log_to_syslog <EOL> def logToStdout ( ) : <EOL> startLoggingWithObserver ( carbonLogObserver ) <EOL> def cache ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def clients ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def creates ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def updates ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def listener ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def relay ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def aggregator ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def query ( message , ** context ) : <EOL> context [ '<STR_LIT:type>' ] = '<STR_LIT>' <EOL> msg ( message , ** context ) <EOL> def debug ( message , ** context ) : <EOL> if debugEnabled : <EOL> msg ( message , ** context ) <EOL> debugEnabled = False <EOL> def setDebugEnabled ( enabled ) : <EOL> global debugEnabled <EOL> debugEnabled = enabled </s>
