diff --git a/forgejo/__main__.py b/forgejo/__main__.py index c2ffd00..ce7589e 100644 --- a/forgejo/__main__.py +++ b/forgejo/__main__.py @@ -4,11 +4,12 @@ import argparse +from .logger import logger from .cli import Cli def admin(args): - print(args) + logger.info(args) if __name__ == "__main__": diff --git a/forgejo/forgejo.py b/forgejo/forgejo.py index fcbfffb..20ae3d1 100755 --- a/forgejo/forgejo.py +++ b/forgejo/forgejo.py @@ -14,6 +14,7 @@ from requests.auth import HTTPBasicAuth import requests from .csrf import ParseCSRF +from .logger import logger # FORGEJO_USER = "root" # FORGEJO_EMAIL = "root@example.com" @@ -66,7 +67,7 @@ class Forgejo: break except: sleep(2) - print(f"Retrying {count} time") + logger.info(f"Retrying {count} time") count += 1 continue @@ -124,7 +125,7 @@ class Forgejo: """ resp = self.c.get(url, allow_redirects=False) if resp.status_code != 200 and resp.status_code != 302: - print(url, resp.status_code) + logger.info(url, resp.status_code) raise Exception(f"Can't get csrf token: {resp.status_code}") parser = ParseCSRF(name=self.__csrf_key) parser.feed(resp.text) @@ -165,7 +166,7 @@ class Forgejo: if any( [resp.status_code == 302, resp.status_code == 200, resp.status_code == 303] ): - print("User logged in") + logger.info("User logged in") self.__logged_in = True return @@ -202,7 +203,7 @@ class Forgejo: data = get_repository_payload(csrf, name, user_id=user_id) resp = self.c.post(url, data=data, allow_redirects=False) - print(f"Created repository {name}") + logger.info(f"Created repository {name}") if ( resp.status_code != 302 and resp.status_code != 200 @@ -238,7 +239,7 @@ class Forgejo: data = create_issue_payload(csrf=csrf, title=title, body=body) resp = self.c.post(url, data=data, allow_redirects=False) - print(f"Created issue") + logger.info(f"Created issue") if ( resp.status_code != 302 and resp.status_code != 200 @@ -267,7 +268,7 @@ class Forgejo: url = self.get_uri(f"/{owner}/{repo}/issues/{issue}/comments") resp = self.c.post(url, data=data, allow_redirects=False) - print(f"Created comment") + logger.info(f"Created comment") if ( resp.status_code != 302 and resp.status_code != 200 @@ -301,9 +302,11 @@ class Forgejo: resp = session.post(url, json=data, allow_redirects=False) if resp.status_code != 201: - raise Exception(f"Error while creating access token: {resp.status_code} {resp} {resp.text}") + raise Exception( + f"Error while creating access token: {resp.status_code} {resp} {resp.text}" + ) - print("Created access token") + logger.info("Created access token") data = resp.json() with open(file, "w") as f: data["login"] = self.username @@ -312,4 +315,4 @@ class Forgejo: data["forgejo_url"] = self.get_uri("") content = json.dumps(data) f.write(content) - print(f"Wrote access token to {file}") + logger.info(f"Wrote access token to {file}") diff --git a/forgejo/logger.py b/forgejo/logger.py new file mode 100644 index 0000000..88ccb2b --- /dev/null +++ b/forgejo/logger.py @@ -0,0 +1,17 @@ +import logging + + +def configure_logger(): + logger = logging.getLogger("forgejo-installer") + logger.setLevel(logging.DEBUG) + ch = logging.StreamHandler() + ch.setLevel(logging.DEBUG) + formatter = logging.Formatter( + "%(asctime)s - %(name)s - %(levelname)s - %(message)s" + ) + ch.setFormatter(formatter) + logger.addHandler(ch) + return logger + + +logger = configure_logger()