Hammaad commited on
Commit
9bfd8d8
1 Parent(s): 07ddc2f

fixed the logging part

Browse files
Files changed (2) hide show
  1. reggpt/api/router.py +2 -2
  2. reggpt/configs/logger.py +47 -41
reggpt/api/router.py CHANGED
@@ -1,5 +1,5 @@
1
  import time
2
- import logging
3
 
4
  from fastapi import APIRouter, HTTPException, status
5
  from fastapi import HTTPException, status
@@ -8,7 +8,7 @@ from reggpt.schemas.schema import UserQuery, LoginRequest, UserModel
8
  from reggpt.routers.controller import get_QA_Answers, get_avaliable_models
9
  from reggpt.configs.api import API_ENDPOINT_LOGIN,API_ENDPOINT_CHAT, API_ENDPOINT_HEALTH, API_ENDPOINT_MODEL
10
 
11
- logger = logging.getLogger(__name__)
12
 
13
  class ChatAPI:
14
 
 
1
  import time
2
+ from reggpt.configs.logger import logger
3
 
4
  from fastapi import APIRouter, HTTPException, status
5
  from fastapi import HTTPException, status
 
8
  from reggpt.routers.controller import get_QA_Answers, get_avaliable_models
9
  from reggpt.configs.api import API_ENDPOINT_LOGIN,API_ENDPOINT_CHAT, API_ENDPOINT_HEALTH, API_ENDPOINT_MODEL
10
 
11
+ # logger = logging.getLogger(__name__)
12
 
13
  class ChatAPI:
14
 
reggpt/configs/logger.py CHANGED
@@ -1,44 +1,50 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import logging
2
- import time
3
- import sys
4
- import codecs
5
- sys.stdout = codecs.getwriter("utf-8")(sys.stdout.detach())
6
- sys.stderr = codecs.getwriter("utf-8")(sys.stderr.detach())
7
-
8
- logger = logging.getLogger(__name__)
9
-
10
- stream_handler = logging.StreamHandler()
11
- log_filename = "output.log"
12
- file_handler = logging.FileHandler(filename=log_filename , encoding='utf-8')
13
- handlers = [stream_handler, file_handler]
14
-
15
- class TimeFilter(logging.Filter):
16
- def filter(self, record):
17
- return "Running" in record.getMessage()
18
-
19
-
20
- logger.addFilter(TimeFilter())
21
-
22
- # Configure the logging module
23
  logging.basicConfig(
24
- level=logging.INFO,
25
- format="%(name)s %(asctime)s - %(levelname)s - %(message)s",
26
- handlers=handlers,
 
 
27
  )
28
-
29
- import logging
30
-
31
-
32
- def time_logger(func):
33
- """Decorator function to log time taken by any function."""
34
-
35
- # @wraps(func)
36
- def wrapper(*args, **kwargs):
37
- start_time = time.time() # Start time before function execution
38
- result = func(*args, **kwargs) # Function execution
39
- end_time = time.time() # End time after function execution
40
- execution_time = end_time - start_time # Calculate execution time
41
- logger.info(f"Running {func.__name__}: --- {execution_time} seconds ---")
42
- return result
43
-
44
- return wrapper
 
1
+ """
2
+ /*************************************************************************
3
+ *
4
+ * CONFIDENTIAL
5
+ * __________________
6
+ *
7
+ * Copyright (2024-2025) AI Labs, IronOne Technologies, LLC
8
+ * All Rights Reserved
9
+ *
10
+ * Author : Theekshana Samaradiwakara
11
+ * Description : This file contains the application logging logic.
12
+ * CreatedDate : 14/10/2024
13
+ * LastModifiedDate :
14
+ *************************************************************************/
15
+ """
16
+
17
+ '''
18
+ TO DO : Implement the api endpoint router seperately
19
+ '''
20
+ import datetime
21
  import logging
22
+ import os
23
+ import pytz
24
+
25
+ def filer():
26
+ """
27
+ Returns the log file path based on the current date and ensures the directory exists.
28
+ """
29
+ colombo_tz = pytz.timezone('Asia/Colombo')
30
+ today = datetime.datetime.now(colombo_tz)
31
+ # log_dir = "logs"#for local
32
+ log_dir = "usr/app/logs"#for docker
33
+
34
+ if not os.path.exists(log_dir):
35
+ os.makedirs(log_dir)
36
+
37
+ log_filename = f"{log_dir}/{today.year}-{today.month:02d}-{today.day:02d}.log"
38
+ print("logged",log_filename)
39
+ return log_filename
40
+
41
+ file_handler = logging.FileHandler(filer())
42
+ file_handler.setLevel(logging.INFO)
43
  logging.basicConfig(
44
+ level=logging.DEBUG,
45
+ format="%(asctime)s %(levelname)s (%(name)s) : %(message)s",
46
+ datefmt="%Y-%m-%d %H:%M:%S",
47
+ handlers=[file_handler],
48
+ force=True,
49
  )
50
+ logger = logging.getLogger(__name__)