国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python實現的系統實用log類實例

2020-02-23 01:38:58
字體:
來源:轉載
供稿:網友

本文實例講述了python實現的系統實用log類。分享給大家供大家參考。具體如下:

每個系統都必不可少會需要一個log類,方便了解系統的運行狀況和排錯,python本身已經提供了一個logger了,很強大,只要稍微封裝一下就可以放到自己的系統了,下面是我自己的log類

文件名:logger.py

"""This module takes care of the logginglogger helps in creating a logging system for the application Logging is initialised by function LoggerInit."""import loggingimport osimport sysclass 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):    """Calls function 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)      except 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 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 = 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')

執行結果:

終端和文件中都顯示有:[2012-09-06 16:56:01,498] dxscs: [logger.py: 88]: INFO    : hello,world

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 竹山县| 湘阴县| 吴江市| 增城市| 会东县| 云霄县| 云林县| 论坛| 南昌县| 大理市| 体育| 房山区| 宣化县| 彩票| 永川市| 县级市| 宁远县| 磴口县| 西昌市| 孝义市| 东丽区| 平果县| 古田县| 会东县| 图片| 诸城市| 咸丰县| 筠连县| 克山县| 泗水县| 克东县| 平遥县| 汝州市| 探索| 珠海市| 阳高县| 河池市| 沾益县| 遂昌县| 监利县| 汶上县|