def test_error(self): """ StdioOnnaStick created with isError=True log messages as errors. """ stdio = log.StdioOnnaStick(isError=True) stdio.write("log 1\n") self.assertTrue(self.resultLogs[0]['isError'])
def test_unicode(self): """ StdioOnnaStick converts unicode prints to byte strings on Python 2, in order to be compatible with the normal stdout/stderr objects. On Python 3, the prints are left unmodified. """ unicodeString = u"Hello, \N{VULGAR FRACTION ONE HALF} world." stdio = log.StdioOnnaStick(encoding="utf-8") self.assertEqual(stdio.encoding, "utf-8") stdio.write(unicodeString + u"\n") stdio.writelines([u"Also, " + unicodeString]) oldStdout = sys.stdout sys.stdout = stdio self.addCleanup(setattr, sys, "stdout", oldStdout) # This should go to the log, utf-8 encoded too: print(unicodeString) if _PY3: self.assertEqual(self.getLogMessages(), [unicodeString, u"Also, " + unicodeString, unicodeString]) else: self.assertEqual(self.getLogMessages(), [unicodeString.encode("utf-8"), (u"Also, " + unicodeString).encode("utf-8"), unicodeString.encode("utf-8")])
def test_writeLines(self): """ Writing lines to a StdioOnnaStick results in Twisted log messages. """ stdio = log.StdioOnnaStick() stdio.writelines(["log 1", "log 2"]) self.assertEqual(self.getLogMessages(), ["log 1", "log 2"])
def test_metadata(self): """ The log messages written by StdioOnnaStick have printed=1 keyword, and by default are not errors. """ stdio = log.StdioOnnaStick() stdio.write("hello\n") self.assertFalse(self.resultLogs[0]['isError']) self.assertTrue(self.resultLogs[0]['printed'])
def test_print(self): """ When StdioOnnaStick is set as sys.stdout, prints become log messages. """ oldStdout = sys.stdout sys.stdout = log.StdioOnnaStick() self.addCleanup(setattr, sys, "stdout", oldStdout) print("This", end=" ") print("is a test") self.assertEqual(self.getLogMessages(), ["This is a test"])
def test_write(self): """ Writing to a StdioOnnaStick instance results in Twisted log messages. Log messages are generated every time a '\n' is encountered. """ stdio = log.StdioOnnaStick() stdio.write("Hello there\nThis is a test") self.assertEqual(self.getLogMessages(), ["Hello there"]) stdio.write("!\n") self.assertEqual(self.getLogMessages(), ["Hello there", "This is a test!"])
def serve(filename): logging.debug("Serving %s" % filename) name = os.path.basename(filename) OptionsClass = WrapTwistedOptions(TwistdServerOptions) options = OptionsClass() argv = options.parseOptions() logging.getLogger('amqplib').setLevel(logging.WARN) FLAGS.python = filename FLAGS.no_save = True if not FLAGS.pidfile: FLAGS.pidfile = '%s.pid' % name elif FLAGS.pidfile.endswith('twistd.pid'): FLAGS.pidfile = FLAGS.pidfile.replace('twistd.pid', '%s.pid' % name) if not FLAGS.logfile: FLAGS.logfile = '%s.log' % name action = 'start' if len(argv) > 1: action = argv.pop() if action == 'stop': stop(FLAGS.pidfile) sys.exit() elif action == 'restart': stop(FLAGS.pidfile) elif action == 'start': pass else: print 'usage: %s [options] [start|stop|restart]' % argv[0] sys.exit(1) formatter = logging.Formatter( name + '(%(name)s): %(levelname)s %(message)s') handler = logging.StreamHandler(log.StdioOnnaStick()) handler.setFormatter(formatter) logging.getLogger().addHandler(handler) if FLAGS.verbose: logging.getLogger().setLevel(logging.DEBUG) else: logging.getLogger().setLevel(logging.WARNING) if FLAGS.syslog: syslog = logging.handlers.SysLogHandler(address='/dev/log') syslog.setFormatter(formatter) logging.getLogger().addHandler(syslog) logging.debug("Full set of FLAGS:") for flag in FLAGS: logging.debug("%s : %s" % (flag, FLAGS.get(flag, None))) twistd.runApp(options)
def _setupLogger(self): """Set up a 'slave-scanner' logger that redirects to twisted. Make it less verbose to avoid messing too much with the old code. """ level = logging.INFO logger = logging.getLogger(BUILDD_MANAGER_LOG_NAME) # Redirect the output to the twisted log module. channel = logging.StreamHandler(log.StdioOnnaStick()) channel.setLevel(level) channel.setFormatter(logging.Formatter('%(message)s')) logger.addHandler(channel) logger.setLevel(level) return logger
return connection.rotator_test if is_test else connection.rotator mongo_connection = { 'connection': acquire_new_connection_pool(), 'connected': True, 'rotator_database': None } mongo_connection['rotator_database'] = get_rotator_database( mongo_connection.get('connection'), is_test=False ) log.startLogging(log.StdioOnnaStick()) def log_me(*args, **kwargs): # pylint: disable=unused-argument fmt_msg = pformat([arg for arg in args], width=80, indent=2) if USE_SIMPLE_PRINT: # pylint: disable=print-statement print fmt_msg else: log.msg(fmt_msg)