pythonloggingpython-logging

Create a log file only when there were logs


Python version: 3.11.8

Hi everyone,

I am using the package logging to log of the execution. I am using logging.basicConfig to output the logs in a log file. I added it at the beginning of my python script. I realised a file was created when invoking logging.basicConfig. However, I do not want to create a log file if there were no data to log. Currently, I am getting empty files in my repository and I am having issues with a lot of files created in that repository. This is a part of my script to show you how I am using the logging package. How can I create the log file only when there were logs?.

def create_target_file(logs_file_path: str,
                       platform: str,
                        )-> str:

    try:
        
        data ={}

        try:
            logging.basicConfig(filename=logs_file_path,
                                filemode='a',
                                level=logging.INFO,
                                format="%(levelname)s | %(asctime)s | %(message)s", 
                                datefmt="%Y-%m-%d %H:%M:%S")
        except OSError as e:
            sleep(10)
            logging.basicConfig(filename=logs_file_path,
                                filemode='a',
                                level=logging.INFO,
                                format="%(levelname)s | %(asctime)s | %(message)s", 
                                datefmt="%Y-%m-%d %H:%M:%S")
        
          if platform == 'DRD':
              logging.info("Logging some important stuff")

     except Exception as e:
        
        logging.error(str(e))
        data['error_message'] = str(e)
        data['stack_trace'] = format_exc()
    
     finally:
        return dumps(data)

I am using a try-except block to prevent an error when creating the log file with logging.basicConfig. My goal is to create a log file only when the function logging is used. For example if platform is not equal to "DRD" and there was no errors, then the file must not be created.


Solution

  • Behind the scenes basicConfig() creates a FileHandler which by default immediately creates the file. FileHandler does have an argument - delay - to delay file creation until a log is written, but it is not accessible through basicConfig(). You will have to create the handler yourself.

    import logging
    
    logs_file_path = 'logfile.txt'
    
    fhandler = logging.FileHandler(logs_file_path, mode='a', delay=True)
    
    logging.basicConfig(handlers=[fhandler],
                        level=logging.INFO,
                        format="%(levelname)s | %(asctime)s | %(message)s",
                        datefmt="%Y-%m-%d %H:%M:%S")
    
    # at this point no file has been created/opened
    
    logging.info('some log') # this line creates a file and writes to it