Пример #1
0
 def test_configurator_configures_correct_formatter(self):
     logging_config = get_logger_config(merge_configs([default_format]))
     self.assertEqual(logging_config["handlers"]["file"]["formatter"],
                      "default")
     logging_config = get_logger_config(merge_configs([json_format]))
     self.assertEqual(logging_config["handlers"]["file"]["formatter"],
                      "json")
Пример #2
0
 def test_configurator_configures_correct_formatter_for_syslog(self):
     logging_config = get_logger_config(
         merge_configs([syslog_target, default_format]))
     self.assertEqual(logging_config["handlers"]["syslog"]["formatter"],
                      "syslog_default")
     logging_config = get_logger_config(
         merge_configs([syslog_target, json_format]))
     self.assertEqual(logging_config["handlers"]["syslog"]["formatter"],
                      "syslog_json")
Пример #3
0
 def test_configurator_configures_correct_handler(self):
     logging.config.dictConfig(
         get_logger_config(merge_configs([stdout_target])))
     self.assertIsInstance(logging.getLogger().handlers[0],
                           logging.StreamHandler)
     logging.config.dictConfig(
         get_logger_config(merge_configs([file_target])))
     self.assertIsInstance(logging.getLogger().handlers[0],
                           logging.handlers.RotatingFileHandler)
Пример #4
0
 def test_json_logger_extra_args(self):
     with patch("sys.stdout", new=io.StringIO()) as fake_out:
         logging.config.dictConfig(
             get_logger_config(merge_configs([stdout_target, json_format])))
         now = datetime.datetime.utcnow()
         logging.info(
             "testmessage",
             extra={
                 "time": now,
                 "extraarg": "potatoes",
                 "anothertimefield": now,
                 "anumber": 42,
             },
         )
         self.assertEqual(
             {
                 "message": "testmessage",
                 "extraarg": "potatoes",
                 "anothertimefield": now.isoformat(),
                 "anumber": 42,
                 "level": "INFO",
                 "time": now.isoformat(),
             },
             json.loads(fake_out.getvalue()),
         )
Пример #5
0
    def test_configurator_configures_correct_syslog_handler(self):
        logging.config.dictConfig(
            get_logger_config(merge_configs([syslog_target])))
        expected_parent = (logging.handlers.SysLogHandler
                           if os.path.exists("/dev/log") else
                           logging.StreamHandler)

        self.assertIsInstance(logging.getLogger().handlers[0], expected_parent)
Пример #6
0
 def test_json_logger_integration(self):
     with patch("sys.stdout", new=io.StringIO()) as fake_out:
         logging.config.dictConfig(
             get_logger_config(merge_configs([stdout_target, json_format])))
         now = datetime.datetime.utcnow()
         logging.info("testmessage",
                      extra={"time":
                             now})  # force in time for the logged entry
         self.assertEqual(
             {
                 "message": "testmessage",
                 "time": now.isoformat(),
                 "level": "INFO"
             },
             json.loads(fake_out.getvalue()),
         )
Пример #7
0
try:
    opts, args = getopt.getopt(sys.argv[1:], "hc:", ["config="])
except getopt.GetoptError:
    print("rest.py -c <config_file>")
    sys.exit(2)

for opt, arg in opts:
    if opt == "-h":
        print("rest.py -c <config_file>")
        sys.exit()
    elif opt in ("-c", "--config"):
        configpath = arg

config = parse_config(configpath)

logging.config.dictConfig(get_logger_config(config))

servers = []

app = web.Application(middlewares=[jsonerrorhandler, auth_enforcer])
setup_plat_routes(app, config)

loop = asyncio.get_event_loop()
handler = app.make_handler(access_log=access_logger,
                           access_log_format=ACCESS_LOG_FORMAT)

servers.extend(
    [loop.create_server(handler, "*", port) for port in config["ports"]])

if config["ssl_certificate"] and os.path.isfile(config["ssl_certificate"]):
    ssl_context = ssl.create_default_context(ssl.Purpose.CLIENT_AUTH)
Пример #8
0
 def test_configurator_configures_correct_logfile(self):
     logging_config = get_logger_config(merge_configs([logfile_location]))
     self.assertEqual(logging_config["handlers"]["file"]["filename"],
                      "/tmp/otherfile.log")