国产探花免费观看_亚洲丰满少妇自慰呻吟_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

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 绥芬河市| 晋州市| 孝感市| 巧家县| 垣曲县| 高邑县| 龙川县| 平远县| 吴堡县| 三河市| 三江| 苍南县| 宜春市| 利辛县| 灯塔市| 河曲县| 墨江| 松滋市| 广宗县| 岗巴县| 文成县| 贵港市| 交城县| 凭祥市| 塔城市| 河津市| 昔阳县| 抚顺县| 松溪县| 莲花县| 通榆县| 大宁县| 谢通门县| 溆浦县| 拉萨市| 上犹县| 晋江市| 衡阳县| 内江市| 赤壁市| 汝南县|