• 简单实用的日志类CLog (Python版)


    clog

    #coding: utf-8
    import time
    
    '''
    /*****************************************************************
      Function     : GetCurrFmtTime
      Description  : 获取当前时间
    ******************************************************************/
    '''
    def GetCurrFmtTime() :
        t = time.localtime()
        strtime = "%02d/%02d %02d:%02d:%02d" % (t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
        return strtime
    '''
    /*****************************************************************
      日志类 CLog
    ******************************************************************/
    '''
    class CLog :
        def __init__(self, logFile='log.txt') :
            self.DEBUG = True
            self.logFile = logFile
    
        def __log__(self, *params) :
            msgs = GetCurrFmtTime()
            for msg in params:
                if msgs != "" : msgs += " "
                msgs += str(msg)
            print msgs
            self.__write__(msgs)
    
        def __write__(self, msgs=None) :
            if len(self.logFile) > 0 :
                f = open(self.logFile, 'at+')
                if msgs != None : f.write(msgs)
                f.write("
    ")
                f.close()
    
        #####################################################################
        def setDebug(self, dbgFlag ) :
            self.DEBUG = dbgFlag
    
        def setLogFile(self, logName ) :
            self.logFile = logName
    
        ###########################################
        def debug(self, *params) :
            if self.DEBUG == True :
                self.__log__('DBG:', *params)
    
        def info(self, *params) :
            self.__log__('INF:', *params)
    
        def error(self, *params) :
            self.__log__('ERR:', *params)
    
        ###########################################
        def blankLine(self) :
            print
            self.__write__()
    
        def oneLine(self, char='=') :
            line = char
            for i in range(1, 50) :
                line += char
            print line
            self.__write__(line)
    
        def titleLog(self, title) :
            char = '*'
            line = char
            for i in range(1, 50) :
                line += char
    
            msgs = '
    '
            msgs += line + '
    
    '
            msgs += '	' + title + '
    
    '
            msgs += line + '
    
    '
            print msgs
            self.__write__(msgs)
    
    #################################################################
    def test() :
        log = CLog()
        log.titleLog('测试CLog类')
        
        log.oneLine('#')
    
        log.setDebug(False)
        log.debug(2,3,4)
        log.info('Hello', 'World', 100)
        log.blankLine()
        CLog().error('This is a test.')
        
        log.setDebug(True)
        log.debug(20,30,40)
        
        log.blankLine()
        log.oneLine()
    ######################
    if __name__ == '__main__':
        test()

    python 多进程 logging:ConcurrentLogHandler

  • 相关阅读:
    挑战编程 uva100 3n+1
    《算法问题实战策略》 BOGGLE
    图论 最短路专辑
    acwing 76. 和为S的连续正数序列
    leetcode 19 删除链表的倒数第N个节点
    水文一篇 汇报下最*的学**况
    acwing 81. 扑克牌的顺子
    Solr基础理论与维护管理快速上手(含查询参数说明)
    Solr基础理论与维护管理快速上手(含查询参数说明)
    利用SolrJ操作solr API完成index操作
  • 原文地址:https://www.cnblogs.com/robinunix/p/9152159.html
Copyright © 2020-2023  润新知