How to log the output from the multiple telnet sessions to separate log file

dieter dieter at handshake.de
Wed Oct 28 02:52:25 EDT 2015


manjunatha.mahalingappa at gmail.com writes:
> ...
>  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')

This will always give you the same logger object.

I recommend to carefully read the "logging" related documentation
to understand its architecture. In your context, the logger
hierarchy and the concept of "logging handler" are of great importance.

> ...
> 		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')
This will configure the so called "root" logger - the object at
the root of the logger hierarchy. Loggers lower down in the hiearrchy
will "inherit" from loggers higher up, unless they override things.

Therefore, you will usually have a single "basicConfig" call, which
globally (independent of the individual telnet sessions) configures
common aspects. In the individual telnet sessions, you would
use session specific loggers (below the "root" logger) which would
inherit the common configuration and override session specific things
(e.g. the log file via a specific handler).




More information about the Python-list mailing list