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

  • 相关阅读:
    【QT 学习笔记】 一、 VS2015+ QT环境安装
    Ubuntu16.04 + caffe + cuda 环境搭建
    【Caffe学习笔记】一 、环境安装 Caffe + cuda + windows10 + VS2015 安装笔记, win7也适用
    【CNN】 吴恩达课程中几种网络的比较
    【图像处理】二 高斯滤波及其加速方法
    【图像处理 】 一、OSTU分割法
    1028: C语言程序设计教程(第三版)课后习题8.2
    函数和带参的宏,从三个数中找出最大的数
    Last Defence (run time error)
    Penalty
  • 原文地址:https://www.cnblogs.com/robinunix/p/9152159.html
Copyright © 2020-2023  润新知