Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python script, log to screen and to file

I have this script, and I want now to put the output on screen and into a log file. Anyone who can help me on how to do this?

PS: don't mind my debug lines plz

Thx

#!/usr/bin/python


import os
import subprocess
import sys
import argparse
from subprocess import Popen, PIPE, call

parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url', help='       Add here the url you want     to use. Example: www.google.com')
parser.add_argument('-o', '--output', help='    Add here the output file for logging')
args = parser.parse_args()


print args.url
print args.output

cmd1 = ("ping -c 4 "+args.url)
cmd2 = cmd1, args.url

print cmd2
print cmd1

p = subprocess.Popen(cmd2, shell=True, stderr=subprocess.PIPE)
like image 914
Bert Colemont Avatar asked Dec 04 '25 15:12

Bert Colemont


1 Answers

You can use the logging module and communicate() method of a subprocess process:

import logging    
import argparse
import subprocess

def initLogging( args ):
    formatString = '[%(levelname)s][%(asctime)s] : %(message)s' # specify a format string
    logLevel = logging.INFO # specify standard log level
    logging.basicConfig( format=formatString , level=logLevel, datefmt='%Y-%m-%d %I:%M:%S')
    log_file = args.output 
    fileHandler = logging.FileHandler( log_file )
    logging.root.addHandler( fileHandler ) # add file handler to logging

parser = argparse.ArgumentParser()
parser.add_argument('-u', '--url', help='       Add here the url you want     to use. Example: www.google.com')
parser.add_argument('-o', '--output', help='    Add here the output file for logging')
args = parser.parse_args()
initLogging( args )

cmd = [ "ping" , "-c" ,"4", args.url ]

p = subprocess.Popen( cmd, stdout=subprocess.PIPE, stderr=subprocess.PIPE )
stdout_string , stderr_string = p.communicate() # receive stdout, stderr, take care, this is a blocking call,
# stdout_string or stderr_string could be of type None

logging.info( stderr_string )
logging.info( stdout_string )

This will log to stdout and to a file.

You can even add more handlers e.g. stream handlers with

logging.addHandler( logging.StreamHandler( someStreamlikeObject ) )

One other thing: You should never use shell=True unless necessary because it is unsafe and brings some technical conditions (see subprocess documentation). The above code is altered in a way which does not use shell=True.

like image 183
Waschbaer Avatar answered Dec 06 '25 04:12

Waschbaer