I have the following Python file, teststout.py
import os,sys
class Logger(object):
    def __init__(self):
        self.terminal = sys.stdout
        self.log = open("odareplayout.log", "a")
    def write(self, message):
        self.terminal.write(message)
        self.log.write(message)
    def flush(self):
        # this flush method is needed for python 3 compatibility.
        # this handles the flush command by doing nothing.
        # you might want to specify some extra behavior here.
        pass
sys.stdout = Logger()
def main():
    print('Hello World')
    cmd = 'ansible-playbook test3.yaml'
    os.system(cmd)
if __name__ == '__main__':
    main ()
and the following playbook, test3.yaml
---
 - name: Test
   hosts: localhost
   vars:
     akash: "myhost"
   tasks:
   - name: Get timestamp from the system
     shell: "date +%Y-%m-%d%H-%M-%S"
     register: tstamp
   - name: date and time
     debug:
        var: ansible_date_time
And this is the output when I run the Python script with python3 teststout.py
Hello World
PLAY [Test] *************************************************************************************************************************************
TASK [Gathering Facts] **************************************************************************************************************************
ok: [localhost]
TASK [Get timestamp from the system] ************************************************************************************************************
changed: [localhost]
TASK [date and time] ****************************************************************************************************************************
ok: [localhost] => {
    "ansible_date_time": {
        "date": "2023-03-23",
        "day": "23",
        "epoch": "1679547708",
        "hour": "05",
        "iso8601": "2023-03-23T05:01:48Z",
        "iso8601_basic": "20230323T050148745778",
        "iso8601_basic_short": "20230323T050148",
        "iso8601_micro": "2023-03-23T05:01:48.745778Z",
        "minute": "01",
        "month": "03",
        "second": "48",
        "time": "05:01:48",
        "tz": "UTC",
        "tz_offset": "+0000",
        "weekday": "Thursday",
        "weekday_number": "4",
        "weeknumber": "12",
        "year": "2023"
    }
}
PLAY RECAP **************************************************************************************************************************************
localhost                  : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  
cat odareplayout.log 
Hello World
If you see above, only print statement is getting captured in the file but I want to capture this entire output into a file along with the stdout.
I have already referred various posts and the Ansible documentation about Logging, but I didn't help.
Can someone please help here?
 
    