Last active
June 9, 2020 08:25
-
-
Save jogu/05bb15530f19540c3016a630b0b1f050 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
joseph@button:~$ python3 | |
Python 3.4.2 (default, Sep 16 2019, 12:55:35) | |
[GCC 4.9.2] on linux | |
Type "help", "copyright", "credits" or "license" for more information. | |
>>> import time | |
>>> import sys | |
>>> | |
>>> class TimestampFilter: | |
... # pending_output stores any output passed to write where a \n has not yet been found | |
... pending_output = '' | |
... def write(self, message): | |
... output = self.pending_output + message | |
... (output, not_used, self.pending_output) = output.rpartition('\n') | |
... if output != '': | |
... timestamp = time.strftime("%Y-%m-%d %X") | |
... output = timestamp + " " + output.replace("\n", "\n"+timestamp+" ") | |
... print(output, file=sys.__stdout__) | |
... sys.__stdout__.flush() | |
... | |
>>> sys.stdout = TimestampFilter() | |
>>> print('hi') | |
2020-06-09 09:21:13 hi | |
>>> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment