I know that there are many other similar questions to mine, but I have not found one that deals specifically with a log file having a timestamp as a part of its name.
I have 4 modules (currently) that I want to implement logging within. I also have a base_logger.py
script that I am using to create my logging object. The code is below:
import configparser
import logging
import sys
from datetime import datetime
from pathlib import Path
def write_default_config(path: Path):
# This function creates a 'config.ini' if it does not already exist
conf = configparser.ConfigParser()
conf['logging'] = {'level': 'INFO'}
with open(path, 'w') as configfile:
conf.write(configfile)
def setup_config_and_logging():
# Create a location for the log file to be saved to, and find the config file
logs_dir_name = "logs"
config_file_name = "config.ini"
if getattr(sys, 'frozen', False): # if running as .exe, save logs to folder in cwd
script_dir = Path(sys.executable).parent.absolute()
logs_dir = script_dir / logs_dir_name
config_file_path = script_dir / config_file_name
else: # if running in IDE, save logs to folder at src level
script_dir = Path(__file__).parent.absolute()
logs_dir = script_dir.parent.parent / logs_dir_name
config_file_path = script_dir.parent.parent / config_file_name
logs_dir.mkdir(parents=True, exist_ok=True)
if not config_file_path.exists():
write_default_config(config_file_path)
# Read the logging level
config = configparser.ConfigParser()
config.read(config_file_path)
logging_level = config.get("logging", "level", fallback="DEBUG")
# Path for the log file (name dependent on logging level)
log_file_path = logs_dir / f"app-{logging_level}-{datetime.now().strftime('%Y%m%d-%H%M%S')}.log"
# Setup logging with the specified level
level = getattr(logging, logging_level.upper(), logging.DEBUG) # DEBUG is the default value
logging.basicConfig(level=level,
format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
handlers=[
logging.FileHandler(log_file_path),
logging.StreamHandler()
])
def get_logger() -> logging.Logger:
# Provide a logger instance for other modules to use
return logging.getLogger()
As you can see, the filename for the log files (log_file_path
) contains a timestamp for when the log file was created. However, this ends up creating 4 log files as the modules that import it all occur at different times (e.g. app-INFO-20240529-115900.log
, app-INFO-20240529-115901.log
, …).
If I were to remove the timestamp from the filename, only one file is created and it correctly contains the logged information from all of my modules.
Just to be clear, when importing the logging object into other modules for use, I use this code:
from base_logger import get_logger
log = get_logger()
I have previously tried to define a global variable initialised
that would be checked within each import of my base_logger.py
module, but this global value would be reset to False
each time, hence my check to see whether it was True
(i.e. if the logger had already been created) always turned up false, and hence the logger was recreated.
Any advice or solutions would be greatly appreciated!