Skip to content

Commit 6b37d7a

Browse files
surajitsurajit
authored andcommitted
modify log file setup
1 parent d4a942c commit 6b37d7a

File tree

2 files changed

+26
-5
lines changed

2 files changed

+26
-5
lines changed

logger/log.py

Lines changed: 25 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,33 @@
11
import logging
2+
import os
23
from logging.handlers import RotatingFileHandler
3-
44
from functools import wraps
55

6+
from settings import LOG_FILE_DIR
67
from settings import LOG_FILE_NAME
78

9+
LOG_FILE = LOG_FILE_DIR.joinpath(LOG_FILE_NAME)
10+
11+
12+
def create_log_dir():
13+
if not os.path.exists("logs"):
14+
os.makedirs("logs")
15+
16+
17+
def create_log_file():
18+
try:
19+
open(LOG_FILE, "x")
20+
except FileExistsError:
21+
pass
22+
23+
24+
create_log_dir()
25+
create_log_file()
26+
827
logger = logging.getLogger(__name__)
928
logger.setLevel(logging.DEBUG)
10-
formatter = logging.Formatter('%(asctime)s | %(levelname)s | %(message)s')
11-
fh = RotatingFileHandler(LOG_FILE_NAME)
29+
formatter = logging.Formatter("%(asctime)s | %(levelname)s | %(message)s")
30+
fh = RotatingFileHandler(LOG_FILE)
1231
fh.setLevel(logging.DEBUG)
1332
fh.setFormatter(formatter)
1433
logger.addHandler(fh)
@@ -28,12 +47,13 @@ async def wrapper(*args, **kwargs):
2847
if hasattr(request, "data"):
2948
data = await request.data()
3049
message["data"] = [data]
31-
if hasattr(request, 'json'):
50+
if hasattr(request, "json"):
3251
json_data = await request.json()
3352
message["data"] = [json_data]
3453
logger.debug(message)
3554
except Exception as exc:
36-
logger.exception(f'Exception occurred {exc}')
55+
logger.exception(f"Exception occurred {exc}")
3756
pass
3857
return await func(*args, **kwargs)
58+
3959
return wrapper

settings.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@
2121
PAYMENT_CONFIRMATION_TEMPLATE_ID = config("WELCOME_MESSAGE_TEMPLATE_ID")
2222

2323
TEST_DATA_DIR = BASE_DIR.joinpath("tests/data")
24+
LOG_FILE_DIR = BASE_DIR.joinpath("logs")
2425
LOG_FILE_NAME = "app.json"

0 commit comments

Comments
 (0)