2

When using the python sh module, there are outputs from the module itself, like the status of command it is running, etc. Is it possible to suppress that? I am not referring to the output of the command being executed, but what the module prints when it executes.

For example, if the script contains:

import pip
pip.main(['install', "sh"])

import sh

cmdArgs = ["-lrt","/tmp"]
sh.ls(cmdArgs)

When the script is run, the following is output:

<Command '/bin/ls -lrt /tmp'>: starting process
<Command '/bin/ls -lrt /tmp', pid 22235>: process started
<Command '/bin/ls -lrt /tmp', pid 22235>: process completed

I would prefer no output. I was unable to find anything in http://amoffat.github.io/sh/

Joe Friedeggs
  • 129
  • 1
  • 1
  • 8
  • It must be possible since I don't see that when I run your script. I don't know why though. – Stop harming Monica May 06 '19 at 22:53
  • Yeah, unfortunately, my script was rather large, so I only added an 'untested' snippet. The script does a pip install of sh at the beginning. I've modified the above to do that. IF I comment that out (since sh was installed on first run), it does not log. If I leave it in, it prints the log. – Joe Friedeggs May 06 '19 at 23:53

1 Answers1

5

As can be seen from https://github.com/amoffat/sh/blob/master/sh.py#L754 (found by searching the sources for <Command, then looking through the references to the function that produces that string) , these are logging messages with INFO level, so they will appear if you are using logging.

As such, they can be silenced using the standard logging machinery as per How do I disable log messages from the Requests library?:

# sh uses a wrapper over logging.Logger, its logger names are "sh.<smth>"
# see sh.Logger.__init__ : https://github.com/amoffat/sh/blob/master/sh.py#L608
logging.getLogger("sh").setLevel(logging.WARNING)
ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152