我们从Python开源项目中,提取了以下20个代码示例,用于说明如何使用twisted.python.log.FileLogObserver()。
def setUp(self): """ Add a log observer which records log events in C{self.out}. Also, make sure the default string encoding is ASCII so that L{testSingleUnicode} can test the behavior of logging unencodable unicode messages. """ self.out = FakeFile() self.lp = log.LogPublisher() self.flo = log.FileLogObserver(self.out) self.lp.addObserver(self.flo.emit) try: str(u'\N{VULGAR FRACTION ONE HALF}') except UnicodeEncodeError: # This is the behavior we want - don't change anything. self._origEncoding = None else: if _PY3: self._origEncoding = None return reload(sys) self._origEncoding = sys.getdefaultencoding() sys.setdefaultencoding('ascii')
def test_startLogging(self): """ startLogging() installs FileLogObserver and overrides sys.stdout and sys.stderr. """ origStdout, origStderr = sys.stdout, sys.stderr self._startLoggingCleanup() # When done with test, reset stdout and stderr to current values: fakeFile = StringIO() observer = log.startLogging(fakeFile) self.addCleanup(observer.stop) log.msg("Hello!") self.assertIn("Hello!", fakeFile.getvalue()) self.assertIsInstance(sys.stdout, LoggingFile) self.assertEqual(sys.stdout.level, NewLogLevel.info) encoding = getattr(origStdout, "encoding", None) if not encoding: encoding = sys.getdefaultencoding() self.assertEqual(sys.stdout.encoding.upper(), encoding.upper()) self.assertIsInstance(sys.stderr, LoggingFile) self.assertEqual(sys.stderr.level, NewLogLevel.error) encoding = getattr(origStderr, "encoding", None) if not encoding: encoding = sys.getdefaultencoding() self.assertEqual(sys.stderr.encoding.upper(), encoding.upper())
def test_emitNewline(self): """ FileLogObserver.emit() will append a newline to its file output. """ output = StringIO() flo = log.FileLogObserver(output) publisher = log.LogPublisher() publisher.addObserver(flo.emit) publisher.msg("Hello!") result = output.getvalue() suffix = "Hello!\n" self.assertTrue( result.endswith(suffix), "{0!r} does not end with {1!r}".format(result, suffix) )
def setUp(self): # Fuck you Python. reload(sys) self._origEncoding = sys.getdefaultencoding() sys.setdefaultencoding('ascii') self.out = FakeFile() self.lp = log.LogPublisher() self.flo = log.FileLogObserver(self.out) self.lp.addObserver(self.flo.emit)
def test_timeFormatting(self): """ Test the method of L{FileLogObserver} which turns a timestamp into a human-readable string. """ # There is no function in the time module which converts a UTC time # tuple to a timestamp. when = time.mktime((2001, 2, 3, 4, 5, 6, 7, 8, 0)) - time.timezone # Pretend to be in US/Eastern for a moment self.flo.getTimezoneOffset = lambda: 18000 self.assertEquals(self.flo.formatTime(when), '2001/02/02 23:05 -0500') # Okay now we're in Eastern Europe somewhere self.flo.getTimezoneOffset = lambda: -3600 self.assertEquals(self.flo.formatTime(when), '2001/02/03 05:05 +0100') # And off in the Pacific or someplace like that self.flo.getTimezoneOffset = lambda: -39600 self.assertEquals(self.flo.formatTime(when), '2001/02/03 15:05 +1100') # One of those weird places with a half-hour offset timezone self.flo.getTimezoneOffset = lambda: 5400 self.assertEquals(self.flo.formatTime(when), '2001/02/03 02:35 -0130') # Half-hour offset in the other direction self.flo.getTimezoneOffset = lambda: -5400 self.assertEquals(self.flo.formatTime(when), '2001/02/03 05:35 +0130') # If a strftime-format string is present on the logger, it should # use that instead. Note we don't assert anything about day, hour # or minute because we cannot easily control what time.strftime() # thinks the local timezone is. self.flo.timeFormat = '%Y %m' self.assertEquals(self.flo.formatTime(when), '2001 02')
def _setUpLogFile(self): self._tearDownLogFile() if self.logfile == '-': logFile = sys.stdout else: logFile = file(self.logfile, 'a') self._logFileObject = logFile self._logFileObserver = log.FileLogObserver(logFile) log.startLoggingWithObserver(self._logFileObserver.emit, 0)
def _captureLocalLog(self): self.fobs = log.FileLogObserver(gtk2manhole._Notafile(self, "log")) self.fobs.start()
def __init__(self, level=logging.INFO): log.FileLogObserver.__init__(self, sys.stdout) self.logLevel = level
def test_getTimezoneOffsetWestOfUTC(self): """ Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns correct values for the current C{TZ} environment setting for at least some cases. This test method exercises a timezone that is west of UTC (and should produce positive results). """ self._getTimezoneOffsetTest("America/New_York", 14400, 18000)
def test_getTimezoneOffsetEastOfUTC(self): """ Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns correct values for the current C{TZ} environment setting for at least some cases. This test method exercises a timezone that is east of UTC (and should produce negative results). """ self._getTimezoneOffsetTest("Europe/Berlin", -7200, -3600)
def test_getTimezoneOffsetWithoutDaylightSavingTime(self): """ Attempt to verify that L{FileLogObserver.getTimezoneOffset} returns correct values for the current C{TZ} environment setting for at least some cases. This test method exercises a timezone that does not use daylight saving time at all (so both summer and winter time test values should have the same offset). """ # Test a timezone that doesn't have DST. mktime() implementations # available for testing seem happy to produce results for this even # though it's not entirely valid. self._getTimezoneOffsetTest("Africa/Johannesburg", -7200, -7200)
def test_emitPrefix(self): """ FileLogObserver.emit() will add a timestamp and system prefix to its file output. """ output = StringIO() flo = log.FileLogObserver(output) events = [] def observer(event): # Capture the event for reference and pass it along to flo events.append(event) flo.emit(event) publisher = log.LogPublisher() publisher.addObserver(observer) publisher.msg("Hello!") self.assertEqual(len(events), 1) event = events[0] result = output.getvalue() prefix = "{time} [{system}] ".format( time=flo.formatTime(event["time"]), system=event["system"], ) self.assertTrue( result.startswith(prefix), "{0!r} does not start with {1!r}".format(result, prefix) )
def _setUpLogFile(self): self._tearDownLogFile() if self.logfile == '-': logFile = sys.stdout else: logFile = open(self.logfile, 'a') self._logFileObject = logFile self._logFileObserver = log.FileLogObserver(logFile) log.startLoggingWithObserver(self._logFileObserver.emit, 0)
def __init__(self, f, log_level=levels['INFO']): tw_log.FileLogObserver.__init__(self, f) self.log_level = log_level
def emit(self, eventDict): if not self.should_emit(eventDict): return tw_log.FileLogObserver.emit(self, eventDict)
def test_timeFormatting(self): """ Test the method of L{FileLogObserver} which turns a timestamp into a human-readable string. """ when = calendar.timegm((2001, 2, 3, 4, 5, 6, 7, 8, 0)) # Pretend to be in US/Eastern for a moment self.flo.getTimezoneOffset = lambda when: 18000 self.assertEqual(self.flo.formatTime(when), '2001-02-02 23:05:06-0500') # Okay now we're in Eastern Europe somewhere self.flo.getTimezoneOffset = lambda when: -3600 self.assertEqual(self.flo.formatTime(when), '2001-02-03 05:05:06+0100') # And off in the Pacific or someplace like that self.flo.getTimezoneOffset = lambda when: -39600 self.assertEqual(self.flo.formatTime(when), '2001-02-03 15:05:06+1100') # One of those weird places with a half-hour offset timezone self.flo.getTimezoneOffset = lambda when: 5400 self.assertEqual(self.flo.formatTime(when), '2001-02-03 02:35:06-0130') # Half-hour offset in the other direction self.flo.getTimezoneOffset = lambda when: -5400 self.assertEqual(self.flo.formatTime(when), '2001-02-03 05:35:06+0130') # Test an offset which is between 0 and 60 minutes to make sure the # sign comes out properly in that case. self.flo.getTimezoneOffset = lambda when: 1800 self.assertEqual(self.flo.formatTime(when), '2001-02-03 03:35:06-0030') # Test an offset between 0 and 60 minutes in the other direction. self.flo.getTimezoneOffset = lambda when: -1800 self.assertEqual(self.flo.formatTime(when), '2001-02-03 04:35:06+0030') # If a strftime-format string is present on the logger, it should # use that instead. Note we don't assert anything about day, hour # or minute because we cannot easily control what time.strftime() # thinks the local timezone is. self.flo.timeFormat = '%Y %m' self.assertEqual(self.flo.formatTime(when), '2001 02')