• 简单实用的日志类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

  • 相关阅读:
    Oracle 11g RAC到单实例ASM的物理Standby搭建
    Oracle"log file sync"等待分析
    RAC ASM到单机ASM的dataguard创建步骤
    Kafka学习之路 (五)Kafka在zookeeper中的存储
    A highly available WEB cluster based on HAProxy + KeepAlived
    postgresql 10数据库备份
    Oracle DG+sqlserver alwayson主节点切换时OGG源端目标端容灾实践
    Kafka学习之路 (三)Kafka的高可用
    趋势图
    火狐浏览器
  • 原文地址:https://www.cnblogs.com/robinunix/p/9152159.html
Copyright © 2020-2023  润新知