diff --git a/.gitignore b/.gitignore index 84c26bd0..c71ccfaf 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ *.py[cod] venv *.egg-info +*.log dist diff --git a/matter_server/server/__main__.py b/matter_server/server/__main__.py index 8c424877..fc18cf99 100644 --- a/matter_server/server/__main__.py +++ b/matter_server/server/__main__.py @@ -1,3 +1,4 @@ +import argparse import asyncio import logging import os @@ -9,12 +10,42 @@ from .matter_stack import MatterStack from .server import MatterServer -logging.basicConfig(level=logging.WARN) _LOGGER = logging.getLogger(__package__) -_LOGGER.setLevel(logging.DEBUG) + + +def get_arguments() -> argparse.Namespace: + """Get parsed passed in arguments.""" + + parser = argparse.ArgumentParser( + description="Matter Controller Server using WebSockets." + ) + parser.add_argument( + "--log-file", + type=str, + default=None, + help="Log file to write to. If not set, matter_server.log is used", + ) + parser.add_argument( + "-log", + "--log-level", + default="info", + help="Provide logging level. Example --log-level debug, default=info, possible=(critical, error, warning, info, debug)", + ) + + arguments = parser.parse_args() + + return arguments def main() -> int: + args = get_arguments() + handlers = None + + if args.log_file: + handlers = [logging.FileHandler(args.log_file)] + + logging.basicConfig(handlers=handlers, level=args.log_level.upper()) + host = os.getenv("CHIP_WS_SERVER_HOST", "::,0.0.0.0").split(",") port = int(os.getenv("CHIP_WS_SERVER_PORT", "5580")) storage_path = os.getenv( @@ -23,7 +54,7 @@ def main() -> int: ) debug = os.getenv("CHIP_WS_DEBUG") is not None - coloredlogs.install(level=logging.DEBUG if debug else logging.INFO) + coloredlogs.install(level=logging.DEBUG if debug else args.log_level.upper()) stack = MatterStack(storage_path) loop = asyncio.get_event_loop()