2017-09-11 78 views
0

logclient.py文件看起来像下面Python记录问题

此文件实现了es_logger类可以提取 行号和文件主叫用户线, 的信息,并与此消息一起分发消息给 服务器

class es_logger: 
    def __init__(self, logname='es_general'): 
     rootLogger = logging.getLogger('') 
     rootLogger.setLevel(logging.DEBUG) 
     socketHandler = logging.handlers.SocketHandler('localhost', 
         logging.handlers.DEFAULT_TCP_LOGGING_PORT) 
     rootLogger.addHandler(socketHandler) 
     self.es_logger = logging.getLogger(logname) 

def __display__(self, msg, level='DEBUG'): 
    levels=['DEBUG', 'INFO', 'WARNING', 'ERROR', 'CRITICAL'] 
    if not level in levels: 
     level = 'DEBUG' 
     ##msg = 'Defaulting to DEBUG Level' + msg 
    if level == 'DEBUG': 
     self.es_logger.debug(msg) 
    if level == 'INFO': 
     self.es_logger.info(msg) 
    if level == 'WARNING': 
     self.es_logger.warning(msg) 
    if level == 'ERROR': 
     self.es_logger.error(msg) 
    if level == 'CRITICAL': 
     self.es_logger.critical(msg) 
    return 


def logmsg(self, msg, level='DEBUG'): 
    ''' 
    To be called from the python code. 
    ''' 
    log_time=time.asctime() 
    code_pos=inspect.stack()[1][1:3] 
    code_file=code_pos[0] 
    code_line=code_pos[1] 
    msgstr='{0} : {1} : {2} : {3}'.format(log_time, code_file, 
              code_line, msg) 
    self.__display__(msgstr, level) 
    return 

我没有发布我的服务器代码,因为它不是必需的我想这里。 所以我用这个logclient.py在另一模块即wd_update.py如下

import logclient 
import requests 

WDU_DATA = 'http://192.168.0.100/cgi-bin//time' 
mylogger=logclient.es_logger() 

mylogger.logmsg("Hi") 
mylogger.logmsg("Hi cgoma INFO", "INFO") 
try: 
    r = requests.get(WDU_DATA) 
except Exception as err: 
    print "Error while reaching endpoint" 

当我运行wd_update.py文件我越来越日志,

Hi 
Hi cgoma INFO 
Starting new HTTP connection (1): 192.168.0.100 

我期待得到印刷只有前两行,但我猜Python请求模块也有记录在其中实现,因为来自请求模块的日志语句Starting new HTTP connection (1): 192.168.0.100*也正在打印,我不想要。请让我知道如何禁止来自Python request模块的日志消息。

在此先感谢。

+0

可能的重复[如何禁用来自请求库的日志消息?](https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-图书馆) – georgexsh

回答

0

似乎还有已经有很多答案:11029717

logging.getLogger('requests').setLevel(logging.ERROR) 
0

您配置根记录器,所以它会从propagate = True任何记录仪收集的信息 - 这是默认的。

FWIW,你应该只使用logging.DictConfig()在您的应用程序的顶层,并在代码中使用logger = logging.getLogger(__name_) - 这样一来,所有您需要做的是properly setup the config dict和你的库代码是完全独立的从配置(这是在点日志包)。