This article is an example of a python implementation of the system utility log class. Shared for your reference. Specific as follows:
Each system is essential will need a log class, easy to understand the system's operating conditions and troubleshooting, python itself has provided a logger, very powerful, as long as a little encapsulation can be put into their own systems, the following is my own log class
Filename:
"""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.""" = opts = 'dxscs' = '.' = logfile = (, ) def loginit(self): """Calls function LoggerInit to start initialising the logging system.""" logdir = (()) = (()) if not (logdir): try: (logdir) except OSError, e: msg = ('(%s)'%e) print msg (1) self.logger_init() def logger_init(self, loggername): """Initialise the logging system. This includes 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)-8s: %(message)s' logger.m_logger = (loggername) logger.m_logger.setLevel() = () () consformat = (fileformat) (consformat) = (filename=, mode='w+') () (consformat) logger.m_logger.addHandler() logger.m_logger.addHandler() if ['debug'] == True: () () logger.m_logger.setLevel() if not ['nofork']: () def logstop(self): """Shutdown logging process.""" () #test if __name__ == '__main__': #debug mode & not in daemon opts = {'debug':True,'nofork':True} log = logger(opts, 'dxscs_source.log') () log.m_logger.info('hello,world')
Implementation results:
Both the terminal and the file show the following:[2012-09-06 16:56:01,498] dxscs: [: 88]: INFO : hello,world
If you only want to display in the file you can set both the debug and nofork options to false.
I hope that what I have described in this article will help you in your Python programming.