Redirect Python 'print' output to Logger

前端 未结 7 1870
北荒
北荒 2020-12-02 17:31

I have a Python script that makes use of \'Print\' for printing to stdout. I\'ve recently added logging via Python Logger and would like to make it so these print statement

7条回答
  •  离开以前
    2020-12-02 17:59

    You really should do that the other way: by adjusting your logging configuration to use print statements or something else, depending on the settings. Do not overwrite print behaviour, as some of the settings that may be introduced in the future (eg. by you or by someone else using your module) may actually output it to the stdout and you will have problems.

    There is a handler that is supposed to redirect your log messages to proper stream (file, stdout or anything else file-like). It is called StreamHandler and it is bundled with logging module.

    So basically in my opinion you should do, what you stated you don't want to do: replace print statements with actual logging.

提交回复
热议问题