pythonoutput-buffering

Save console output in txt file as it happens


I want to save my console output in a text file, but I want it to be as it happens so that if the program crashes, logs will be saved. Do you have some ideas?

I can't just specify file in logger because I have a lot of different loggers that are printing into the console.


Solution

  • I think that you indeed can use a logger, just adding a file handler, from the logging module you can read this

    As an example you can use something like this, which logs both to the terminal and to a file:

    import logging
    from pathlib import Path
    
    root_path = <YOUR PATH>
    
    log_level = logging.DEBUG
    
    # Print to the terminal
    logging.root.setLevel(log_level)
    formatter = logging.Formatter("%(asctime)s | %(levelname)s | %(message)s", "%Y-%m-%d %H:%M:%S")
    stream = logging.StreamHandler()
    stream.setLevel(log_level)
    stream.setFormatter(formatter)
    log = logging.getLogger("pythonConfig")
    if not log.hasHandlers():
        log.setLevel(log_level)
        log.addHandler(stream)
    
    # file handler:
    file_handler = logging.FileHandler(Path(root_path / "process.log"), mode="w")
    file_handler.setLevel(log_level)
    file_handler.setFormatter(formatter)
    log.addHandler(file_handler)
    
    log.info("test")
    

    If you have multiple loggers, you can still use this solutions as loggers can inherit from other just put the handler in the root logger and ensure that the others take the handler from that one.

    As an alternative you can use nohup command which will keep the process running even if the terminal closes and will return the outputs to the desired location:

    nohup python main.py > log_file.out &