I'm writing a buildscript. I want to log everything that is going on, but I also want to see everything while it is running. What's the best practice to do this?
Asked
Active
Viewed 608 times
0
-
2https://docs.python.org/3/library/logging.html? – jonrsharpe Nov 30 '20 at 17:26
-
As far as I understand it, this just logs to a file, but doesn't print to the screen. Am I missing something? – waldelb Nov 30 '20 at 17:36
-
It logs to *whatever you set it up to*, you can have multiple handlers for e.g. a file (rotated or otherwise), a stream, ... – jonrsharpe Nov 30 '20 at 17:38
-
Do you have an example? I tried logging.addHandler, but that seems to not be working. – waldelb Nov 30 '20 at 17:53
-
1The docs have several examples and there are plenty of questions on SO and articles elsewhere covering the topic. If after researching it you still have a specific problem, [edit] to give a [mre]. – jonrsharpe Nov 30 '20 at 17:55
1 Answers
0
I will recommend to use tee command to save output to file
Just write print statement in python script and pipe content to tee command
Tee command capture std in and print and write it to file.
e.g. console content will be saved to build.log
Linux $ your_buildscript.py 2>&1 | tee build.log
Windows
your_buildscript.py | tee build.log
Refernece: Linux tee: https://man7.org/linux/man-pages/man1/tee.1.html
windows tee: https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.utility/tee-object?view=powershell-7.1

Kalpesh Chavan
- 56
- 10