class Logger: def __init__( self , logName , LogPath = '/var/log/pythonlog/' ,LogFileName = 'autotest.log' ): logfile = LogPath + LogFileName try : os.makedirs(LogPath ) os.mknod(logfile) except : os.mknod(logfile) self ._logger = logging.getLogger(logName) handler = logging.FileHandler(logfile) formatter = logging.Formatter( '%(asctime)s %(filename)s[line:%(lineno)d] module:%(module)s %(levelname)s: %(message)s' ) handler.setFormatter(formatter) self ._logger.addHandler(handler) if GlobalVar.debug: self ._logger.setLevel(logging.INFO) else : self ._logger.setLevel(logging.DEBUG) # 记录日志并前台打印 def log( self , msg , type = 'info' ): if self ._logger is not None : if type = = 'info' : self ._logger.info(msg) mess = '[INFO ' + time.strftime( '%D %H:%M:%S' ) + '] ' + msg print mess elif type = = 'error' : self ._logger.error(msg) mess = '[ERR ' + time.strftime( '%D %H:%M:%S' ) + '] ' + msg print mess elif type = = 'warning' : self ._logger.warning(msg) mess = '[WAR ' + time.strftime( '%D %H:%M:%S' ) + '] ' + msg print mess elif type = = 'debug' : self ._logger.debug(msg) mess = '[DEB ' + time.strftime( '%D %H:%M:%S' ) + '] ' + msg print mess # 记录日志不进行前台打印,全部依照info写入日志文件。 def setIniLog( self , msg ): if self ._logger is not None : self ._logger.info(msg) |