In the following code I am having a problem, the log file should get generated each day with timestamp in its name. Right now the first file has name as dashboard_asset_logs and then other subsequent log files come as dashboard_asset_logs.2022_08_18.log, dashboard_asset_logs.2022_08_19.log.
How can the first filename also have name like dashboard_asset_logs_2022_08_17.log and subsequent file have names with . removed from between and replaced by _ from dashboard_asset_logs.2022_08_18.log to dashboard_asset_logs_2022_08_18.log
Here is the logger.py code with TimedRotatorFileHandler
import logging
import os
from logging.handlers import TimedRotatingFileHandler
class Logger():
loggers = {}
logger = None
def getlogger(self, name):
# file_formatter = logging.Formatter('%(asctime)s~%(levelname)s~%(message)s~module:%(module)s~function:%(
# module)s')
file_formatter = logging.Formatter('%(asctime)s %(levelname)s [%(module)s : %(funcName)s] : %('
'message)s')
console_formatter = logging.Formatter('%(levelname)s -- %(message)s')
print(os.getcwd())
# file_name = "src/logs/" name ".log"
# file_handler = logging.FileHandler(file_name)
#
# file_handler.setLevel(logging.DEBUG)
# file_handler.setFormatter(file_formatter)
# console_handler = logging.StreamHandler()
# console_handler.setLevel(logging.DEBUG)
# console_handler.setFormatter(console_formatter)
logger = logging.getLogger(name)
# logger.addHandler(file_handler)
# logger.addHandler(console_handler)
logger.setLevel(logging.DEBUG)
file_name = "src/logs/dashboard_asset_logs"
handler = TimedRotatingFileHandler(
file_name, when="midnight", interval=1)
handler.suffix = "%Y_%m_%d.log"
handler.setLevel(logging.DEBUG)
handler.setFormatter(file_formatter)
logger.addHandler(handler)
logger.propagate = False
self.loggers[name] = logger
return logger
def getCommonLogger(self):
if self.loggers.get("dashboard_asset_logs"):
return self.loggers.get("dashboard_asset_logs")
else:
self.logger = self.getlogger("dashboard_asset_logs")
return self.logger
Here is the app_log code
from src.main import logger
import time
logger_obj = logger.Logger()
logger = logger_obj.getCommonLogger()
def log_trial():
while True:
time.sleep(10)
logger.info("----------Log - start ---------------------")
if __name__ == '__main__':
log_trial()
I run the code by using python -m src.main.app_log
CodePudding user response:
In short, you will have to write your own TimedRotatingFileHandler
implementation to make it happen.
The more important question is why you need it? File name without date is a file with log from the current day and this log is incomplete during the day. When log is rotated at midnight, old file (the one without the date) is renamed (date part is added). Just after that, a new empty file without the date part in the name is created. This behavior of log files is very common in most of the systems because it's easy to see which file is the current one and which are the archived ones (those with dates). Adding date to the file name of current one, will mess with this conception and can lead to some problems when this file will be read by people who don't know the idea begind this file naming convention.
CodePudding user response:
You might be able to use the namer
property of the handler, which you can set to a callable to customise naming. See the documentation for it.