The logclient.py
file look like below
This file implements an es_logger
class which can extract the
line number and file information of the calling line,
and dispatch the message along with this message to
the server
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
I am not posting my server code since it is not needed I guess here.
So I am using this logclient.py
in another module i.e wd_update.py
as follow
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"
when I am running wd_update.py
file I am getting logs as
Hi
Hi cgoma INFO
Starting new HTTP connection (1): 192.168.0.100
I am expecting only first two lines to get printed but I guess Python requests module also having logging implemented in it because of which log statement Starting new HTTP connection (1): 192.168.0.100*
from requests module is also getting printed which I don't want. Please let me know how to suppress log messages from Python request
module.