|
"This module takes care of the logging
Logger helps in creating a logging system for the application
Logging is initialised by function LoggerInit.
"""
Import logging
Import OS
Import sys
Class logger (object ):
"Class provides methods to perform logging ."""
M_logger = None
Def _ init _ (self, opts, logfile ):
"Set the default logging path ."""
Self. opts = opts
Self. myname = 'dxscs'
Self. logdir = '.'
Self. logfile = logfile
Self. filename = OS. path. join (self. logdir, self. logfile)
Def loginit (self ):
"Callfunction LoggerInit to start initialising the logging system ."""
Logdir = OS. path. normpath (OS. path. expanduser (self. logdir ))
Self. logfilename = OS. path. normpath (OS. path. expanduser (self. filename ))
If not OS. path. isdir (logdir ):
Try:
OS. mkdir (logdir)
Counter t OSError, e:
Msg = ('(% s)' % e)
Print msg
Sys. exit (1)
Self. logger_init (self. myname)
Def logger_init (self, loggername ):
"Initialise the logging system.
This operation des logging to console and a file. By default, console prints
Messages of level WARN and above and file prints level INFO and above.
In DEBUG mode (-D command line option) prints messages of level DEBUG
And above to both console and file.
Args:
Loggername: String-Name of the application printed along with the log
Message.
"""
Fileformat = '[% (asctime) s] % (name) s: [% (filename) s: % (lineno) d]: % (levelname)-8 s: % (message) s'
Logger. m_logger = logging. getLogger (loggername)
Logger. m_logger.setLevel (logging. INFO)
Self. console = logging. StreamHandler ()
Self. console. setLevel (logging. CRITICAL)
Consformat = logging. Formatter (fileformat)
Self. console. setFormatter (consformat)
Self. filelog = logging. FileHandler (filename = self. logfilename, mode = 'W + ')
Self. filelog. setLevel (logging. INFO)
Self. filelog. setFormatter (consformat)
Logger. m_logger.addHandler (self. filelog)
Logger. m_logger.addHandler (self. console)
If self. opts ['debug'] = True:
Self. console. setLevel (logging. DEBUG)
Self. filelog. setLevel (logging. DEBUG)
Logger. m_logger.setLevel (logging. DEBUG)
If not self. opts ['nofork']:
Self. console. setLevel (logging. WARN)
Def logstop (self ):
"Shutdown logging process ."""
Logging. shutdown ()
# Test
If _ name _ = '_ main __':
# Debug mode & not in daemon
Opts = {'debug': True, 'nofork': True}
Log = logger (opts, 'dxscs _ source. log ')
Log. loginit ()
Log. m_logger.info ('hello, World ')
|