On Friday, October 16, 2015 at 11:11:58 PM UTC-7, dieter wrote: > manjunatha.mahalinga...@gmail.com writes: > > I'm very much new to python. > > I'm doing the automation for networking device testing , I will be opening > > the 4 telnet session, and doing some testing operations on each of those > > telnet sessions and capture or log the respective output in 4 different log > > files. > > Personally, I find it a bit strange that each (telnet) session > should get its own logfile, but, if that is what you need, I would > approach it as follows: > > * define a class "TelnetSession"; create a logger in its "__init__" method; > use this logger for all operations inside a "TelnetSession" > > * instantiate the class "TelnetSession" for each telnet session you > want to open. > Use those objects method to operate on the sessions.
Hello Dieter, I created the my own class MyLogger and passing log file name to it. I'm seeing no log is being written to passed log file instead everything is written to the logfilename [actually logfilename is variable with file name] I'm trying to create MyLogger object for each telnet session. and use that object. MyLogger is beeing called inside the Telnetsession class. those are present in PmTelnet3.py file. class MyLogger(): import logging def __init__(self,logfilename): #create logger with 'spam_application' self.logger = self.logging.getLogger('TelnetSession') self.logger.setLevel(self.logging.INFO) # create file handler which logs even debug messages #self.fh = self.logging.FileHandler(logfile) print "The log file name is %s\n" % logfilename self.logging.basicConfig(filename = logfilename, level = self.logging.INFO , format= '%(asctime)s %(name)-12s %(levelname)-8s %(message)s', datefmt='%m-%d %H:%M', filemode='w') # create console handler with a higher log level self.ch = self.logging.StreamHandler() def Log(self): self.logger.setLevel(self.logging.INFO) self.fh.setLevel(self.logging.INFO) self.ch.setLevel(self.logging.INFO) self.fh.setFormatter(self.formatter) self.ch.setFormatter(self.formatter) self.logger.addHandler(self.ch) class TelnetSession: import telnetlib import logging def __init__(self, inHost, inPort, Logname): self.log = MyLogger(Logname) self.telnet = self.telnetlib.Telnet(inHost, inPort) ---------------------------------------------------------------------------- def IP_port(file) : global Testbed_info Testbed_info = [] F = open(file, 'r') F.seek(0) line = F.read() tuples = re.findall(r'(.+ \d)\s+(.+?)\s+(\d+)', line) for (dname, ip, port) in tuples : LogFileName = dname.replace(" ","") + ".log" #Log = open(logfile, "a") #Log = MyLogger(LogFileName) Telnet_handle=PmTelnet3.TelnetSession(ip,port,LogFileName) tuple = (dname, ip, port, Telnet_handle ) print tuple Testbed_info.append(tuple) #T.append(T1) return(Testbed_info) #This function is to switch the console file = '/users/manmahal/MANJU/IP_port.txt' Testbed_info = IP_port(file) print "Iam done" for (dname, ip, port, Telnet) in Testbed_info : My_handle = Telnet #My_log = log My_handle.Write("\n") My_handle.Write("show config \n") time.sleep(2) output = My_handle.ReadBuffer() #My_fp.write(My_handle.ReadBuffer()) My_handle.log.logger.info(output) My_handle.log.logger.info("HI THERE") Kindly let me know how can I fix this.. -- https://mail.python.org/mailman/listinfo/python-list