- Add Logger to the script

- Use ful path for config file so that it can be available if run from cron job
This commit is contained in:
Vikas Nale 2025-06-17 15:36:55 +05:30
parent 8804a87b9d
commit c039efa4bf

View File

@ -1,10 +1,24 @@
import requests import requests
from dotenv import load_dotenv from dotenv import load_dotenv
import os import os
import logging, traceback
load_dotenv(dotenv_path="config.env", override=True)
logging.basicConfig(
level=logging.INFO,
format='[%(asctime)s] %(levelname)s: %(message)s',
handlers=[
logging.FileHandler("app.log"),
logging.StreamHandler() # logs to console
]
)
script_dir = os.path.dirname(os.path.abspath(__file__))
load_dotenv(dotenv_path=f"{script_dir}/config.env", override=True)
base_url = os.getenv("API_BASE_URL") base_url = os.getenv("API_BASE_URL")
print(f"base_url: {base_url}")
def login(): def login():
payload = { payload = {
"username": os.getenv("USERNAME"), "username": os.getenv("USERNAME"),
@ -30,13 +44,19 @@ def project_proccess(jwt):
return response return response
try: try:
logging.info("Script started")
jwt = login() jwt = login()
logging.info("Login Success")
response = project_proccess(jwt) # Call your function response = project_proccess(jwt) # Call your function
if response.status_code == 200: if response.status_code == 200:
print("Email sent") print("Email sent")
logging.info("Script started")
else: else:
print(f"Failed with response: {response}") print(f"Failed with response: {response}")
except Exception as e: logging.info(f"Failed with response: {response}")
print(f"An error occurred: {e}") except Exception:
msg = traceback.format_exc()
logging.error("An error occurred:\n%s", msg)
print(f"An error occurred: {msg }")