的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
模块的日志消息。
在此先感谢。
可能的重复[如何禁用来自请求库的日志消息?](https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-图书馆) – georgexsh