From 090141ea59f2bd280ed8dcfd51909758fd57fb30 Mon Sep 17 00:00:00 2001 From: Kian-Tat Lim Date: Mon, 16 Oct 2023 07:28:09 -0700 Subject: [PATCH] Change to logging. --- python/s3daemon/s3daemon.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/python/s3daemon/s3daemon.py b/python/s3daemon/s3daemon.py index 28f323c..cbe09e9 100644 --- a/python/s3daemon/s3daemon.py +++ b/python/s3daemon/s3daemon.py @@ -20,6 +20,7 @@ # along with this program. If not, see . import asyncio +import logging import os import time @@ -40,6 +41,13 @@ access_key = os.environ["AWS_ACCESS_KEY_ID"] secret_key = os.environ["AWS_SECRET_ACCESS_KEY"] +pylog_longLogFmt = "{levelname} {asctime} {name} - {message}" +if "S3DAEMON_LOG" in os.environ: + logging.basicConfig(filename=os.environ["S3DAEMON_LOG"], format=pylog_longLogFmt, style="{") +else: + logging.basicConfig(format=pylog_longLogFmt, style="{") +log = logging.getLogger(__name__) +log.setLevel(logging.INFO) async def handle_client(client, reader, writer): """Handle a client connection to the server socket. @@ -63,7 +71,7 @@ async def handle_client(client, reader, writer): writer.write(b"Success") except Exception as e: writer.write(bytes(repr(e), "UTF-8")) - print(start, time.time() - start, "sec") + log.info("%f %f sec", start, time.time() - start) async def main(): @@ -81,6 +89,7 @@ async def client_cb(reader, writer): await handle_client(client, reader, writer) server = await asyncio.start_server(client_cb, "localhost", PORT) + log.info("Starting server") async with server: await server.serve_forever()