소스 검색

Set logging from cli

Sean 4 년 전
부모
커밋
dbf30dfac2
2개의 변경된 파일17개의 추가작업 그리고 9개의 파일을 삭제
  1. 7 0
      ocma/cli.py
  2. 10 9
      ocma/connect.py

+ 7 - 0
ocma/cli.py

@@ -45,6 +45,11 @@ def run() -> None:
         action="store_false",
         help="If the browser window should be shown during the authentication process.",
     )
+    parser.add_argument(
+        "-v",
+        action="store_true",
+        help="If verbal messages should be printed to stderr",
+    )
 
     parser.add_argument(
         "--print-to-stdout",
@@ -65,6 +70,7 @@ def run() -> None:
     vpn_url: str = args.vpn_url
     headless: bool = args.show_head
     print_to_stdout: bool = args.print_to_stdout
+    log_messages: bool = args.v
 
     if password is None:
         password = input()
@@ -82,6 +88,7 @@ def run() -> None:
         mfa_secret=mfa_secret,
         vpn_site=vpn_url,
         headless=headless,
+        log_messages=log_messages,
     )
 
     if print_to_stdout:

+ 10 - 9
ocma/connect.py

@@ -30,15 +30,6 @@ DOMAIN_CHECK_DELAY = 0.5
 LOGGER = logging.getLogger("OCMA")
 LOGGER.setLevel(logging.INFO)
 
-fh = logging.FileHandler("ocma.log")
-fh.setLevel(logging.DEBUG)
-
-fh_s = logging.StreamHandler()
-fh_s.setLevel(logging.DEBUG)
-
-LOGGER.addHandler(fh)
-LOGGER.addHandler(fh_s)
-
 
 @dataclass
 class VPNCookie:
@@ -52,7 +43,17 @@ def login(
     mfa_secret: Optional[str] = None,
     vpn_site: str = "https://vpn.fhnw.ch",
     headless: bool = True,
+    log_messages: bool = False,
 ) -> VPNCookie:
+    if log_messages:
+        formatter = logging.Formatter(
+            "%(asctime)s: %(levelname)s - %(name)s - %(message)s"
+        )
+        fh_s = logging.StreamHandler()
+        fh_s.setLevel(logging.INFO)
+        fh_s.setFormatter(formatter)
+        LOGGER.addHandler(fh_s)
+
     LOGGER.info("Starting")
 
     options = FirefoxOptions()