Skip to content

Commit 0d12a3a

Browse files
exowandererrti
authored andcommitted
created logger.py to serve get_logger to all modules
1 parent bd62fbb commit 0d12a3a

File tree

4 files changed

+41
-29
lines changed

4 files changed

+41
-29
lines changed

gswikichat/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1+
# from .logger import logger
12
from .api import *

gswikichat/api.py

Lines changed: 3 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,10 @@
88
from .vector_store_interface import embedder, retriever, input_documents
99

1010
from haystack import Document
11+
from .logger import get_logger
1112

12-
import logging
13-
import sys
14-
15-
# TODO: Test if this can be added to the `__init__.py` file
16-
# TODO: Add volume to Dockerfile for `gbnc_api.log` file
17-
# Source: https://docs.python.org/3/howto/logging.html
18-
logging.basicConfig(
19-
filename='gbnc_api.log',
20-
encoding='utf-8',
21-
level=logging.DEBUG
22-
)
23-
24-
# Source: https://stackoverflow.com/questions/14058453/
25-
# making-python-loggers-output-all-messages-to-stdout-in-addition-to-log-file
26-
logger = logging.getLogger('gswikicat api')
27-
logger.setLevel(logging.DEBUG)
28-
29-
handler = logging.StreamHandler(sys.stdout)
30-
handler.setLevel(logging.DEBUG)
31-
formatter = logging.Formatter(
32-
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
33-
)
34-
handler.setFormatter(formatter)
35-
logger.addHandler(handler)
36-
# End of logging logger configuration
37-
13+
# Create logger instance from base logger config in `logger.py`
14+
logger = get_logger(__name__)
3815

3916
STATIC_DIR = 'frontend/dist'
4017
LANDING_PAGE = f'/{STATIC_DIR}'

gswikichat/llm_config.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
import os
22
from haystack_integrations.components.generators.ollama import OllamaGenerator
33

4-
import logging
5-
logger = logging.getLogger()
4+
from .logger import get_logger
5+
6+
# Create logger instance from base logger config in `logger.py`
7+
logger = get_logger(__name__)
8+
69

710
OLLAMA_MODEL_NAME = os.environ.get("OLLAMA_MODEL_NAME")
811
OLLAMA_URL = os.environ.get("OLLAMA_URL")
@@ -14,7 +17,8 @@
1417

1518
logger.debug(f'I AM HERE')
1619

17-
print(f"Setting up ollama with {OLLAMA_MODEL_NAME}")
20+
logger.info(f"Setting up ollama with {OLLAMA_MODEL_NAME}")
21+
1822
llm = OllamaGenerator(
1923
model=OLLAMA_MODEL_NAME,
2024
url=OLLAMA_GENERATE_URL

gswikichat/logger.py

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import logging
2+
import sys
3+
4+
5+
def get_logger(name):
6+
# Create a logger
7+
# Source: https://docs.python.org/3/howto/logging.html
8+
logging.basicConfig(
9+
filename='gbnc_api.log',
10+
encoding='utf-8',
11+
level=logging.DEBUG
12+
)
13+
14+
logger = logging.getLogger(name)
15+
logger.setLevel(logging.DEBUG) # Set the logging level
16+
17+
# Source: stackoverflow.com/questions/14058453/
18+
# making-python-loggers-output-all-messages-
19+
# to-stdout-in-addition-to-log-file
20+
21+
# Create console handler and set level to debug
22+
handler = logging.StreamHandler(sys.stdout)
23+
handler.setLevel(logging.DEBUG)
24+
formatter = logging.Formatter(
25+
'%(asctime)s - %(name)s - %(levelname)s - %(message)s'
26+
)
27+
handler.setFormatter(formatter)
28+
logger.addHandler(handler)
29+
30+
return logger

0 commit comments

Comments
 (0)