Python: How to create log file everyday using logg

2019-04-25 14:59发布

I'm new to logging module of python. I want to create a new log file everyday while my application is in running condition.

log file name - my_app_20170622.log
log file entries within time - 00:00:01 to 23:59:59

On next day I want to create a new log file with next day's date -

log file name - my_app_20170623.log
log file entries within time - 00:00:01 to 23:59:59

I'm using logging module of python.

I'm using like below -

log_level = int(log_level)
logger = logging.getLogger('simple')
logger.setLevel(log_level)
fh = logging.FileHandler(log_file_name)
fh.setLevel(log_level)
formatter = logging.Formatter(log_format)
fh.setFormatter(formatter)
logger.addHandler(fh)

Is their any configurations in logging module of python to create a log on daily basis?

3条回答
放荡不羁爱自由
2楼-- · 2019-04-25 15:34

I suggest you take a look at logging.handlers.TimedRotatingFileHandler. I think it's what you're looking for.

查看更多
叼着烟拽天下
3楼-- · 2019-04-25 15:38

You have to create a TimedRotatingFileHandler:

from logging.handlers import TimedRotatingFileHandler
logname = "my_app.log"
handler = TimedRotatingFileHandler(logname, when="midnight", interval=1)
handler.suffix = "%Y%m%d"
logger.addHandler(handler)

This piece of code will create a my_app.log but the log will be moved to a new log file named my_app.log.20170623 when the current day ends at midnight.

I hope this helps.

查看更多
啃猪蹄的小仙女
4楼-- · 2019-04-25 15:46

Finally, I got the correct answer and I want to share this answer.

Basically, need to create a TimedRotatingFileHandler like below -

log_format = "%(asctime)s - %(levelname)s - %(message)s"
log_level = 10
handler = TimedRotatingFileHandler("my_app.log", when="midnight", interval=1)
handler.setLevel(log_level)
formatter = logging.Formatter(log_format)
handler.setFormatter(formatter)

# add a suffix which you want
handler.suffix = "%Y%m%d"

#need to change the extMatch variable to match the suffix for it
handler.extMatch = re.compile(r"^\d{8}$") 

# finally add handler to logger    
logger.addHandler(handler)

This above code will generate file like my_app.log for current day and my_app.log.20170704 for previous day.

Hope it helps.

查看更多
登录 后发表回答