Skip to content

Commit

Permalink
Adds argparse to enable log to file (#2)
Browse files Browse the repository at this point in the history
* Adds argparse to enable log to file

* Adds log-level to arguments

Co-authored-by: Stefan Agner <[email protected]>
  • Loading branch information
jmarcelomb and agners authored Jul 5, 2022
1 parent fc877ca commit c2e45da
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
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
37 changes: 34 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,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(
Expand All @@ -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()
Expand Down

0 comments on commit c2e45da

Please sign in to comment.