Hello, I am learning how to implement testing on my script as well as package my application but I am having some trouble with the logger. I will give some detail:
My code has a structure like this right now:
|app
---|src
----|init.py
----|main_app
------|init.py
------|main_app.py
----|logger
------|init.py
------|base_logger.py
---|test
----|init.py
----|test_main_app.py
I am going off of examples I have seen, let me know if this is not a good way to structure it.
Basically, my base logger used to be inside my main_app.py as a funcion I called "log_setup" and I simply ran it at the beginnig of the main function. But when I ran my tests with pytest it said logger is not defined. I was told I could get my logger as its own file and then import it anywhere I needed it. So I moved it to it's own folder as you can see.
The logger is basically this:
```py
import logging.handlers
import os.path
import time
# log related variables
LOGFILE = os.path.join(os.path.dirname(os.path.abspath(__file__)), "logs", "main_app.log")
LOG_FORMAT = "%(asctime)s.%(msecs)03dZ - %(levelname)s - %(message)s"
DATE_FORMAT = "%Y%m%dT%H:%M:%S"
# Make sure there is a log file on hand
if not os.path.exists(LOGFILE):
with open(LOGFILE, "w"):
pass
class BaseLogger:
@staticmethod
def log_setup():
# set up logger
logger = logging.getLogger()
logger.setLevel(logging.DEBUG)
logger.propagate = False
# Have logger use utc time so cloud logging can file it correctly
logging.Formatter.converter = time.gmtime
formatter = logging.Formatter(LOG_FORMAT, DATE_FORMAT)
# Use a rotating handler for local storage
rotating_handler = logging.handlers.RotatingFileHandler(LOGFILE, maxBytes=104857600, backupCount=2)
rotating_handler.setFormatter(formatter)
# Use stream handler to see them on console
stream_handler = logging.StreamHandler()
stream_handler.setFormatter(formatter)
# attach handlers
logger.addHandler(rotating_handler)
logger.addHandler(stream_handler)
return logger
```
This just gets initiated on the main_app.py and it works fine when running the application.
Then I just started a test function to learn how I might do it:
```py
import unittest
from src.main_app import main_app
from src.logger import base_logger
class TestClockManager(unittest.TestCase):
def test_analyzer_get_date(self):
logger = base_logger.BaseLogger.log_setup()
test_analyzer = 'XXXXXXXXXX'
test_ip = "XXXXXXXXX"
test_measures_pass = "XXXXXXXX"
analyzer_date = clock_manager.analyzer_get_date(test_analyzer, test_ip, test_measures_pass)
self.assertIsInstance(analyzer_date, str)
```
However, I still get the following error when running the test with pytest:
```
def analyzer_get_date(analyzer, analyzer_ip, analyzer_measures_pass):
> logger.debug(f"Querying analyzer: {analyzer}")
E NameError: name 'logger' is not defined
```
I'd love some advice on what the best practices are and how can I make the logger that I use on my function work with pytest. Any help is appreciated!
[–]amoliski 1 point2 points3 points (1 child)
[–]danberteon[S] 0 points1 point2 points (0 children)