Redirect stdout/stderr to logs after initialization (#19131)

This regressed in https://github.com/element-hq/synapse/pull/19121. I
moved things in https://github.com/element-hq/synapse/pull/19121 because
I thought that it made sense to redirect anything printed to
`stdout`/`stderr` to the logs as early as possible. But we actually want
to log any immediately apparent problems during initialization to
`stderr` in the terminal so that they are obvious and visible to the
operator.

Now, I've moved `redirect_stdio_to_logs()` back to where it was
previously along with some proper comment context for why we have it
there.
This commit is contained in:
Eric Eastwood
2025-11-03 16:16:23 -06:00
committed by GitHub
parent 891acfd502
commit db00925ae7
4 changed files with 20 additions and 8 deletions

1
changelog.d/19131.misc Normal file
View File

@@ -0,0 +1 @@
Refactor and align app entrypoints (avoid `exit(1)` in our composable functions).

View File

@@ -369,6 +369,7 @@ async def start(admin_command_server: AdminCmdServer, args: argparse.Namespace)
def main() -> None:
homeserver_config, args = load_config(sys.argv[1:])
with LoggingContext(name="main", server_name=homeserver_config.server.server_name):
# Initialize and setup the homeserver
admin_command_server = create_homeserver(homeserver_config)
setup(admin_command_server)

View File

@@ -450,16 +450,21 @@ def main() -> None:
# Create a logging context as soon as possible so we can start associating
# everything with this homeserver.
with LoggingContext(name="main", server_name=homeserver_config.server.server_name):
# redirect stdio to the logs, if configured.
if not homeserver_config.logging.no_redirect_stdio:
redirect_stdio_to_logs()
# Initialize and setup the homeserver
hs = create_homeserver(homeserver_config)
try:
setup(hs)
except Exception as e:
handle_startup_exception(e)
# For problems immediately apparent during initialization, we want to log to
# stderr in the terminal so that they are obvious and visible to the operator.
#
# Now that we're past the initialization stage, we can redirect anything printed
# to stdio to the logs, if configured.
if not homeserver_config.logging.no_redirect_stdio:
redirect_stdio_to_logs()
# Register a callback to be invoked once the reactor is running
register_start(hs, start, hs)

View File

@@ -479,16 +479,21 @@ def main() -> None:
# Create a logging context as soon as possible so we can start associating
# everything with this homeserver.
with LoggingContext(name="main", server_name=homeserver_config.server.server_name):
# redirect stdio to the logs, if configured.
if not homeserver_config.logging.no_redirect_stdio:
redirect_stdio_to_logs()
# Initialize and setup the homeserver
hs = create_homeserver(homeserver_config)
try:
setup(hs)
except Exception as e:
handle_startup_exception(e)
# For problems immediately apparent during initialization, we want to log to
# stderr in the terminal so that they are obvious and visible to the operator.
#
# Now that we're past the initialization stage, we can redirect anything printed
# to stdio to the logs, if configured.
if not homeserver_config.logging.no_redirect_stdio:
redirect_stdio_to_logs()
# Register a callback to be invoked once the reactor is running
register_start(hs, start, hs)