Last active
August 13, 2021 03:19
-
-
Save cleiveliu/ed5ecfc7dc074a85ccf3d11cd38ea827 to your computer and use it in GitHub Desktop.
Revisions
-
cleiveliu revised this gist
Mar 30, 2021 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -4,22 +4,23 @@ import contextlib import sys def your_task(): print("The test !") @contextlib.contextmanager def close_stdout(redirect_to="/var/log/your.log"): raw_stdout = sys.stdout file = open(redirect_to, "a+") sys.stdout = file yield sys.stdout = raw_stdout file.close() with close_stdout(redirect_to="./test.log"): your_task() ``` -
cleiveliu revised this gist
Feb 5, 2021 . 1 changed file with 2 additions and 1 deletion.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,4 +1,5 @@ [source](http://magic.iswbm.com/zh/latest/c05/c05_09.html) ```Python import contextlib import sys -
cleiveliu revised this gist
Feb 5, 2021 . 1 changed file with 5 additions and 4 deletions.There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -1,11 +1,12 @@ # http://magic.iswbm.com/zh/latest/c05/c05_09.html ```Python import contextlib import sys log_file = "/var/log/your.log" def your_task(): print("The test !") @contextlib.contextmanager def close_stdout(): @@ -19,5 +20,5 @@ def close_stdout(): file.close() with close_stdout(): your_task() ``` -
cleiveliu renamed this gist
Feb 5, 2021 . 1 changed file with 0 additions and 0 deletions.There are no files selected for viewing
File renamed without changes. -
cleiveliu created this gist
Feb 5, 2021 .There are no files selected for viewing
This file contains hidden or 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 charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,23 @@ # http://magic.iswbm.com/zh/latest/c05/c05_09.html ```Python import contextlib log_file="/var/log/you.log" def you_task(): pass @contextlib.contextmanager def close_stdout(): raw_stdout = sys.stdout file = open(log_file, 'a+') sys.stdout = file yield sys.stdout = raw_stdout file.close() with close_stdout(): you_task() ```