print(,flush=True) and assert ,print(,flush=True) doesn't show up in the log correctly

I have a loop in python, with both regular print() statements, and assert clauses which also do print() statements. Both of them say flush=True.

When the assert happens, it is in the middle near to the end, and not where it really happened…

For example…

2024-01-04T20:30:49.5018283Z looping for secret smartpoint and usergws 
2024-01-04T20:30:49.5019411Z Traceback (most recent call last):
2024-01-04T20:30:49.5022574Z   File "/runner/_work/_actions/Tvlport-SPC/spc-infra-deploy-service-manifest-action/main/make-keyvault-secrets-action/../app/make-keyvault-secrets.py", line 15, in <module>
2024-01-04T20:30:49.5025344Z     main()
2024-01-04T20:30:49.5028119Z   File "/runner/_work/_actions/Tvlport-SPC/spc-infra-deploy-service-manifest-action/main/make-keyvault-secrets-action/../app/make-keyvault-secrets.py", line 11, in main
2024-01-04T20:30:49.5030923Z     make_keyvault_secrets(environment)
2024-01-04T20:30:49.5033797Z   File "/runner/_work/_actions/Tvlport-SPC/spc-infra-deploy-service-manifest-action/main/app/src/make_keyvault_secrets.py", line 173, in make_keyvault_secrets
2024-01-04T20:30:49.5038875Z     assert (FoundDefault or FoundEnviron), print(f"Bad things!  secret ***productnamefull***-***secretname*** does not have a nonprod-unenc.yml file nor a ***environstub***-unenc.yml file, the world is ending!", flush=True)
2024-01-04T20:30:49.5042369Z NameError: name 'secretname' is not defined
2024-01-04T20:30:49.5044423Z      default file /runner/_work/_temp/KSSharedComplete/smartpoint-usergws/nonprod-unenc.yml has 4 entries
2024-01-04T20:30:49.5046791Z         entry is as follows, kvsecretname is smartpoint-usergws-default---USER-GWS, value is EvolvePP_XR7, originalsecretname is usergws
2024-01-04T20:30:49.5049550Z         entry is as follows, kvsecretname is smartpoint-usergws-default---SECRET-GWS, value is EvolveXR7, originalsecretname is usergws
2024-01-04T20:30:49.5052870Z         entry is as follows, kvsecretname is smartpoint-usergws-default---HOSTACCESSPROFILE-GWS, value is EVO_1G_XR7_9FA7BB, originalsecretname is usergws
2024-01-04T20:30:49.5054922Z looping for secret smartpoint and launchdarklycredentials 
2024-01-04T20:30:49.5056829Z      default file /runner/_work/_temp/KSSharedComplete/smartpoint-launchdarklycredentials/nonprod-unenc.yml has 4 entries
2024-01-04T20:30:49.5060171Z         entry is as follows, kvsecretname is smartpoint-launchdarklycredentials-default---SPC-SDK-KEY, value is sdk-3b1b2441-9b9a-4946-b397-78f8ae77d811, originalsecretname is launchdarklycredentials
2024-01-04T20:30:49.5064134Z         entry is as follows, kvsecretname is smartpoint-launchdarklycredentials-default---SPC-CLIENT-SDK-KEY, value is 6333257c62e30211c361ae2f, originalsecretname is launchdarklycredentials
2024-01-04T20:30:49.5068331Z         entry is as follows, kvsecretname is smartpoint-launchdarklycredentials-default---SPC-SERVICE-READER, value is api-7a02de3d-9110-4a5c-8d54-3866572aa59e, originalsecretname is launchdarklycredentials
2024-01-04T20:30:49.5072480Z         entry is as follows, kvsecretname is smartpoint-launchdarklycredentials-default---SPC-SERVICE-WRITER, value is api-62f79391-4b0b-441e-9518-5897358d2d49, originalsecretname is launchdarklycredentials
2024-01-04T20:30:49.5074889Z looping for secret smartpoint and cdn 
2024-01-04T20:30:49.5446726Z ##[error]Process completed with exit code 1.

Looking at the logs, the traceback is shown right after the line “looping for secret smartpoint and usergws”, but the problem really happened at the bottom of the file “looping for secret smartpoint and cdn”…

This means that even though a normal print statement, and a print associated with an assert happen, they aren’t sequenced in the log file the way. And I have flush=True

Snippet of my code:

print(f'looping for secret {productnamefull} and {originalsecretname} ', flush=True)

and

assert (FoundDefault or FoundEnviron), print(f"Bad things!  secret {productnamefull}-{secretname} does not have a nonprod-unenc.yml file nor a {environstub}-unenc.yml file, the world is ending!", flush=True)

What am I missing, how to fix it?

I’m fairly new to python, so my C/C# stuff is showing, but still…

The most important thing that is missing here, is enough code for others to try to reproduce the problem, in order to see it themselves and understand it. Without that, we are only guessing from your description (and if you don’t know how to solve the problem, then how can you be sure that you’re showing the important parts?).

So, please first try to create a proper example.

Putting print in the assert is not necessary.

assert cond, “message if cond it not true”

This will print a traceback with the message if the cond is false.