how to Create log file for all commands in one file using python

Question:

import subprocess

print("Starting the PMAX Lun-Allocation scrict using Python............")
#step1
sid = (input("Please enter the Array SID:"))
prebackup = (input("Please enter the backup which is to backed up before the process:"))
print(f"Starting the backup Command is : symaccess -sid {sid} -f {prebackup}")
command1 = ("symaccess -sid {sid} -f {prebackup} backup")
process = subprocess.run(command1, shell=True)
command1

#step2
print(f"Listing the all availble devices for sid {sid}............")
listfile = (input("Please enter the text file name where the list of Luns should be stored"))
print(f"Starting the listing Command is : symdev -sid {sid} list -all > {listfile}")
command2=(f"symdev -sid {sid} list -all > {listfile}")
process = subprocess.run(command2, shell=True)
command2

#step3
print("creating Tdev")
tdevfile = (input("Please enter Tdev file name:"))
print(f"Running Command : symconfigure -sid {sid} -f {tdevfile} preview -v")
command3 =(f"symconfigure -sid {sid} -f {tdevfile} preview -v")
process = subprocess.run(command3, shell=True)
command3

Need to create log file in same directory with date for all process output

can you please help me with this problem I could not able to capture output in file but all output is showing in Shell

Asked By: Deepak Gowda

||

Answers:

Instead using print, you can use logger. You can view the output in console and at the same time, Log will datewise will be generated in Log folder.

import logging
import logging.handlers as handlers
import subprocess
import datetime
import os

logger = logging.getLogger("PMAX-Lun-Allocation")
logger.setLevel(logging.DEBUG)


# maintains log file here  # maintains log file here
Log = os.path.join(os.path.dirname(os.path.realpath(__file__)), "Log")
if not os.path.exists(Log):
    os.mkdir(Log)
ch = logging.StreamHandler()
ch.setLevel(logging.DEBUG)

# logHandler = handlers.TimedRotatingFileHandler(os.path.join(Log, datetime.datetime.now(
# ).strftime('log_file_%Y_%m_%d.log')), when='midnight', backupCount=10)
logHandler = handlers.RotatingFileHandler(
    os.path.join(Log, datetime.datetime.now().strftime("log_file_%Y_%m_%d.log")),
    maxBytes=5000000,
    backupCount=10,
)
logHandler.setLevel(logging.DEBUG)


# create a logging format
formatter = logging.Formatter(
    "%(asctime)s: [%(thread)d]:[%(name)s]: %(levelname)s:[PMAX-Lun-Allocation] - %(message)s"
)
logHandler.setFormatter(formatter)
ch.setFormatter(formatter)

# add the handlers to the logger

logger.addHandler(logHandler)
logger.addHandler(ch)


logger.debug("Starting the PMAX Lun-Allocation scrict using Python............")
# step1
sid = input("Please enter the Array SID:")
prebackup = input("Please enter the backup which is to backed up before the process:")
logger.debug(f"Starting the backup Command is : symaccess -sid {sid} -f {prebackup}")
command1 = "symaccess -sid {sid} -f {prebackup} backup"
process = subprocess.run(command1, shell=True)
command1

# step2
logger.debug(f"Listing the all availble devices for sid {sid}............")
listfile = input(
    "Please enter the text file name where the list of Luns should be stored"
)
logger.debug(
    f"Starting the listing Command is : symdev -sid {sid} list -all > {listfile}"
)
command2 = f"symdev -sid {sid} list -all > {listfile}"
process = subprocess.run(command2, shell=True)
command2

# step3
logger.debug("creating Tdev")
tdevfile = input("Please enter Tdev file name:")
logger.debug(f"Running Command : symconfigure -sid {sid} -f {tdevfile} preview -v")
command3 = f"symconfigure -sid {sid} -f {tdevfile} preview -v"
process = subprocess.run(command3, shell=True)
command3
Answered By: Sachin Salve
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.