最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python實(shí)現(xiàn)的系統(tǒng)實(shí)用log類實(shí)例

 更新時(shí)間:2015年06月30日 10:07:03   作者:liujian0616  
這篇文章主要介紹了python實(shí)現(xiàn)的系統(tǒng)實(shí)用log類,實(shí)例分析了Python基于logging模塊實(shí)現(xiàn)日志類的相關(guān)技巧,需要的朋友可以參考下

本文實(shí)例講述了python實(shí)現(xiàn)的系統(tǒng)實(shí)用log類。分享給大家供大家參考。具體如下:

每個(gè)系統(tǒng)都必不可少會(huì)需要一個(gè)log類,方便了解系統(tǒng)的運(yùn)行狀況和排錯(cuò),python本身已經(jīng)提供了一個(gè)logger了,很強(qiáng)大,只要稍微封裝一下就可以放到自己的系統(tǒng)了,下面是我自己的log類

文件名:logger.py

"""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):
    """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')

執(zhí)行結(jié)果:

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

如果只需要顯示在文件中可以將debug和nofork選項(xiàng)都置為false

希望本文所述對(duì)大家的Python程序設(shè)計(jì)有所幫助。

相關(guān)文章

  • 刪除PyCharm解釋器的方法步驟

    刪除PyCharm解釋器的方法步驟

    這篇文章主要給大家介紹了關(guān)于刪除PyCharm解釋器的方法步驟,PyCharm解釋器是指在PyCharm集成開發(fā)環(huán)境中用于運(yùn)行和調(diào)試Python代碼的解釋器,需要的朋友可以參考下
    2023-09-09
  • Python正則表達(dá)式re.sub()用法詳解

    Python正則表達(dá)式re.sub()用法詳解

    re.sub用于替換字符串中的匹配項(xiàng),下面這篇文章主要給大家介紹了關(guān)于Python正則表達(dá)式re.sub()用法的相關(guān)資料,文中通過實(shí)例代碼以及圖文介紹的非常詳細(xì),需要的朋友可以參考下
    2022-09-09
  • Python讀寫常用數(shù)據(jù)文件的示例詳解

    Python讀寫常用數(shù)據(jù)文件的示例詳解

    Python?提供了多種強(qiáng)大的工具和庫,可以輕松實(shí)現(xiàn)對(duì)各種類型文件的讀寫操作,本文為大家整理了Python讀寫常用的那些數(shù)據(jù)文件的方法,希望對(duì)大家有所幫助
    2025-01-01
  • Python判斷for循環(huán)最后一次的6種方法

    Python判斷for循環(huán)最后一次的6種方法

    在Python中,通常我們不會(huì)直接判斷for循環(huán)是否正在執(zhí)行最后一次迭代,因?yàn)镻ython的for循環(huán)是基于可迭代對(duì)象的,它不知道也不關(guān)心迭代的內(nèi)部狀態(tài)(比如當(dāng)前是第幾次迭代),但是,我們可以使用一些技巧來間接地實(shí)現(xiàn)這個(gè)需求,需要的朋友可以參考下
    2025-01-01
  • Python實(shí)現(xiàn)打印金字塔圖案的方法詳解

    Python實(shí)現(xiàn)打印金字塔圖案的方法詳解

    使用簡(jiǎn)單的?for?循環(huán)在?python?中打印模式。第一個(gè)外循環(huán)用于處理行數(shù),?而內(nèi)嵌套循環(huán)用于處理列數(shù)。操作打印語句,可以打印不同的數(shù)字圖案、字母圖案或星形圖案。本文將利用這些方法實(shí)現(xiàn)打印金字塔圖案,需要的可以參考一下
    2022-09-09
  • python讀csv文件時(shí)指定行為表頭或無表頭的方法

    python讀csv文件時(shí)指定行為表頭或無表頭的方法

    這篇文章主要介紹了python讀csv文件時(shí)指定行為表頭或無表頭的方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-06-06
  • Python基于tkinter模塊實(shí)現(xiàn)的改名小工具示例

    Python基于tkinter模塊實(shí)現(xiàn)的改名小工具示例

    這篇文章主要介紹了Python基于tkinter模塊實(shí)現(xiàn)的改名小工具,結(jié)合實(shí)例形式分析了tkinter模塊操作文件后綴名的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下
    2017-07-07
  • python中子類調(diào)用父類函數(shù)的方法示例

    python中子類調(diào)用父類函數(shù)的方法示例

    Python中類的初始化方法是__init__(),因此父類、子類的初始化方法都是這個(gè),下面這篇文章主要給大家介紹了關(guān)于python中子類調(diào)用父類函數(shù)的方法示例,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下。
    2017-08-08
  • python高并發(fā)異步服務(wù)器核心庫forkcore使用方法

    python高并發(fā)異步服務(wù)器核心庫forkcore使用方法

    這篇文章主要介紹了python高并發(fā)異步服務(wù)器核心庫forkcore的使用方法,大家參考使用吧
    2013-11-11
  • Flask實(shí)現(xiàn)的接口響應(yīng)中存在中文時(shí)接口返回為unicode亂碼的解決方法

    Flask實(shí)現(xiàn)的接口響應(yīng)中存在中文時(shí)接口返回為unicode亂碼的解決方法

    本文給大家分享了新版Flask實(shí)現(xiàn)的接口響應(yīng)中存在中文時(shí)接口返回為unicode亂碼的解決方法,文中通過代碼示例和圖文介紹的非常詳細(xì),如果有遇到相同問題的朋友,可以參考閱讀本文
    2023-11-11

最新評(píng)論

清丰县| 乐亭县| 东至县| 商洛市| 罗城| 申扎县| 普兰店市| 灵璧县| 睢宁县| 承德县| 长顺县| 南雄市| 交口县| 长寿区| 石景山区| 龙江县| 当阳市| 安新县| 宣化县| 平塘县| 贵德县| 宁武县| 天门市| 普兰县| 元朗区| 灌南县| 南雄市| 阳东县| 伊宁市| 黄浦区| 静安区| 星子县| 永兴县| 中方县| 仁化县| 汕尾市| 台山市| 叙永县| 江城| 红桥区| 周至县|