Skip to content
This repository was archived by the owner on Mar 19, 2021. It is now read-only.

Commit 7edf9ea

Browse files
committed
[serve] Make pywebsocket honor log_level config
Previously, `wpt serve` always starts pywebsocket with hard-coded the default "debug" log level regardless of config.json, although it does pass the log level from the config to wptserve. This leads to inconsistent logging levels when a non-default log level is specified in config.json, and makes it impossible for users to silence `wpt serve` (pywebsocket also happens to be quite spammy). The implementation is unfortunately hacky (double configurations) in order to work around an earlier workaround which reloads the logging module (#13091).
1 parent 87fea09 commit 7edf9ea

File tree

1 file changed

+27
-18
lines changed

1 file changed

+27
-18
lines changed

tools/serve/serve.py

Lines changed: 27 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -308,6 +308,7 @@ def _script_replacement(self, key, value):
308308

309309
rewrites = [("GET", "/resources/WebIDLParser.js", "/resources/webidl2/lib/webidl2.js")]
310310

311+
311312
class RoutesBuilder(object):
312313
def __init__(self):
313314
self.forbidden_override = [("GET", "/tools/runner/*", handlers.file_handler),
@@ -501,7 +502,7 @@ def make_hosts_file(config, host):
501502
def start_servers(host, ports, paths, routes, bind_address, config, **kwargs):
502503
servers = defaultdict(list)
503504
for scheme, ports in ports.items():
504-
assert len(ports) == {"http":2}.get(scheme, 1)
505+
assert len(ports) == {"http": 2}.get(scheme, 1)
505506

506507
# If trying to start HTTP/2.0 server, check compatibility
507508
if scheme == 'http2' and not http2_compatible():
@@ -512,11 +513,11 @@ def start_servers(host, ports, paths, routes, bind_address, config, **kwargs):
512513
for port in ports:
513514
if port is None:
514515
continue
515-
init_func = {"http":start_http_server,
516-
"https":start_https_server,
517-
"http2":start_http2_server,
518-
"ws":start_ws_server,
519-
"wss":start_wss_server}[scheme]
516+
init_func = {"http": start_http_server,
517+
"https": start_https_server,
518+
"http2": start_http2_server,
519+
"ws": start_ws_server,
520+
"wss": start_wss_server}[scheme]
520521

521522
server_proc = ServerProc(scheme=scheme)
522523
server_proc.start(init_func, host, port, paths, routes, bind_address,
@@ -570,6 +571,8 @@ def start_http2_server(host, port, paths, routes, bind_address, config, **kwargs
570571
encrypt_after_connect=config.ssl_config["encrypt_after_connect"],
571572
latency=kwargs.get("latency"),
572573
http2=True)
574+
575+
573576
class WebSocketDaemon(object):
574577
def __init__(self, host, port, doc_root, handlers_root, log_level, bind_address,
575578
ssl_config):
@@ -601,6 +604,17 @@ def __init__(self, host, port, doc_root, handlers_root, log_level, bind_address,
601604
opts, args = pywebsocket._parse_args_and_config(cmd_args)
602605
opts.cgi_directories = []
603606
opts.is_executable_method = None
607+
608+
# Logging needs to be configured both before and after reloading,
609+
# because some modules store loggers as global variables.
610+
pywebsocket._configure_logging(opts)
611+
# Ensure that when we start this in a new process we have the global
612+
# lock in the logging module unlocked.
613+
reload_module(logging)
614+
release_mozlog_lock()
615+
pywebsocket._configure_logging(opts)
616+
# DO NOT LOG BEFORE THIS LINE.
617+
604618
self.server = pywebsocket.WebSocketServer(opts)
605619
ports = [item[0].getsockname()[1] for item in self.server._sockets]
606620
assert all(item == ports[0] for item in ports)
@@ -647,29 +661,21 @@ def release_mozlog_lock():
647661

648662

649663
def start_ws_server(host, port, paths, routes, bind_address, config, **kwargs):
650-
# Ensure that when we start this in a new process we have the global lock
651-
# in the logging module unlocked
652-
reload_module(logging)
653-
release_mozlog_lock()
654664
return WebSocketDaemon(host,
655665
str(port),
656666
repo_root,
657667
config.paths["ws_doc_root"],
658-
"debug",
668+
config.log_level.lower(),
659669
bind_address,
660-
ssl_config = None)
670+
ssl_config=None)
661671

662672

663673
def start_wss_server(host, port, paths, routes, bind_address, config, **kwargs):
664-
# Ensure that when we start this in a new process we have the global lock
665-
# in the logging module unlocked
666-
reload_module(logging)
667-
release_mozlog_lock()
668674
return WebSocketDaemon(host,
669675
str(port),
670676
repo_root,
671677
config.paths["ws_doc_root"],
672-
"debug",
678+
config.log_level.lower(),
673679
bind_address,
674680
config.ssl_config)
675681

@@ -726,9 +732,11 @@ def build_config(override_path=None, **kwargs):
726732

727733
return rv
728734

735+
729736
def _make_subdomains_product(s, depth=2):
730737
return {u".".join(x) for x in chain(*(product(s, repeat=i) for i in range(1, depth+1)))}
731738

739+
732740
_subdomains = {u"www",
733741
u"www1",
734742
u"www2",
@@ -745,7 +753,8 @@ def _make_subdomains_product(s, depth=2):
745753
class ConfigBuilder(config.ConfigBuilder):
746754
"""serve config
747755
748-
this subclasses wptserve.config.ConfigBuilder to add serve config options"""
756+
This subclasses wptserve.config.ConfigBuilder to add serve config options.
757+
"""
749758

750759
_default = {
751760
"browser_host": "web-platform.test",

0 commit comments

Comments
 (0)