Search code examples
pythonloggingarguments

rolling logs and specific arguments in python


I would like to be able to implement some rolling logs (once a week rolling). Arguments such as the log name and log path should be passed through in my batch file. I would need to be able to leave out the log name whilst keeping the log path in the arguments and it would take a default value for the log name.

I have tried using the logging.basicConfig but cant seem to achieve the intended result


Solution

  • import logging
    from logging.handlers import TimedRotatingFileHandler
    import inspect
    import argparse
    
    def rolling_logger(app_id: str = None, logger_name: str = None, logging_level=logging.INFO, path='C\\dir\\Logs\\',
                   log_file='file_name.log'):
        """
        Handles writing to the log file with specific style rules applied.
        Rolling logs generates a new log file every week and stores onlu one weeks worth of old logs
        
        dir
            Logs
                file_name.log.20230814
                file_name.log
        """
        print(path, log_file)
        app_id_str = f"[app-id: {app_id}]" if app_id else ""
        fmt = f"%(asctime)s {app_id_str} %(levelname)s %(threadName)s %(filename)s %(message)s"
    
        if not logger_name:
            # gets the name of the module calling get_logger
            logger_name = inspect.getmodule(inspect.stack()[1][0]).__name__
    
        logger = logging.getLogger(logger_name)
        logger.setLevel(logging_level)
    
        for handler in logger.handlers[:]:
            logger.removeHandler(handler)
    
        rotating_handler = TimedRotatingFileHandler(path + '\\' + log_file, when='W6', backupCount=1) # specifies how many logs to keep and when to roll over
        rotating_handler.setFormatter(logging.Formatter(fmt))
        logger.addHandler(rotating_handler)
    
        return logger
    
    parser = argparse.ArgumentParser(description='Parses path and log_file arguments')
    parser.add_argument('--path', default='dir/logs', type=str, help='Path to logs')
    parser.add_argument('--log_file', default='name.log', type=str, help='Log file name')
    args = parser.parse_args()
    logger = rolling_logger(path=args.path, log_file=args.log_file)
    

    This should match your needs. The when='W6' specifies the day of the week 6 (indexed at 0) that the log should roll over on. So Sunday night at midnight.