Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds argparse to enable log to file #2

Merged
merged 12 commits into from
Jul 5, 2022
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.py[cod]
venv
*.egg-info
*.log
dist
42 changes: 39 additions & 3 deletions matter_server/server/__main__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import argparse
import asyncio
import logging
import os
Expand All @@ -9,12 +10,47 @@
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."
jmarcelomb marked this conversation as resolved.
Show resolved Hide resolved
)

parser.add_argument(
"-v", "--verbose", action="store_true", help="Enable verbose logging to file."
)
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()

if args.verbose:
file_handler = logging.FileHandler(
"matter_server.log" if args.log_file is None else args.log_file
)

logging.basicConfig(handlers=[file_handler], 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(
Expand All @@ -23,7 +59,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()
Expand Down