Jun-02-2019, 06:48 PM
An example using logging and catch subprocess output.
Using logzero simplify logging,as don't have to write all that boilerplate logging code.
Using logzero simplify logging,as don't have to write all that boilerplate logging code.
import subprocess import logzero from logzero import logger # 3 rotations, each with a maximum filesize of 1MB: logzero.logfile("logfile.log", maxBytes=1e6, backupCount=3, disableStderrLogger=True) out = subprocess.run(['ping', 'google.com'], capture_output=True) output = out.stdout.decode() logger.info(output)So now output only goes to logfile,
disableStderrLogger=True
False
if want output also in console.Output:[I 190602 20:45:39 log_zero:10]
Pinging google.com [2a00:1450:400f:809::200e] with 32 bytes of data:
Request timed out.
Request timed out.
Reply from 2a00:1450:400f:809::200e: time=94ms
Reply from 2a00:1450:400f:809::200e: time=84ms
Ping statistics for 2a00:1450:400f:809::200e:
Packets: Sent = 4, Received = 2, Lost = 2 (50% loss),
Approximate round trip times in milli-seconds:
Minimum = 84ms, Maximum = 94ms, Average = 89ms