0

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.

halfer
  • 19,824
  • 17
  • 99
  • 186
cgoma
  • 47
  • 2
  • 13
  • Possible duplicate of [How do I disable log messages from the Requests library?](https://stackoverflow.com/questions/11029717/how-do-i-disable-log-messages-from-the-requests-library) – georgexsh Sep 11 '17 at 14:00

2 Answers2

0

seems there already have many answers: 11029717

logging.getLogger('requests').setLevel(logging.ERROR)
georgexsh
  • 15,984
  • 2
  • 37
  • 62
0

You configured the root logger, so it will collect messages from any logger with propagate = True - which is the default.

FWIW you should just use logging.DictConfig() at your app's top-level and use logger = logging.getLogger(__name_) in your code - this way all you have to do is to properly setup the config dict and your library code is totally independant from the configuration (which is the point of the logging package).

bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118