How to create daily log folder in python logging
Question: I want to make the log file output into daily folder in python.

I can make the log path in hander like "../myapp/logs/20150514/xx.log" through
current date. But the problem is that the log path doesn't change when the
date changes.

I create the log instance while i start my **long-running** python script
xx.py, and now the instance's log path is "../myapp/logs/20150514/xx.log". But
on tomorrow, as the instance is not changed, so its path is still
"../myapp/logs/20150514/xx.log" which should be
"../myapp/logs/20150515/xx.log".

How can i make the log output into daily folder?

My get log instance codes:

    
    
    import os
    import utils
    import logging
    from logging.handlers import RotatingFileHandler
    import datetime
    
    def getInstance(file=None):
        global logMap
        if file is None:
            file = 'other/default.log'
        else:
            file = file + '.log'
        if(logMap.has_key(file)):
            return logMap.get(file)
        else:
            visit_date = datetime.date.today().strftime('%Y-%m-%d')
            date_file = os.path.join(visit_date,file)
            log_path = utils.read_from_ini('log_path').strip()
            log_path = os.path.join(log_path,date_file);
            if not os.path.isdir(os.path.dirname(log_path)):
                os.makedirs(os.path.dirname(log_path))
            logging.basicConfig(datefmt='%Y-%m-%d %H:%M:%S',level=logging.INFO)
            log_format = '[%(asctime)s][%(levelname)s]%(filename)s==> %(message)s'
            formatter = logging.Formatter(log_format)
            log_file = RotatingFileHandler(log_path, maxBytes=10*1024*1024,backupCount=5)
            log_file.setLevel(logging.INFO)
            log_file.setFormatter(formatter)
            instance = logging.getLogger(file)
            instance.addHandler(log_file)
            logMap[file] = instance
            return instance
    


Answer: Your `RotatingFileHandler` doesn't rotate on a time basis, but rather a size
basis. That's what the `maxBytes` argument is for. If you want to rotate based
on time, use a `TimedRotatingFileHandler` instead. Note that this works with
filenames, but not paths (as far as I know). You can have 20150505.log,
20150506.log, but not 20150505/mylog.log, 20150506/mylog.log.

If you want to rotate folder names you could probably do it by subclassing the
TimedRotatingFileHandler and adding your own logic.

