feat: use logging library to log
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
Aravinth Manivannan 2023-10-05 00:22:07 +05:30
parent b9d17d75f5
commit 40899951ad
Signed by: realaravinth
GPG key ID: F8F50389936984FF
3 changed files with 31 additions and 10 deletions

View file

@ -4,11 +4,12 @@
import argparse import argparse
from .logger import logger
from .cli import Cli from .cli import Cli
def admin(args): def admin(args):
print(args) logger.info(args)
if __name__ == "__main__": if __name__ == "__main__":

View file

@ -14,6 +14,7 @@ from requests.auth import HTTPBasicAuth
import requests import requests
from .csrf import ParseCSRF from .csrf import ParseCSRF
from .logger import logger
# FORGEJO_USER = "root" # FORGEJO_USER = "root"
# FORGEJO_EMAIL = "root@example.com" # FORGEJO_EMAIL = "root@example.com"
@ -66,7 +67,7 @@ class Forgejo:
break break
except: except:
sleep(2) sleep(2)
print(f"Retrying {count} time") logger.info(f"Retrying {count} time")
count += 1 count += 1
continue continue
@ -124,7 +125,7 @@ class Forgejo:
""" """
resp = self.c.get(url, allow_redirects=False) resp = self.c.get(url, allow_redirects=False)
if resp.status_code != 200 and resp.status_code != 302: 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}") raise Exception(f"Can't get csrf token: {resp.status_code}")
parser = ParseCSRF(name=self.__csrf_key) parser = ParseCSRF(name=self.__csrf_key)
parser.feed(resp.text) parser.feed(resp.text)
@ -165,7 +166,7 @@ class Forgejo:
if any( if any(
[resp.status_code == 302, resp.status_code == 200, resp.status_code == 303] [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 self.__logged_in = True
return return
@ -202,7 +203,7 @@ class Forgejo:
data = get_repository_payload(csrf, name, user_id=user_id) data = get_repository_payload(csrf, name, user_id=user_id)
resp = self.c.post(url, data=data, allow_redirects=False) resp = self.c.post(url, data=data, allow_redirects=False)
print(f"Created repository {name}") logger.info(f"Created repository {name}")
if ( if (
resp.status_code != 302 resp.status_code != 302
and resp.status_code != 200 and resp.status_code != 200
@ -238,7 +239,7 @@ class Forgejo:
data = create_issue_payload(csrf=csrf, title=title, body=body) data = create_issue_payload(csrf=csrf, title=title, body=body)
resp = self.c.post(url, data=data, allow_redirects=False) resp = self.c.post(url, data=data, allow_redirects=False)
print(f"Created issue") logger.info(f"Created issue")
if ( if (
resp.status_code != 302 resp.status_code != 302
and resp.status_code != 200 and resp.status_code != 200
@ -267,7 +268,7 @@ class Forgejo:
url = self.get_uri(f"/{owner}/{repo}/issues/{issue}/comments") url = self.get_uri(f"/{owner}/{repo}/issues/{issue}/comments")
resp = self.c.post(url, data=data, allow_redirects=False) resp = self.c.post(url, data=data, allow_redirects=False)
print(f"Created comment") logger.info(f"Created comment")
if ( if (
resp.status_code != 302 resp.status_code != 302
and resp.status_code != 200 and resp.status_code != 200
@ -301,9 +302,11 @@ class Forgejo:
resp = session.post(url, json=data, allow_redirects=False) resp = session.post(url, json=data, allow_redirects=False)
if resp.status_code != 201: 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() data = resp.json()
with open(file, "w") as f: with open(file, "w") as f:
data["login"] = self.username data["login"] = self.username
@ -312,4 +315,4 @@ class Forgejo:
data["forgejo_url"] = self.get_uri("") data["forgejo_url"] = self.get_uri("")
content = json.dumps(data) content = json.dumps(data)
f.write(content) f.write(content)
print(f"Wrote access token to {file}") logger.info(f"Wrote access token to {file}")

17
forgejo/logger.py Normal file
View file

@ -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()