Python logging: create log if not exists or open and continue logging if it does

The logging module’s FileHandler takes care of that for you. No need for complexity. The handler takes an optional mode parameter, to specify whether it starts writing or appending data to it. From the docs: class logging.FileHandler(filename, mode=”a”, encoding=None, delay=False) The specified file is opened and used as the stream for logging. If mode is … Read more

How to limit log file size in python

Lose basicConfig() and use RotatingFileHandler: import logging from logging.handlers import RotatingFileHandler log_formatter = logging.Formatter(‘%(asctime)s %(levelname)s %(funcName)s(%(lineno)d) %(message)s’) logFile=”C:\\Temp\\log” my_handler = RotatingFileHandler(logFile, mode=”a”, maxBytes=5*1024*1024, backupCount=2, encoding=None, delay=0) my_handler.setFormatter(log_formatter) my_handler.setLevel(logging.INFO) app_log = logging.getLogger(‘root’) app_log.setLevel(logging.INFO) app_log.addHandler(my_handler) while True: app_log.info(“data”)

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)